单击listview按钮不会获​​取该列表视图项

时间:2016-04-20 05:32:35

标签: android listview

我有listview的每个项目的按钮。单击该按钮时,必须获取相应列表视图项的值。

以下是我的getView方法。

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

        view = convertView;

        if (convertView == null) {
            //inflate the view for each row of listview
            if (imageLoader == null)
                imageLoader = AppController.getInstance().getImageLoader();
            view = inflator.inflate(R.layout.pending_orders_fragment, null);
            vholder = new ViewHolder();
            vholder.mShippngRefNo = (TextView) view.findViewById(R.id.shipment_ref_no_value);
            vholder.contact = (ImageButton) view.findViewById(R.id.imageButton);
            vholder.mShippingStatus.setTag(position);

            view.setTag(vholder);
        } else
            vholder = (ViewHolder) view.getTag();

        PendingOrdersDao item = listforview.get(position);


        vholder.mShippngRefNo.setText(item.getSipmentNo());
        vholder.contact.setOnClickListener(this.onClickListener);

        return view;
    }

    public void setOnButtonClickListener(final View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

我在我的代码中按如下方式调用按钮..

adapter.setOnButtonClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            LinearLayout relativeLayout = (LinearLayout) v.getParent();
                                            TextView textView = (TextView) relativeLayout.findViewById(R.id.shipment_ref_no_value);
                                            Toast.makeText(getActivity(),textView.getText().toString(),Toast.LENGTH_SHORT).show();

                                        }
                                    });

但是这样做我得到空指针异常。我怎么能解决这个问题呢?

以下是我的logcat

  

显示java.lang.NullPointerException                                                                    在com.vaighai.fragments.MyOrdersFragment $ 2 $ 1.onClick(MyOrdersFragment.java:253)                                                                    在android.view.View.performClick(View.java:4475)                                                                    在android.view.View $ PerformClick.run(View.java:18786)                                                                    在android.os.Handler.handleCallback(Handler.java:730)                                                                    在android.os.Handler.dispatchMessage(Handler.java:92)                                                                    在android.os.Looper.loop(Looper.java:176)

更新的LOGCAT:

致命的例外:主要                                                            显示java.lang.NullPointerException                                                                在

  

com.vaighai.adapters.CustomAdapter $ 1.onClick(CustomAdapter.java:122)                                                                  在android.view.View.performClick(View.java:4475)                                                                  在android.view.View $ PerformClick.run(View.java:18786)                                                                  在android.os.Handler.handleCallback(Handler.java:730)                                                                  在android.os.Handler.dispatchMessage(Handler.java:92)                                                                  在android.os.Looper.loop(Looper.java:176)                                                                  在android.app.ActivityThread.main(ActivityThread.java:5419)                                                                  at java.lang.reflect.Method.invokeNative(Native Method)                                                                  在java.lang.reflect.Method.invoke(Method.java:525)                                                                  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)                                                                  在dalvik.system.NativeStart.main(本地方法)

3 个答案:

答案 0 :(得分:2)

您可以直接在代码中设置setOnClickListener .so remove

vholder.contact.setOnClickListener(this.onClickListener);

添加以下代码

    vholder.contact.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        LinearLayout relativeLayout = (LinearLayout) v.getParent();
        TextView textView = (TextView) relativeLayout.findViewById(R.id.shipment_ref_no_value);
        Toast.makeText(context,textView.getText().toString(),Toast.LENGTH_SHORT).show();}
       });

答案 1 :(得分:1)

很可能你在活动中忘记了这一点

class MainAct extends Activity implements AdapterView.OnItemClickListener

答案 2 :(得分:1)

在你的Toast消息中写道:

Toast.makeText(getActivity(),listforview.get((int)vholder.mShippingStatus.getTag()).getSipmentNo(),Toast.LENGTH_SHORT).show();

在你的if和else里面的getView()方法之外写:

vholder.mShippngRefNo.setTag(position);