gpt4 book ai didi

android - 如何从 ArrayAdapter 类的 ListView 获取索引和字符串

转载 作者:行者123 更新时间:2023-11-30 01:53:42 24 4
gpt4 key购买 nike

我正在创建网络服务应用程序。它具有将 Web 数据呈现给 Listview 的 ArrayAdapter 类。我想获取所选 Listview 项目的索引并将索引值发送到下一个 Activity 以执行其他任务。但我无法从 ListView 获取索引。 ListView id 是默认的 Android API id

我几乎使用了所有东西,比如 setOnItemSelectedListenersetOnClickListener OnItemSelectedListener 但没有用

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
}

private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");

String url = "xxxxxxxx";
// json class doing background data processing
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}

@Override
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
lv.setAdapter(adapter);

}

这是 ArrayAdapter 类

public class ApplicationAdapter extends ArrayAdapter<Application>  implements OnItemClickListener{
private List<Application> items;
ListView listview;
public ApplicationAdapter(Context context, List<Application> items) {
super(context, R.layout.app_custom_list, items);
this.items = items;

MainActivity.lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id)
{
String text = ((TextView)childView.getTag()).toString();
Log.d("index",text);
}
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
}

@Override
public int getCount() {
return items.size();
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;

if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.app_custom_list, null);
}

Application app = items.get(position);

if(app != null) {
ImageView icon = (ImageView)v.findViewById(R.id.appIcon);



for(int i=1; i<=5; i++) {
ImageView iv = new ImageView(getContext());

if(i <= app.getRating()) {
iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_checked));
}
else {
iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_unchecked));
}

ratingCntr.addView(iv);
}
}
}

return v;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String[] lv_arr = null;
// TODO Auto-generated method stub
String itemname = lv_arr[position];
Log.d("string from ListView",itemname);


}
}

请帮助我获取 ListView 的所选项目的索引。如果有人可以建议我应该使用什么,我将不胜感激

提前致谢

最佳答案

protected void onCreate(Bundle savedInstanceState) {的底部添加

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//get position index of item here.

String indexid = String.valueOf(position);

//and do whatever afterwards.


});

}

并删除 public void onItemClick(AdapterView<?> parent, View view, int position, .......在你的适配器中

关于android - 如何从 ArrayAdapter 类的 ListView 获取索引和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com