2011-12-29 20 views
8

この質問はこれに似ています - Android - Listview delete item and Refreshリストビュー削除アイテムとリフレッシュ - アンドロイド

私は私のアダプターをリフレッシュカント:

adapter.notifyDataSetChanged(); 

私が試した:

adapter.remove(adapter.getItem(pos)); 

が、成功せず、一度だけ(変なの...)。

別の答えはそこにある:

Call that Activity once again Using Intent 

sombodyは、私は、このための正確なコードを与える(またはアダプタ/カーソルのために)ことができますか?

私はこれを数時間試して成功しています。

私の完全なコード:

protected void onCreate (Bundle SavedInstanceState) { 


    super.onCreate(SavedInstanceState); 
    setContentView(R.layout.personalmessageview); 

    headtitle= getIntent().getExtras().getString("head"); 

    setTitle(headtitle); 


    personalresults = getIntent().getExtras().getStringArrayList("personalres"); 
    personalresultswithtime = getIntent().getExtras().getStringArrayList("personalrestime"); 



    // setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,personalresults)); 

    ListView list = (ListView)findViewById(R.id.listview_personal);  
    // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, personalresults); 
    list.setAdapter(adapter); 
    registerForContextMenu(list); 


    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) { 


       String time = personalresultswithtime.get(pos).toString(); 


        Show_Alert_box(v.getContext(),"Please select action.",time,pos); 


       return true; 
      } 
     }); 

公共ボイドShow_Alert_box(コンテキスト・コンテキスト、文字列メッセージ、文字列の時間、INT位置) 最終列タイムスタンプ=時間。

   final int pos = position; 

      final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 
       alertDialog.setTitle(getString(R.string.app_name)); 
       alertDialog.setButton("Delete", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 

         try 
         { 
          db = databaseHelper.getWritableDatabase(); 


          db.delete("messages","timestamp" + "=?", new String[] { timestamp }); 

          Log.d("DB"," delete! "); 

          ArrayAdapter<String> adapter = new ArrayAdapter<String>(PersonalMessageView.this, android.R.layout.simple_list_item_1, personalresults); 


            adapter.remove(adapter.getItem(pos)); //not working t all! why ? 

          list.notify(); 
          list.invalidate(); 


          personalresults.remove(pos); 
          personalresultswithtime.remove(pos); 

          adapter.notifyDataSetChanged(); 

          db.close(); 




         } 
         catch(Exception e) 
         { 

         } 
       } }); 
       alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         alertDialog.dismiss(); 
       } }); 

       alertDialog.setMessage(message); 
       alertDialog.show(); 

}

+0

は、アダプタから項目を削除してから日付のセットを通知してください、あなたのアダプタのために何を使用している –

+0

をchaged?つまり、ArrayAdapterまたはBaseAdapter、またはそれらのいずれかから派生したクラスですか? –

+0

iv'eがコードを追加しました。もう一度それを確認してください。 – Smoker09

答えて

3

データがListViewコントロールとちょうど呼び出しを使用して、アダプターをバインドする

getListView().invalidate(); 
+0

動作していません...私のコードを確認してください。 – Smoker09

2

メイク機能を変更した場合、次の行を使用したデータの変更後、あなたのListActivity内のリストビューを無効に削除が完了している間にその機能を再度実行して、Listviewが再び満たされ、更新されたリストを取得します。
データベースを使用していますか?

+0

はい私はdbを使用しています。私の追加したコードをチェックしてください。タンク! – Smoker09

2

listview.invalidateViews();を使用してみてくださいadapter.remove(adapter.getItem(pos));

+0

はまだ動作しません。タンク。 – Smoker09

6

後にあなたは、あなたが、その後 (再クエリまたはそれは非推奨だからのようなもので)あなたのArrayListを更新し、あなたのDBの変更を更新する必要が

adapter.remove(adapter.getItem(pos)); 
notifyDataSetChanged(); 
+0

私のコードを確認してください:) >>> – Smoker09

+0

私のアダプターには削除機能がありません –

0
public class Listview_cls extends Activity implements OnItemClickListener{ 
      ListView lv; 
      String items[]= {"Kolkata","Delhi","Mumbai","Pune"}; 
      ArrayAdapter adp; 
      public void onCreate(Bundle savedInstanceState) 
      { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      arr = new ArrayList(); 
      lv = (ListView) findViewById (R.id.ListView01); 
      for(int i=0; i<items.length; i++) 
      { 
       arr.add(items[i]); 
      } 
     adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr); 
     lv.setAdapter(adp); 
     lv.setOnItemClickListener(this); 
    } 


    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
    {  

    adp.remove(adp.getItem(arg2)); 
     adp.notifyDataSetChanged(); 

    } 
} 
2

試してみてくださいあなたはadapter.notifyDataSetChanged()を呼び出さなければなりません。

6

あなたはおそらく、それは解決しましたが、念のために他の誰が同じ問題を抱えている、ここに私のソリューションです:

ArrayAdapter<String> myAdapter = (ArrayAdapter<String>)getListView().getAdapter(); 
myAdapter.remove(myAdapter.getItem(info.position)); 
myAdapter.notifyDataSetChanged(); 

問題はあなたのリストのアダプタを持っていなかったということでした。

+0

私のために働きました。ありがとう。 – Si8

0
@Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();  
     int position = info.position; 
     favouriteReportList d; 
     d= array_list.get(position); 
     switch (item.getItemId()) { 
      case R.id.connect:    
       return true; 
      case R.id.mark: 
       return true; 
      case R.id.delete:   
       myFavReport = new DBhandlerReport(this); 
       myFavReport.deleteMyFavReport(d.getReportName(), d.getPathName(), myUrl); 
       myFavReport.close(); 
// arrarAdapter is an object of your class. 
       arrayAdapter.remove(arrayAdapter.getItem(info.position)); 
       arrayAdapter.notifyDataSetChanged(); 
       return true;     
      default: 
       return super.onContextItemSelected(item); 
     } 
    } 
-1

この

Intent intent= getIntent(); 
      finish(); 
      startActivity(intent); 
関連する問題