1

似乎无法找到解决此问题的方法。我正在尝试编辑我的列表视图项目中的数据。列表视图使用数组适配器在列表视图中以一行的形式显示数据。

用户将手指放在他们希望编辑的列表视图项目上,这会导致弹出一个框,其中包含“删除”或“编辑”选项。当用户单击编辑数据时,该列表视图项中的数据应该被传输到用户可以编辑它的其他活动。不是传输我要编辑的数据,而是传输列表视图中最后一项的数据。

这是下面的代码。

    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;

这是我数据库中的数据

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

这就是使用从数据库导入的数据创建变量的方式。

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

这就是数组列表项的制作方式,用于在我的列表视图中显示数据。

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

如果有人能告诉我如何让它工作,或者至少告诉我哪里出了问题以及我应该尝试什么,我将不胜感激。感谢任何试图帮助我的人。

4

1 回答 1

0

我认为您可以使用 OnItemClickListener 而不是使用 onContextItemSelected 打开新活动。你可以这样声明它:

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
    }
});
于 2013-09-07T01:27:49.403 回答