Java – unable to get the selected item index in the custom arrayadapter on the listview

There is a problem with my android project because I can't get the selected project index from my list using my own arrayadapter I tried some examples from the tutorial, but they didn't work What is the solution?

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);

Solution

Try the following 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);

                                            }
                                        });

We want you to get the exact location from the selected list view If you have any questions, please let me know I hope it will work

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>