编辑列表视图项中的数据

时间:2013-09-07 00:21:23

标签: android android-sqlite

似乎无法找到解决方案。我正在尝试编辑List View项目中的数据。列表视图使用数组适配器在列表视图中显示行中的数据。

用户将手指放在他们想要编辑的列表视图项上,这会导致弹出一个框,其中包含“删除”或“编辑”选项。当用户单击“编辑数据”时,该列表视图项中的数据应该转移到用户可以编辑的其他活动。

将传输列表视图中最后一项的数据,而不是传输我想要编辑的数据

这是下面的代码。

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

如果有人能告诉我如何让这个工作或者至少告诉我哪里出错了以及我应该尝试做什么,我真的很感激。感谢任何试图帮助我的人。

1 个答案:

答案 0 :(得分: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
    }
});