Reputation: 2116
For an android application, I needed to create a list that would allow me to enter different things into the same 'item'. For example, each one of the objects in the list would need different information(each with a separate row). Specifically, my app would want the following for each list item: pain location, treatment, type of pain, and some other categories per instance.
This is what I have gathered so far, but it only displays one TextView per item on the list:
PACKAGE, Imports, etc.
public class PainLoggerActivity extends Activity implements OnClickListener,
OnKeyListener {
/** Called when the activity is first created. */
EditText txtItem;
Button btnAdd;
ListView listItems;
ArrayList <String> painItems;
ArrayAdapter<String> aa;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtItem = (EditText)findViewById(R.id.txtItem);
btnAdd = (Button)findViewById(R.id.btnAdd);
listItems = (ListView)findViewById(R.id.listItems);
btnAdd.setOnClickListener(this);
txtItem.setOnKeyListener(this);
painItems = new ArrayList<String>();
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
painItems);
listItems.setAdapter(aa);
}
private void addItem(String item){
if(item.length() > 0){
this.painItems.add(item);
this.aa.notifyDataSetChanged();
this.txtItem.setText("");
}
}
@Override
public void onClick(View v) {
if(v == this.btnAdd)
this.addItem(this.txtItem.getText().toString());
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode ==
KeyEvent.KEYCODE_DPAD_CENTER){
this.addItem(this.txtItem.getText().toString());
}
return false;
}
}
*Some sample code would be appreciated, so I have an idea how to implement suggestions - no doubt, I am still a beginner *
Upvotes: 1
Views: 417
Reputation: 6452
It's not possible to have multiple TextView items when using the layout "simple_list_item_1" for your Adapter's layout ... that's why they call it "simple - item - 1".
If you want a total of 2 TextViews per row you can use the "simple_list_item_2" but I actually recommend you go with using a SimpleAdapter and creating your own row entry layout file. It's much more flexible and you can include as many text items, images, check boxes, or anything else you can think of, versus just a couple of text items on a single row.
Food for thought ...
Do a search for SimpleAdapter examples to see how to implement this. Not a big deal, and you'll find it's a "gift that keeps on giving."
Upvotes: 1