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
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:
private static List<Entry<String, String>> painItems = ....
should do the trick.
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;
public class TestResultActivity extends Activity implements OnClickListener {
private static final int REQUEST_LIST_ITEM = 235; //arbitrary request code
static final String ADAPTER_VALUE_1 = "head";
static final String ADAPTER_VALUE_2 = "sub";
ArrayList<HashMap<String, String>> masterlist = null;
ListAdapter adapter = null;
ListView listview = null;
Button addbutton = null;
public ListAdapter getAdapter() {
if(adapter == null){
adapter = new SimpleAdapter(this, getMylist(), R.layout.listitem, new String[] {ADAPTER_VALUE_1, ADAPTER_VALUE_2}, new int[] {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 = new ArrayList<HashMap<String,String>>();
}
return masterlist;
}
public void addListItem(String head, String sub){
HashMap<String, String> addme=new HashMap<String, String>();
addme.put(ADAPTER_VALUE_1, head);
addme.put(ADAPTER_VALUE_2, sub);
masterlist.add(addme);
}
@Override
public void onCreate(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
}
@Override
public void onClick(View v) {
Intent launchadditemI = new Intent(this, AddItemActivity.class);
startActivityForResult(launchadditemI, REQUEST_LIST_ITEM);
}
@Override
public void onActivityResult(int req, int res, Intent data){
if(req == REQUEST_LIST_ITEM && res == RESULT_OK){
String thishead = data.getStringExtra(ADAPTER_VALUE_1);
String thissub = 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;
public class AddItemActivity extends Activity implements OnClickListener {
EditText headeredittext = null;
EditText subheadedittext = null;
Button donebutton = null;
Button cancelbutton = 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.affffdonebutton);
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;
}
@Override
public void onCreate(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();
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.addcancelbutton:
finish();
break;
case R.id.affffdonebutton:
sendResult();
break;
default:
break;
}
}
private void sendResult() {
Intent resultI = new Intent();
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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/maintitle"/>
<ListView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/mainlistview"></ListView>
<Button android: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"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/LinearLayout01">
<TextView android:layout_width="wrap_content" android:text="Header:" android:layout_height="wrap_content" android:id="@+id/TextView01"></TextView>
<EditText android:layout_weight="1" android:layout_width="fill_parent" android:id="@+id/headedittext" android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/linearLayout2">
<TextView android:text="Subhead:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_weight="1" android:layout_width="fill_parent" android:id="@+id/subedittext" android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/linearLayout1">
<Button android:text="Cancel" android:id="@+id/addcancelbutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Add Item" android:id="@+id/affffdonebutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/listheading" android:layout_width="wrap_content"
android:layout_height="26dip" android:layout_alignParentTop="true"
android:textSize="20sp" />
<TextView android: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>
<string name="maintitle">List Items:</string>
<string name="app_name">TestResultActivity</string>
<string name="addbuttontext">Add New Item</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arrdude.forumanswer"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestResultActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddItemActivity"></activity>
</application>
</manifest>
Happy coding!