Editing data in a List View item

强颜欢笑 提交于 2020-01-15 10:45:49

问题


Can't seem to find a solution to this. I'm trying to edit the data in my List View item. The List View uses an array adapter to display the data in a row in the list view.

The user holds their finger over the list view item they wish to edit which causes a box to pop up containing the option of either 'Delete' or 'Edit'. When the user clicks to Edit the data, the data from that list view item is supposed to be transferred over to the other activity where the user can edit it. Instead of transferring the data that I want to edit, the data from the last item in the List View is transferred.

This is the code below.

    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = info.position;
        switch (item.getItemId()) {
            case R.id.edit:
            {
                itemCheck = true;


                String bCheck = String.valueOf(itemCheck);

                iTitle = paymentTitle.get(position).toString();
                iPaymentDate = paymentDate.get(position).toString();
                iReminderDate = reminderDate.get(position).toString();
                iReminderTime = reminderTime.get(position).toString();
                iAmount = paymentVal.get(position).toString();



                if(iReminderDate == "No Date Set")
                {
                    iReminderDate = "";


                }
                else
                {
                    Intent reminder = new Intent(this, MyBroadcastReceiver.class);

                    PendingIntent.getBroadcast(this.getApplicationContext(), 1, reminder,
                            PendingIntent.FLAG_UPDATE_CURRENT).cancel();
                }


                if(iReminderTime == "No Time Set")
                {
                    iReminderTime = "";
                }


                Intent intent = new Intent(Payments.this, AddPayment.class);
                intent.putExtra("PID", pID);
                intent.putExtra("ITITLE", pTitle);
                intent.putExtra("PAYMENTDAY", pDay);
                intent.putExtra("PAYMENTMONTH", pMonth);
                intent.putExtra("PAYMENTYEAR", pYear);
                intent.putExtra("REMINDERDAY", rDay);
                intent.putExtra("REMINDERMONTH", rMonth);
                intent.putExtra("REMINDERYEAR", rYear);
                intent.putExtra("REMINDERHOUR", rHour);
                intent.putExtra("REMINDERMIN", rMin);
                intent.putExtra("IAMOUNT", pValue);
                intent.putExtra("ITEMCHECK", itemCheck);
                startActivity(intent);
                finish();
            }

                return true;

This is the data from my database

C = readableDB.query("PaymentTable", new String[] { "_ID", "PTITLE", "PA", "PDAY", "PMONTH", "PYEAR", "RDAY", "RMONTH",
                "RYEAR", "RMIN", "RHOUR", "DATE"}, null, null, null, null, "DATE ASC");

This is how the variables is created using the data imported from the Database.

            paymentID = C.getInt(C.getColumnIndex("_ID"));
            pTitle = C.getString(C.getColumnIndex("PTITLE"));
            pDay = C.getString(C.getColumnIndex("PDAY"));
            pMonth = C.getString(C.getColumnIndex("PMONTH"));
            pYear = C.getString(C.getColumnIndex("PYEAR"));
            rDay = C.getString(C.getColumnIndex("RDAY"));
            rMonth = C.getString(C.getColumnIndex("RMONTH"));
            rYear = C.getString(C.getColumnIndex("RYEAR"));
            rHour = C.getString(C.getColumnIndex("RHOUR"));
            rMin = C.getString(C.getColumnIndex("RMIN"));
            pValue = C.getString(C.getColumnIndex("PA"));

            pID = paymentID.toString();

This is how the array list items are made which are used to display the data in my list view.

            reminderDateStr = rDay + "/" + rMonth + "/" + rYear;
            reminderTimeStr = rHour + ":" + rMin;
            paymentDateStr = pDay + "/" + pMonth + "/" + pYear;
            paymentTitleStr = pTitle;
            paymentValue = "£" + pValue;

            paymentTitle.add(paymentTitleStr);
            reminderTime.add(reminderTimeStr);
            paymentDate.add(paymentDateStr);
            reminderDate.add(reminderDateStr);
            paymentVal.add(paymentValue);

Would really appreciate it if someone could tell me how to get this working or at least tell me where I've went wrong and what I should try instead. Thanks to anyone who tries to help me with this.


回答1:


Instead of using onContextItemSelected to open the new activity, I think you can use OnItemClickListener for that. You can declare it this ways:

yourListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        ListView lv = (ListView)parent;
        @SuppressWarnings("unchecked")
        ArrayAdapter<YourObject> adapter = ArrayAdapter<YourObject>)
        lv.getAdapter();
        YourObjectitem = adapter.getItem(position);

        // The rest of the code goes here
    }
});


来源:https://stackoverflow.com/questions/18668421/editing-data-in-a-list-view-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!