【问题标题】:ListView refresh after removing some items删除某些项目后 ListView 刷新
【发布时间】:2015-10-19 04:26:27
【问题描述】:

大家好。我希望在从数据库中删除一些项目后刷新 ListView,但我在remove 下方看到一条红线(无法解决)。我有以下代码。

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listdisplay1);
        dbHelper = new MyDatabaseHelper(this);
        sqlcon = new InfoAPI(this);
        final String name1 = getIntent().getExtras().getString("name");
        BuildList(name1);

    }

    public void BuildList(String name) {
        final String name1 = name;
        sqlcon.open();
        Cursor cursor=sqlcon.readEntry(name1);

       String[] columns=new String[]{
               MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
       };

        int[] to=new int[]{
                R.id.weather,R.id.date,R.id.status,R.id.in,R.id.out
        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.listdispaly,
                cursor,
                columns,
                to,
                0);

        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this row in the database.
                String ID =
                        cursor.getString(cursor.getColumnIndexOrThrow("_id"));
                String date1 = cursor.getString(cursor.getColumnIndexOrThrow("Date"));
                Intent intent = new Intent(ListDisplay.this, UpdatePage.class);
                intent.putExtra("name1", name1);
                intent.putExtra("date1", date1);
                intent.putExtra("ID", ID);
                startActivity(intent);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
             //   final long id1=id;
                AlertDialog.Builder builder = new AlertDialog.Builder(ListDisplay.this);
                builder.setTitle("Delete");
                builder.setMessage("Are you sure you want to delete?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {

                        database = dbHelper.getWritableDatabase();
                        Cursor cursor = (Cursor) p.getItemAtPosition(po);

                        // Get the state's capital from this row in the database.
                        long ID =
                                cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                      sqlcon.delete(ID);
                       p.remove(p.getItemAtPosition(po));
                        dataAdapter.notifyDataSetChanged();



                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()

                        {
                            public void onClick(DialogInterface dialog, int ii) {
                                dialog.dismiss();
                            }
                        }

                );
                builder.show();
                return true;
            }
        });
    }

            }

有人可以帮我找出问题所在吗?我是 android 新手,不确定这是正确的实现方式吗?谢谢

已编辑

好的,所以我确实喜欢@Sanju 的建议,现在收到此错误

10-19 04:47:48.543    2094-2094/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 2094
    java.lang.ClassCastException: android.app.Application cannot be cast to com.example.project.project.ListDisplay
            at com.example.project.project.ListDisplay$2$1.onClick(ListDisplay.java:109)
            at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
            at android.os.Handler.dispatchMessage(Handler.java:102)

代码已编辑

  database = dbHelper.getWritableDatabase();
                        Cursor cursor = (Cursor) p.getItemAtPosition(po);

                        // Get the state's capital from this row in the database.
                        long ID =
                                cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                      sqlcon.delete(ID);
                        ((ListDisplay)getApplicationContext()).finish();
                        Intent intent = new Intent(getApplicationContext(), ListDisplay.class);
                        getApplicationContext().startActivity(intent);

                    }
                });

有什么想法吗?

最新

final 添加到 columnstolistView

   final String[] columns=new String[]{
           MyDatabaseHelper.Weather,MyDatabaseHelper.Date,MyDatabaseHelper.Status,MyDatabaseHelper.TimeIn_Info,MyDatabaseHelper.TimeOut_Info
   };

    final int[] to=new int[]{
            R.id.weather,R.id.date,R.id.status,R.id.in,R.id.out
    };

    // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
    dataAdapter = new SimpleCursorAdapter(
            this, R.layout.listdispaly,
            cursor,
            columns,
            to,
            0);

 final   ListView listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

确实是这样

 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
             //   final long id1=id;
                AlertDialog.Builder builder = new AlertDialog.Builder(ListDisplay.this);
                builder.setTitle("Delete");
                builder.setMessage("Are you sure you want to delete?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {

                        database = dbHelper.getWritableDatabase();
                        Cursor cursor = (Cursor) p.getItemAtPosition(po);

                        // Get the state's capital from this row in the database.
                        long ID =
                                cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                        sqlcon.delete(ID);

                    }
                });
                 Cursor cursor=sqlcon.readEntry(name1);
                dataAdapter = new SimpleCursorAdapter(
                        getApplicationContext(), R.layout.listdispaly,
                        cursor,
                        columns,
                        to, 0);
                //listView.setAdapter(null);
                listView.setAdapter(dataAdapter);

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()

                        {
                            public void onClick(DialogInterface dialog, int ii) {
                                dialog.dismiss();
                            }
                        }

                );
                builder.show();
                return true;
            }
        });

一切似乎都很好。但是当我长按该行并弹出AlertDialog时,所有文本都变成了白色。为什么会发生这种情况?

【问题讨论】:

标签: java android sqlite listview


【解决方案1】:

使用此(非编译)行,您正在尝试操作列表视图:

p.remove(p.getItemAtPosition(po));

这不是它的工作原理。不要试图直接操作视图。使用适配器为视图提供数据的想法是您操作数据,而不是视图,然后发出信号表明发生了变化。诀窍是知道如何发出信号。您的下一行似乎正是这样做的:

dataAdapter.notifyDataSetChanged();

所以删除第一行(使用 p.remove),它可能确实有效。

如果这还不够(尽管我认为应该如此),那么您可能需要将适配器中的光标换成新的,如下所示:

Cursor cursor2 = ... // create the same way you did when you created SimpleCursorAdapter
dataAdapter.changeCursor(cursor2);

在您更新的代码中:

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int ii) {

        database = dbHelper.getWritableDatabase();
        Cursor cursor = (Cursor) p.getItemAtPosition(po);

        // Get the state's capital from this row in the database.
        long ID =
                cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
        sqlcon.delete(ID);

        Cursor cursor2 = sqlcon.readEntry(name1);
        dataAdapter.changeCursor(cursor2);
    }
});

【讨论】:

  • fetchAllRoutines 里面应该有什么?
  • 以与创建 SimpleCursorAdapter 时相同的方式创建它。 (更新了我的帖子)
  • 在sqlcon.delete()之后创建?
  • 不,就像你在BuildList 中所做的那样,sqlcon.readEntry(name1)。您需要重新创建相同的查询。
  • 是的,我知道,但是我应该把 Cursor cursor2 = ... // 创建与创建 SimpleCursorAdapter dataAdapter.changeCursor(cursor2); 时相同的方式?
【解决方案2】:

这是我为删除列表行所做的,在您的适配器类中尝试这样的操作

delet.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
        cartdata.updateSelected(Integer.parseInt(cart_pdiscription[i]), 0);
        cartdata.deleteProduct(Integer.parseInt(cart_pdiscription[i]));                
        Intent intent = ((CartList) context).getIntent();
        ((CartList) context).finish();
        context.startActivity(intent);

    }
});

【讨论】:

  • 从哪里获得 CartList?
  • context 无法解析,我更改为 getApplicationContext 。但仍然没有运气..
【解决方案3】:

我看到下面有一条红线删除(无法解决)

这是因为类 AdapterView 没有名为 remove 的方法。 也请参考这些linkthis

执行代码以删除所选 ID 的行,就像在您的编辑中一样,

     database =   dbHelper.getWritableDatabase();
                            Cursor cursor = (Cursor) p.getItemAtPosition(po);

                            // Get the state's capital from this row in the database.
                            long ID =
                                    cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                          sqlcon.delete(ID);                          

                        }
                    });
and then try this snippet

    cursor=sqlcon.readEntry(name1);
            dataAdapter = new SimpleCursorAdapter(
                    this, R.layout.listdispaly,
                    cursor,
                    columns,
                    to, 0);
    //listView.setAdapter(null);
    listView.setAdapter(dataAdapter);

但是当我长按该行并弹出AlertDialog时,所有文本都变成了白色。为什么会发生这种情况?

我不知道为什么会这样,但是这个 sn-p 可以帮助你摆脱这个问题

builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();

它将强制反转背景颜色。此标志仅用于 pre-Material 主题。This method was deprecated in API level 23. This flag is only used for pre-Material themes. Instead, specify the window background using on the alert dialog theme.

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
相关资源
最近更新 更多