returning something other than the actual label in a list view
If I have a listview which is populated with a string array say:
Mazda
Renault
Holden
Audi
and when开发者_如何学C I want to click on an item, rather than returning string "Mazda" I'd rather return an associated number e.g. Mazda Returns 0, Renault returns 1 etc.
Is this possible and would I see a significant performance increase on querying an SQLite DB using just ints rather than strings?
Thanks, M
in your listview there are adapter are use, so in this method there are position variable also already exist there.if you print it in logcat
it will get every time position of your list-item.you need to handle it in array easily
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}
You could do something like the following:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] MY_CARS = new STRING[] { Mazda, Renault, Holden, Audi };
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, MY_CARS));
ListView lv = (ListView)findViewById(R.id.MyListView);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Item at Position "+pos+" was clicked. This corresponds to "+MY_CARS[pos],
Toast.LENGTH_SHORT).show();
}
});
}
This should return the position of the item in the list that was clicked, which should be the index of the array.
I am not sure whether you will get a significant increase in db performance though.
精彩评论