简体   繁体   English

单击ListView项目不起作用,我每个ListView项目中都有三个textview和一个按钮

[英]Click on ListView Item not working, I have three textviews and a button in my each ListView Item

I am writing an app in which i am trying to open an activity and update item detail using Tap on Item in a ListView 我正在编写一个应用,其中我试图使用ListView中的“轻按项目” 来打开活动并更新项目详细信息

Problem: 问题:

whenever i do click on item in a ListView, not able to start that activity and i also want to know how to update item detail... 每当我单击ListView中的项目时,都无法启动该活动,我也想知道如何更新项目详细信息...

I have three textviews and a button in my each ListView Item 我的每个ListView项目中都有三个textviews和一个按钮

By using below code in CartActivity, i am not able to open ProductInformationActivity 通过在CartActivity中使用以下代码,我无法打开ProductInformationActivity

    ListView mLstView1 = (ListView) findViewById(R.id.listView1);
    CartAdapter mViewCartAdpt = new CartAdapter(CartActivity.this);
mLstView1.setAdapter(mViewCartAdpt);
    mLstView1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            Intent intent = new Intent(CartActivity.this, com.ProductInformationActivity.class);
            startActivity(intent);
        }
    });

CartAdapter.java: CartAdapter.java:

  public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    HashMap<String, String> item = new HashMap<String, String>();
    item = Constants.mItem_Detail.get(position);

    // Setting all values in listview

    title.setText(item.get(com.ProductInformationActivity.KEY_TITLE));
    qty.setText(item.get(com.ProductInformationActivity.KEY_QTY));
    cost.setText(item.get(com.ProductInformationActivity.KEY_COST));
    total.setText(item.get(com.ProductInformationActivity.KEY_TOTAL));

    return vi;

}   

Have you added your Ativity to your manifest File? 您是否已将Ativity添加到清单文件中? You need to add your activity in the application tag: 您需要在应用程序标记中添加活动:

<activity android:name="com.erachnida.restaurant.versionoct.menu.ProductInformationActivity" />

I think that your problem is that you are assigned OnItemClickListener for whole ListView and then for ImageView. 我认为您的问题是为整个ListView然后为ImageView分配了OnItemClickListener Also about ImageView, look: 同样关于ImageView,请看:

Intent mInViewCartRefresh = new Intent(activity, CartActivity.class);
activity.startActivity(mInViewCartRefresh);
activity.finish();

Here you are probably trying to "refresh" ListView but this is wrong. 在这里您可能正在尝试“刷新” ListView,但这是错误的。 If you want to update your ListView all what you need is to use 如果要更新ListView,则只需使用

adapter.notifyDataSetChanged();

that indicates that: "I changed datasource, i want from your refresh yourself." 表示: “我更改了数据源,希望您刷新自己。”

So my suggestion is to remove your "inner" listener, remove your snippet of code mentioned above and leave only your listener for ListView. 因此,我的建议是删除“内部”侦听器,删除上述代码段,仅将侦听器留给ListView。

Intent intent = new Intent(CartActivity.this, ProductInformationActivity.class);
startActivity(intent);

Then if you click on ListView, from int position parameter you can get particular item for collection and you can do whatever you want with it. 然后,如果您单击ListView,则可以从int position参数中获取要收集的特定项目,并且可以使用它进行任何操作。 After "action" you need to call notifyDataSetChanged() that refresh your ListView. 在“操作”之后,您需要调用notifyDataSetChanged()来刷新您的ListView。

Asper our conversation you have check this link . 在我们进行对话时,您已经检查了此链接 so, in itemClickListener put this lines: 因此,在itemClickListener放置以下行:

Intent intent = new Intent(CartActivity.this, ProductInformationActivity.class);
                startActivity(intent);

just like below: 就像下面这样:

public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
            public void onClick(View v) {
                final int position = list_v
                        .getPositionForView((View) v.getParent());

                Log.d("you are click on Ratings","you are click on Ratings");
                Intent intent = new Intent(CartActivity.this, ProductInformationActivity.class);
                startActivity(intent);

            }
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM