**您好,我正在尝试从已填充的Arraylist / ArrayAdapter中检索第二个值。我是Array的新手,所以如果我错了请纠正我
Q1。我创建了Array Favorite。我认为我创建的是一个带有两组值调用的数组|值。示例Detail =“Yasmin”,Value =“8”。这是对的吗?
Q2。我已将收藏夹数组分配给mFavlist列表视图。在OnItemClick期间,我可以通过listview的位置返回标签“Yasmin”。我想做的是返回值“8”。最好的方法是什么?
如果我通过阵列和适配器**在正确的轨道上,请告诉我。
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 Favorite("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;
}
}
}
答案 0 :(得分:0)
使用position
参数到达适配器中的正确位置
public void onItemClick(AdapterView arg0, View view,
int position, long id) {
Favorite selectedFav=lister.this.fakeFavs.getItem(position)
}