Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6138449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:52:14+00:00 2026-05-23T17:52:14+00:00

I am trying to add an item, first by using an add button, then

  • 0

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 more than one item.

AddScreen.class (My first activity):

    package com.painLogger;
    **IMPORTS**
    public class AddScreen extends Activity implements OnClickListener,
    OnItemClickListener {
/** Called when the activity is first created. */
Button addButton;
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
String painLevelString, timeOfPainString, textTreatmentString,
        painLocation, row1, row2;
TextView painTitle;
boolean myFirstTime;
int k;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addscreen);
    // if it's the first time opening the app, then go to 'AddMyInfo.class'
    SharedPreferences settings = this.getSharedPreferences("MyApp", 0);
    SharedPreferences.Editor e = settings.edit();
    boolean firstrun = settings.getBoolean("firstrun", true);
    if (firstrun) {
        e.putBoolean("firstrun", false);
        e.commit();
        Intent intent = new Intent(this, AddMyInfo.class);
        startActivity(intent);
    }
    //initialize painTitle and set its font
    painTitle = (TextView) findViewById(R.id.painTitle);
    Typeface font = Typeface.createFromAsset(getAssets(),
            "Chantelli_Antiqua.ttf");
    painTitle.setTypeface(font);
    listthings = (ListView) findViewById(R.id.listthings);
    from = new String[] { "row_1" + painItems.size(),
            "row_2" + painItems.size() };
    to = new int[] { R.id.row1, R.id.row2 };
    adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
            from, to);
    listthings.setAdapter(adapter);
    listthings.setOnItemClickListener(this);
    addButton = (Button) findViewById(R.id.addButton);
    addButton.setOnClickListener(this);

}

@Override
//on the activityresult,get the string extra, then add the item to the list
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if(this.getIntent().hasExtra("row1")){
     row1 = this.getIntent().getStringExtra("row1");
     row2 = this.getIntent().getStringExtra("row2");
     painLevelString = 
             this.getIntent().getStringExtra("com.painLogger.painLevel");
     painLocation = this.getIntent().getStringExtra("painLocation");
     timeOfPainString = 
             this.getIntent().getStringExtra("com.painLogger.painTime");
     textTreatmentString = 
             this.getIntent().getStringExtra("com.painLogger.treatment");
     addItem();
    }
}
 // to add the item, put it in the map, and add the map into the list
private void addItem() {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("row_1" + painItems.size(), row1);
    map.put("row_2" + painItems.size(), row2);
    painItems.add(map);
    adapter.notifyDataSetChanged();

}

public void onClick(View v) {
    // When '+' clicked, go to the PainLoggerActivity.java, where you can
    // click enter, and that sends those strings back to here, where I can
    // incorporate them into a list view, same as was there in the
    // PainLogger Activity

    Intent goToFields = new Intent (this, PainLoggerActivity.class);
    //put the desired extras into the intent
    startActivityForResult(goToFields, 1);


}

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    Intent intent = new Intent(this, Item1.class);
    intent.putExtra("com.painLogger.painLevel", painLevelString);
    intent.putExtra("com.painLogger.painTime", timeOfPainString);
    intent.putExtra("com.painLogger.treatment", textTreatmentString);
    intent.putExtra("painLocation", painLocation);
    startActivity(intent);
}

}  

My PainLoggerActivity, where I enter various info to be put into the list:

package com.painLogger;
// imports
public class PainLoggerActivity extends Activity implements OnClickListener,
OnItemClickListener,
OnKeyListener {

/** Called when the activity is first created. */
EditText txtItem, txtItem2, timeOfPain, textTreatment, painLevel;

Button btnAdd;
ListView listItems;
ArrayAdapter<String> aa;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
int[] to;
String[] from;
SimpleAdapter adapter;
String timeOfPainString, textTreatmentString, painLevelString, painLocation;
Spinner s;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    txtItem = (EditText) findViewById(R.id.txtItem);
    txtItem2 = (EditText) findViewById(R.id.txtItem2);
    timeOfPain = (EditText)findViewById(R.id.timeOfPain);
    textTreatment = (EditText)findViewById(R.id.textTreatment);
    painLevel = (EditText)findViewById(R.id.painLevel);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    listItems = (ListView) findViewById(R.id.listItems);

    btnAdd.setOnClickListener(this);

    from = new String[] { "row_1", "row_2" };
    to = new int[] { R.id.row1, R.id.row2 };

    adapter = new SimpleAdapter(this, painItems,
            R.layout.mylistlayout, from, to);
    listItems.setAdapter(adapter);

    listItems.setOnItemClickListener(this);
    s = (Spinner) findViewById(R.id.spinner1);      
    ArrayAdapter<?>adapter = ArrayAdapter.createFromResource(
            this, R.array.planets, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);



}

private void addItem() {
    timeOfPainString = timeOfPain.getText().toString();
    textTreatmentString = textTreatment.getText().toString();
    painLevelString = s.getSelectedItem().toString();
    painLocation = txtItem.getText().toString();
    Intent intent = new Intent (this, AddScreen.class);
    intent.putExtra("com.painLogger.painLevel", painLevelString);
    intent.putExtra("com.painLogger.painTime", timeOfPainString);
    intent.putExtra("com.painLogger.treatment", textTreatmentString);
    intent.putExtra("painLocation", painLocation);
    setResult(1, intent);
    finish();

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("row_1", txtItem.getText().toString());
    map.put("row_2", txtItem2.getText().toString());
    painItems.add(map);
    adapter.notifyDataSetChanged();


    }


public void onClick(View v) {
    if (v == this.btnAdd) {
        addItem();

    }

}

public boolean onKey(View v, int keyCode, KeyEvent event) {

    if (event.getAction() == KeyEvent.ACTION_DOWN
            && keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        this.addItem();

    }
    return false;

}

public void onItemClick(AdapterView<?> a, View v, int position, long id) {

        timeOfPainString = timeOfPain.getText().toString();
        textTreatmentString = textTreatment.getText().toString();
        painLevelString = s.getSelectedItem().toString();
        painLocation = txtItem.getText().toString();
        //create the intent
        Intent intent = new Intent (this, Item1.class);
        //put the desired extras into the intent
        intent.putExtra("com.painLogger.painLevel", painLevelString);
        intent.putExtra("com.painLogger.painTime", timeOfPainString);
        intent.putExtra("com.painLogger.treatment", textTreatmentString);
        intent.putExtra("pain location", painLocation);


        startActivity(intent);
        //set the variables to "", empty

}

}

My Item1.class is simply where the app goes when an item on the list is clicked. it has no other purpose.

I am having problem because when I add the item in the list, it replaces the previous one, and I am left with only one item.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:52:14+00:00Added an answer on May 23, 2026 at 5:52 pm

    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.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;
        }
    
        @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.adddonebutton:
                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/adddonebutton" 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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add together two lists so the first item of one
I'm trying to add an item to a DropDownList in a Setter, and the
I am trying to add a new menu item to Eclipse's popupmenus. It really
I'm trying add a tab to my web page that looks like this: Using
Im trying to add an element to a database and then return a new
I'm trying to completely replace a fragment back stack with one I generate based
I'm trying to add dynamic content to my mvc application. I'm using Steven Sanderson
Ok so I'm trying to move items from one listbox to another by using
I'm trying to select the first list item li in the ul#suckerfishnav in the
Trying to add an onclick handler to my tabs, and can't seem to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.