Skip to content Skip to sidebar Skip to footer

Adding Item To Listview After Passing Info Through An Intent In Android

I am trying to add an item, first by using an add button, then going to a different activity, then coming back to the original one and adding it in a listview. I can't seem to have

Solution 1:

Here is a tested prototype:

TestResultActivity.java

package com.arrdude.forumanswer;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

publicclassTestResultActivityextendsActivityimplementsOnClickListener {
    privatestaticfinalintREQUEST_LIST_ITEM=235; //arbitrary request codestaticfinalStringADAPTER_VALUE_1="head";
    staticfinalStringADAPTER_VALUE_2="sub";

    ArrayList<HashMap<String, String>> masterlist = null;
    ListAdapteradapter=null;

    ListViewlistview=null;
    Buttonaddbutton=null;

    public ListAdapter getAdapter() {
        if(adapter == null){
            adapter = newSimpleAdapter(this, getMylist(), R.layout.listitem, newString[] {ADAPTER_VALUE_1, ADAPTER_VALUE_2}, newint[] {R.id.listheading, R.id.listsubheading});
        }
        return adapter;
    }

    public ListView getListview() {
        if(listview==null){
            listview = (ListView) findViewById(R.id.mainlistview);
            listview.setAdapter(getAdapter());
        }
        return listview;
    }

    public Button getAddbutton() {
        if(addbutton==null){
            addbutton = (Button) findViewById(R.id.mainaddbutton);
            addbutton.setClickable(true);
            addbutton.setOnClickListener(this);
        }
        return addbutton;
    }

    public ArrayList<HashMap<String, String>> getMylist() {
        if(masterlist==null){
            masterlist = newArrayList<HashMap<String,String>>();
        }
        return masterlist;
    }

    publicvoidaddListItem(String head, String sub){
        HashMap<String, String> addme=newHashMap<String, String>();
        addme.put(ADAPTER_VALUE_1, head);
        addme.put(ADAPTER_VALUE_2, sub);

        masterlist.add(addme);
    }

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //lazy init objects not the most efficient in Android but easier reading
        getListview();
        getAddbutton();
        addListItem("Test Head", "Test sub heading here below"); //an initial item for testing
    }

    @OverridepublicvoidonClick(View v) {
        IntentlaunchadditemI=newIntent(this, AddItemActivity.class);
        startActivityForResult(launchadditemI, REQUEST_LIST_ITEM);
    }

    @OverridepublicvoidonActivityResult(int req, int res, Intent data){
        if(req == REQUEST_LIST_ITEM && res == RESULT_OK){
            Stringthishead= data.getStringExtra(ADAPTER_VALUE_1);
            Stringthissub= data.getStringExtra(ADAPTER_VALUE_2);
            addListItem(thishead, thissub);
        }
    }
}

AddItemActivity.java

package com.arrdude.forumanswer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

publicclassAddItemActivityextendsActivityimplementsOnClickListener {
    EditTextheaderedittext=null;
    EditTextsubheadedittext=null;
    Buttondonebutton=null;
    Buttoncancelbutton=null;

    public EditText getHeaderedittext() {
        if(headeredittext==null){
            headeredittext = (EditText) findViewById(R.id.headedittext);
        }
        return headeredittext;
    }

    public EditText getSubheadedittext() {
        if(subheadedittext==null){
            subheadedittext = (EditText) findViewById(R.id.subedittext);
        }
        return subheadedittext;
    }

    public Button getDonebutton() {
        if(donebutton==null){
            donebutton = (Button) findViewById(R.id.adddonebutton);
            donebutton.setClickable(true);
            donebutton.setOnClickListener(this);
        }
        return donebutton;
    }

    public Button getCancelbutton() {
        if(cancelbutton==null){
            cancelbutton = (Button) findViewById(R.id.addcancelbutton);
            cancelbutton.setOnClickListener(this);
        }
        return cancelbutton;
    }

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.additem);
        //lazy init objects not the most efficient in Android but easier reading
        getHeaderedittext();
        getSubheadedittext();
        getDonebutton();
        getCancelbutton();
    }

    @OverridepublicvoidonClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.addcancelbutton:
            finish();
            break;

        case R.id.adddonebutton:
            sendResult();
            break;

        default:
            break;
        }
    }

    privatevoidsendResult() {
        IntentresultI=newIntent();
        resultI.putExtra(TestResultActivity.ADAPTER_VALUE_1, getHeaderedittext().getText().toString());
        resultI.putExtra(TestResultActivity.ADAPTER_VALUE_2, getSubheadedittext().getText().toString());
        setResult(RESULT_OK, resultI);
        finish();
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
    ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/maintitle"/><ListViewandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/mainlistview"></ListView><Buttonandroid:id="@+id/mainaddbutton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/addbuttontext"></Button></LinearLayout>

additem.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/LinearLayout01"><TextViewandroid:layout_width="wrap_content"android:text="Header:"android:layout_height="wrap_content"android:id="@+id/TextView01"></TextView><EditTextandroid:layout_weight="1"android:layout_width="fill_parent"android:id="@+id/headedittext"android:layout_height="wrap_content"><requestFocus></requestFocus></EditText></LinearLayout><LinearLayoutandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/linearLayout2"><TextViewandroid:text="Subhead:"android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView><EditTextandroid:layout_weight="1"android:layout_width="fill_parent"android:id="@+id/subedittext"android:layout_height="wrap_content"></EditText></LinearLayout><LinearLayoutandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/linearLayout1"><Buttonandroid:text="Cancel"android:id="@+id/addcancelbutton"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Buttonandroid:text="Add Item"android:id="@+id/adddonebutton"android:layout_width="wrap_content"android:layout_height="wrap_content"></Button></LinearLayout></LinearLayout>

listitem.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/listheading"android:layout_width="wrap_content"android:layout_height="26dip"android:layout_alignParentTop="true"android:textSize="20sp" /><TextViewandroid:id="@+id/listsubheading"android:layout_width="wrap_content"android:layout_height="22dip"android:layout_below="@id/listheading"android:textSize="16sp" /></RelativeLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?><resources><stringname="maintitle">List Items:</string><stringname="app_name">TestResultActivity</string><stringname="addbuttontext">Add New Item</string></resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.arrdude.forumanswer"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="4" /><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".TestResultActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".AddItemActivity"></activity></application></manifest>

Happy coding!

Solution 2:

First, Hashmap is suppose to be a collection, it seems you need only a pair of object, which, Map.Entry is far better solution (or even better, use a custom inner class).

Second, you add the item each time the addScreen Activity is created, and each time it is created, your painItems are freshly created. That's the reason you see only first content there, you need to have some way to persist your list (at least within application life cycle). Quick fix (though may not be desirable) is to put static:

privatestaticList<Entry<String, String>> painItems = ....

should do the trick.

Post a Comment for "Adding Item To Listview After Passing Info Through An Intent In Android"