2015-10-19 108 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

相關問題