java 无法在我的 ListView 上的自定义 ArrayAdapter 中获取所选项目索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16690535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:40:31  来源:igfitidea点击:

Can't get Selected Item Index in custom ArrayAdapter on my ListView

javaandroidselecteditem

提问by boski

I have problem with my Android project, because I can't get selected item index from my List with my own ArrayAdapter. I've tried few examples from tutorials but they don't work. What is the solution?

我的 Android 项目有问题,因为我无法使用自己的 ArrayAdapter 从我的列表中获取所选项目索引。我已经尝试了一些教程中的示例,但它们不起作用。解决办法是什么?

Adapter

适配器

public class myProductAdapter extends ArrayAdapter<myProductsGroup> {

    private List<myProductsGroup> productList;
    private Context context;

    public myProductAdapter(List<myProductsGroup> productList, Context ctx) {
        super(ctx, R.layout.list_products_row, productList);
        this.productList = productList;
        this.context = ctx;
    }

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

    /*
    public void onClick(View v) {
        int position = Integer.parseInt((String) v.getTag());

        OrderFirstGridPage.setSelectedItem(position);             
    }
    */

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_products_row, parent, false);
        }
            TextView tv = (TextView) convertView.findViewById(R.id.title);
            tv.setTag(""+position);
            TextView distView = (TextView) convertView.findViewById(R.id.description);
            distView.setTag(""+position);
            tv.setText("aa");
        return convertView;
    }
}

In activity

活动中

lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new myProductAdapter(setupArrayProductList((ArrayList<myProduct>) ProductList), OrderFirstGridPage.this));
lv.setSelector(R.drawable.selector_for_position_list);

回答by androidcodehunter

Try to use this line of code:

尝试使用这行代码:

    lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(new myProductAdapter(setupArrayProductList((ArrayList<myProduct>) ProductList), OrderFirstGridPage.this));
    lv.setSelector(R.drawable.selector_for_position_list);      

 lv.setOnItemClickListener(new OnItemClickListener() {

                                            @Override
                                            public void onItemClick(AdapterView<?> parent,
                                                    View view, int position, long id) {
                                                Log.d("My POSITION",""+position);

                                            }
                                        });

Hope you will get the exact position from selected listview. If you get any problem, please inform me. Hope this will works.

希望您能从选定的列表视图中获得准确的位置。如果您有任何问题,请通知我。希望这会奏效。

回答by user2409032

You need to implement AdapterView.OnItemClickListener: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

你需要实现AdapterView.OnItemClickListener:http: //developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

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

There, you'll get the index of each item.

在那里,您将获得每个项目的索引。

You just have to set that listener to your list:

您只需将该侦听器设置为您的列表:

lv.setOnItemClickListener(yourListener);