1

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

            }

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

Edited

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 edited

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

                    }
                });

Any idea?

Latest

final added to columns, to and 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);

And did like this

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

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

Hoo
  • 1,806
  • 7
  • 33
  • 66
  • http://stackoverflow.com/questions/17230933/alertdialog-theme-how-to-change-item-text-color check this link too – Ansal Ali Oct 19 '15 at 09:21

3 Answers3

2

this is what i have done to delete list row try something like this in your adapter class

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

    }
});
Android
  • 535
  • 5
  • 16
2

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

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

In your updated 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);
    }
});
janos
  • 120,954
  • 29
  • 226
  • 236
  • what should have inside fetchAllRoutines? – Hoo Oct 19 '15 at 05:59
  • Create it the same way you did when you created `SimpleCursorAdapter`. (Updated my post) – janos Oct 19 '15 at 06:33
  • create after sqlcon.delete()? – Hoo Oct 19 '15 at 06:35
  • No, like you did in `BuildList`, with `sqlcon.readEntry(name1)`. You need to recreate the same query. – janos Oct 19 '15 at 06:37
  • yeah I know, but where should I put the Cursor cursor2 = ... // create the same way you did when you created SimpleCursorAdapter dataAdapter.changeCursor(cursor2);? – Hoo Oct 19 '15 at 06:39
  • if `dataAdapter.notifyDataSetChanged()` alone doesn't work, then do this instead – janos Oct 19 '15 at 06:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92687/discussion-between-hoo-and-janos). – Hoo Oct 19 '15 at 06:46
1

I'm seeing a red lines underneath remove (cannot be resolved)

this happens because the class AdapterViewdoes not have a method called remove. Refer these link and this too.

do code to delete row of the selected Id as in your edit,

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

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

I dont know why it happens but this snippet can help you to get rid of the problem

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

It will force the background color to be inverted. This flag is only used for pre-Material themes.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.

Community
  • 1
  • 1
Ansal Ali
  • 1,583
  • 1
  • 13
  • 30