I need help in creating a custom listview that allows me to have 2 strings/textviews per row. I have been researching a lot, but I cannot seem to understand how to do this. Any sample code and help would be appreciated. I know how to use simple_list_item_1, but not my own layout.
Thank YOU.
My (Still Non-Functioning) Code
package com.painLogger;
//ALL IMPORTS
public class PainLoggerActivity extends Activity implements OnClickListener,
OnKeyListener {
/** Called when the activity is first created. */
EditText txtItem;
EditText txtItem2;
Button btnAdd;
ListView listItems;
ArrayAdapter < String > aa;
List < HashMap < String, String >> painItems = new ArrayList < HashMap < String, String >> ();
int[] to;
String[] from;
SimpleAdapter adapter;
@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);
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
};
SimpleAdapter adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout,
from, to);
listItems.setAdapter(adapter);
}
private void addItem() {
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();
}
@Override
public void onClick(View v) {
if (v == this.btnAdd) {
addItem();
}
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode ==
KeyEvent.KEYCODE_DPAD_CENTER) { this.addItem();
}
return false;
}
}
With reference to this question, use this code.
EDIT: Added hashmap definition
LinearLayoutwith a couple ofTextViewsR.layout.yourlayoutnamein this lineSimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);The good thing about this approach is that it avoids you having to create any new objects, and it doesn’t involve much code.