自定义ListView适配器上的OnItemClickListener返回错误的数据

时间:2019-04-06 22:53:17

标签: android listview android-arrayadapter onitemclicklistener

我有一个带有搜索栏的自定义列表视图,可以很好地工作。现在,我创建了一个OnItemClickListener,以便当我在Listview中单击返回的搜索项时,它会打开一个新Activity,并在新Activity中显示所选项目。但是,当我单击Listview时返回的值是错误的,并且我已经使用了3天以上了,即使检查示例也似乎找不到问题。下面是我的代码。我会向正确的方向提供帮助

这是我的MainActivity中的setOnItemClickListener

 mListViewTakeAwayPacks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                    String takeAwayPack = adapterView.getItemAtPosition(position).toString();


                    Toast.makeText(ListTakeAwayPackActivity.this, "Selected: " + takeAwayPack, Toast.LENGTH_SHORT).show();

                    Cursor takeAwayPackData = controller.getTakeAwayPAckID(takeAwayPack);  //get the ID associatd with the name




                    while (takeAwayPackData.moveToNext()){

                     }
                        Intent editTakeAwayPackIntent = new Intent(ListTakeAwayPackActivity.this, EditTakeAwayPackActivity.class);
                        editTakeAwayPackIntent.putExtra("PackType", takeAwayPack);
                        startActivity(editTakeAwayPackIntent);

                }
            });

这是我的自定义适配器

public class TwoColumn_ListAdaper extends BaseAdapter implements Filterable {
   private ArrayList<User3> mOriginalValues; // Original Values
    private ArrayList<User3> mDisplayedValues;    // Values to be displayed
    LayoutInflater inflater;
   public TwoColumn_ListAdaper(Context context, int list_take_away_pack_adapter_view, ArrayList<User3> mProductArrayList){
       this.mOriginalValues = mProductArrayList;
        this.mDisplayedValues = mProductArrayList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDisplayedValues.size();
    }
    @Override
    public Object getItem(int position) {
        return mDisplayedValues.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    private class ViewHolder {   // holds the views from the list_take_away_pack_ada[ter_view.xml, from the Textview and the parent Linear layout
        LinearLayout llContainer;
        TextView takeawaypacktype,takeawaypackprice;
    }
    public View getView(int position, View convertView, ViewGroup parents){
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_take_away_pack_adapter_view, null);
            holder.llContainer = (LinearLayout)convertView.findViewById(R.id.llContainer);
            holder.takeawaypacktype = (TextView) convertView.findViewById(R.id.takeawaypacktype);
            holder.takeawaypackprice = (TextView) convertView.findViewById(R.id.takeawaypackprice);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }      holder.takeawaypacktype.setText(mDisplayedValues.get(position).getmTakeAwayPackType());      holder.takeawaypackprice.setText(mDisplayedValues.get(position).getmTakeAwayPackCost()+"");
        holder.llContainer.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            }
        });
        convertView.setClickable(false);    //add a single line of code like this: convertView.setClickable(false) because the OnItemCLickListener not working in listview.    found here https://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview
        return convertView;
    }

这是日志文件

2019-04-08 13:02:43.068 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@47d85e8 time:409548988
2019-04-08 13:02:45.921 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/ViewRootImpl: ViewPostImeInputStage processPointer 0
2019-04-08 13:02:46.071 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/ViewRootImpl: ViewPostImeInputStage processPointer 1
2019-04-08 13:02:46.081 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/TextView: setTypeface with style : 0
2019-04-08 13:02:46.081 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement I/Timeline: Timeline: Activity_launch_request id:com.example.noruw.foodoclockbusinessprocessmanagement time:409552007
2019-04-08 13:02:46.101 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/ViewRootImpl: #1 mView = android.widget.LinearLayout{98564ec V.E...... ......I. 0,0-0,0}
2019-04-08 13:02:46.171 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
2019-04-08 13:02:46.201 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/TextView: setTypeface with style : 0
2019-04-08 13:02:46.201 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/TextView: setTypeface with style : 0
2019-04-08 13:02:46.211 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/TextView: setTypeface with style : 0
2019-04-08 13:02:46.211 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/TextView: setTypeface with style : 0
2019-04-08 13:02:46.221 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/TextView: setTypeface with style : 0
2019-04-08 13:02:46.221 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/TextView: setTypeface with style : 0
2019-04-08 13:02:46.231 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/SecWifiDisplayUtil: Metadata value : SecSettings2
2019-04-08 13:02:46.231 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{b02ba23 I.E...... R.....ID 0,0-0,0}
2019-04-08 13:02:46.301 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 36 - 0, 0) vi=Rect(0, 36 - 0, 0) or=1
2019-04-08 13:02:46.361 27225-27286/com.example.noruw.foodoclockbusinessprocessmanagement D/OpenGLRenderer: endAllStagingAnimators on 0xa122f180 (ListView) with handle 0x9e18b930
2019-04-08 13:02:46.371 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2b43752 time:409552294
2019-04-08 13:02:46.581 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement V/ActivityThread: updateVisibility : ActivityRecord{5a8d6c0 token=android.os.BinderProxy@47d85e8 {com.example.noruw.foodoclockbusinessprocessmanagement/com.example.noruw.foodoclockbusinessprocessmanagement.Take_Away_Pack_Management.ListTakeAwayPackActivity}} show : false
2019-04-08 13:02:48.093 27225-27225/com.example.noruw.foodoclockbusinessprocessmanagement D/ViewRootImpl: #3 mView = null

0 个答案:

没有答案