除去一些项目后的ListView刷新 [英] ListView refresh after removing some items

查看:100
本文介绍了除去一些项目后的ListView刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好日子大家。我想在ListView刷新去除数据库中的一些物品之后,但在我看到一个红色的线下删除(无法解析)。我有低于code。

Good day everyone. I want the ListView refresh after removing some items from database but I'm seeing a red lines underneath remove (cannot be resolved). I am having below code.

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

            }

有人可以帮我找出这个问题?我是新来的Andr​​oid和不知道这是实施正确的方法是什么?谢谢

Can someone help me to figure out the problem? I'm new to android and not sure is this the correct way to implement?Thanks

编辑

好了,我不喜欢什么@Sanju建议,现在收到这个错误

Ok, So I did like what @Sanju suggested and now getting this error

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)

code编辑

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

                    }
                });

任何想法?

最新

最后添加到的ListView

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

似乎一切都很好。但是,当我长preSS行和 AlertDialog 蹦出来,所有的文字变成白色。为什么会出现这种情况?

Everything seems fine. But when I long press the row and the AlertDialog pop out, all the text become white .Why would this happen?

推荐答案

通过这个(未编译)行,你试图操纵列表视图:

With this (not compiling) line, you are trying to manipulate the list view:

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

这不是它的工作原理。不要试图直接操作视图。使用适配器到该视图提供数据的想法是,你处理数据,而不是视图,然后发出信号的东西已经改变。诀窍是知道如何发出信号。看来你的下一行正是这么做的:

That's not how it works. Don't try to manipulate the view directly. The idea of using an adapter to provide data for the view is that you manipulate the data, not the view, and then signal that something has changed. The trick is knowing how to signal. It seems your next line does exactly that:

dataAdapter.notifyDataSetChanged();

因此​​,删除第一行(有p.remove),它可能实际工作。

So delete the first line (with p.remove), and it might actually work.

如果这还不够的(虽然我认为它应该是),那么你可能需要交换的光标适配器一个新的,像这样的:

If that's not enough (though I think it should be) then you might need to swap the cursor in the adapter with a new one, like this:

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

在更新后的code:

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

这篇关于除去一些项目后的ListView刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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