**I am trying to retrieve the 2nd value from a Arraylist/ArrayAdapter that I have populated. I am new to Array so please correct me if I am wrong
Q1. I created the Array Favorite. What I think what I created is an Array with two set of value call Detail | Value. example Detail="Yasmin",Value="8". Is this correct?
Q2. I have assign the Favorite Array to the mFavlist listview. During the OnItemClick I can return the label "Yasmin" by the position of the listview. What I would like to do is return the value of "8". What would be the best way to do this?
Please let me know if I am on the right track via the array and adapter**
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class lister extends Activity {
/** Called when the activity is first created. */
TextView txHomeTeam;
protected ListView mFavlist;
protected ArrayList<Favorite> fakeFavs = new ArrayList<Favorite>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.main);
this.txHomeTeam = (TextView)this.findViewById(R.id.title);
fakeFavs.add(new Favorite("John", "1"));
fakeFavs.add(new Favorite("Yasmin", "8"));
fakeFavs.add(new Favori开发者_如何学编程te("Jack", "10"));
//this.mFavlist = (ListView) this.findViewById(R.id.list_favorites);
this.mFavlist = (ListView) this.findViewById(R.id.list_favorites);
initListView();
mFavlist.setTextFilterEnabled(true);
mFavlist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
// user clicked a list item,
//and read the value from <Favorite>.value
//txHomeTeam.setText=????
}
});
}
public void refreshFavListItems() {
mFavlist.setAdapter(new ArrayAdapter<Favorite>(this,
android.R.layout.simple_list_item_1, fakeFavs));
}
public void initListView() {
/* Loads the items to the ListView. */
refreshFavListItems();
}
protected class Favorite {
protected String Detail;
protected String value;
protected Favorite(String Detail, String value) {
this.Detail = Detail;
this.value = value;
}
public String toString() {
return Detail;
}
}
}
Use the position
parameter to get to the correct position in your adapter
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
Favorite selectedFav=lister.this.fakeFavs.getItem(position)
}
精彩评论