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.
Here is a tested prototype:
TestResultActivity.java
AddItemActivity.java
main.xml
additem.xml
listitem.xml
strings.xml
AndroidManifest.xml
Happy coding!