Note that there are some explanatory texts on larger screens.

plurals
  1. POadding item to listView after passing info through an intent in Android
    primarykey
    data
    text
    <p>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.</p> <p>AddScreen.class (My first activity):</p> <pre><code> package com.painLogger; **IMPORTS** public class AddScreen extends Activity implements OnClickListener, OnItemClickListener { /** Called when the activity is first created. */ Button addButton; SimpleAdapter adapter; List&lt;HashMap&lt;String, String&gt;&gt; painItems = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); 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&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); 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&lt;?&gt; 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); } } </code></pre> <p>My PainLoggerActivity, where I enter various info to be put into the list:</p> <p>package com.painLogger; // imports public class PainLoggerActivity extends Activity implements OnClickListener, OnItemClickListener, OnKeyListener {</p> <pre><code>/** Called when the activity is first created. */ EditText txtItem, txtItem2, timeOfPain, textTreatment, painLevel; Button btnAdd; ListView listItems; ArrayAdapter&lt;String&gt; aa; List&lt;HashMap&lt;String, String&gt;&gt; painItems = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); 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&lt;?&gt;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&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); 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 &amp;&amp; keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { this.addItem(); } return false; } public void onItemClick(AdapterView&lt;?&gt; 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 } } </code></pre> <p>My Item1.class is simply where the app goes when an item on the list is clicked. it has no other purpose.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload