在列表视图项编辑数据 [英] Editing data in a List View item

查看:134
本文介绍了在列表视图项编辑数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎无法找到一个解决的办法。我想在我的列表视图项编辑数据。列表视图使用数组适配器列表视图中的一行中显示的数据。

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.

这是低于code。

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

真的AP preciate,如果有人可以告诉我如何得到这个工作,或至少告诉我在哪里,我去错了,我应该试试吧。由于任何人谁试图帮助我。

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.

推荐答案

而不是使用onContextItemSelected打开新的活动,我想你可以使用OnItemClickListener了点。你可以声明它这个方式:

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

这篇关于在列表视图项编辑数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆