2015-10-19 137 views
1

大家好日子。我想从数据库中删除一些项目后刷新ListView,但我看到remove(无法解析)下方的红线。我有以下代码。删除一些项目后ListView刷新

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

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

+0

问题解决了吗? –

+0

@AnsalAli不... – Hoo

+0

任何人都可以帮忙吗? – Hoo

回答

2

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

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

什么应该在fetchAllRoutines里面? – Hoo

+0

按照您创建'SimpleCursorAdapter'时的相同方式创建它。 (更新我的文章) – janos

+0

sqlcon.delete()后创建? – Hoo

1

这就是我所做的删除列表行尝试这样的事情在你的适配器类

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

    } 
}); 
+0

你在哪里得到CartList? – Hoo

+0

我的java类名称 – Android

+0

上下文无法解析,我更改为getApplicationContext。但仍然没有运气.. – Hoo

1

我看到一个红色的线下删除(无法解析)

发生这种情况是因为类AdapterView没有名为remove的方法。 也请参阅这些linkthis

做代码删除选定的编号排在你的编辑,

 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蹦出来,所有的文字变成白色。为什么会出现这种情况?

我不知道为什么会发生,但这个片段可以帮助你摆脱问题

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

这将迫使背景颜色将被倒置的。此标志仅用于前材质主题。 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.

+0

看到我编辑的帖子 – Hoo

+0

检查此链接http://stackoverflow.com/questions/14482092/alertdialog-background-color –

+0

注意到,谢谢 – Hoo