2016-10-10 90 views
0

美好的一天哪裏把notifydatasetchanged()放在我的項目中

我有問題,當我試圖從我的sqlite中刪除我的項目。
它不會更新我的列表視圖。

主要活動
這是我的主要活動,無論是和的onCreate的onResume

private CustomAdapter customAdapter; 

@Override 
protected voidonCreate(Bundle savedInstanceState) { 
     lv = (ListView)findViewById(R.id.memo_list_view); 
     customAdapter = new CustomAdapter(this, memoIDS, memoTitles, memoCreatedDates, memoAlarmedDates); 
} 

@Override 
protected voidonResume() { 
memoIDS.clear(); 
    memoTitles.clear(); 
    memoCreatedDates.clear(); 
    memoAlarmedDates.clear(); 

    Cursor c=SplashActivity.db.rawQuery("SELECT * FROM memos", null); 
    if(c.getCount()!=0) { 
     while(c.moveToNext()) { 
      memoIDS.add(c.getString(0)); 
      memoTitles.add(c.getString(1)); 
      memoCreatedDates.add(c.getString(3)); 
      memoAlarmedDates.add(c.getString(4)); 
     } 
     c.close(); 
     lv.setAdapter(customAdapter); 
    } 
} 

CustomAdapter
從這個customadater,我試圖刪除自己創建的記錄。

public class CustomAdapter extends BaseAdapter{ 
List<String> mMemoIds; 
List<String> mMemoTitle; 
List<String> mMemoCreatedDate; 
List<String> mMemoAlarmedDate; 
Context context; 

    private static LayoutInflater inflater=null; 
public CustomAdapter(MemoListActivity mainActivity, List<String> memoIds, List<String> memoTitle, List<String> memoCreatedDate, List<String> memoAlarmedDate) { 

    mMemoIds = memoIds; 
    mMemoTitle = memoTitle; 
    mMemoCreatedDate = memoCreatedDate; 
    mMemoAlarmedDate = memoAlarmedDate; 
    context = mainActivity; 

    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 
@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return mMemoIds.size(); 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public class Holder 
{ 
    TextView tViewMemoTitle; 
    TextView tViewMemoCreatedDate; 
    TextView tViewMemoAlarmedDate; 
} 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    Holder holder=new Holder(); 
    View rowView;  
    rowView = inflater.inflate(R.layout.program_list, null); 

    holder.tViewMemoTitle=(TextView) rowView.findViewById(R.id.memo_list_title);  
    holder.tViewMemoCreatedDate=(TextView) rowView.findViewById(R.id.memo_list_created_date); 
    holder.tViewMemoAlarmedDate=(TextView) rowView.findViewById(R.id.memo_list_alarmed_date); 

    holder.tViewMemoTitle.setText(mMemoTitle.get(position));  
    holder.tViewMemoCreatedDate.setText("Created Date : " + mMemoCreatedDate.get(position)); 
    holder.tViewMemoAlarmedDate.setText("Alarm Date : " + mMemoAlarmedDate.get(position)); 

    rowView.setOnClickListener(new OnClickListener() {    
     @Override 
     public void onClick(View v) { 
      Cursor c=SplashActivity.db.rawQuery("SELECT * FROM memos WHERE id='"+mMemoIds.get(position)+"'", null); 
      if(c.moveToFirst()) 
      { 
       SplashActivity.db.execSQL("DELETE FROM memos WHERE id='"+mMemoIds.get(position)+"'"); 
       notifyDataSetChanged(); 
      } 


     } 
    }); 

    return rowView; 
} 

}

我的問題是,我不知道往哪裏放

notifydatasetchanged(); 
+0

立即刪除您的代碼之後。 '如果(c.moveToFirst()){ SplashActivity.db.execSQL( 「DELETE FROM備忘錄WHERE ID = ' 」+ mMemoIds.get(位置)+「'」); notifydatasetchanged(); }' –

回答

1
rowView.setOnClickListener(new OnClickListener() {  

     @Override 
     public void onClick(View v) {   
      Cursor c=SplashActivity.db.rawQuery("SELECT * FROM memos WHERE id='"+mMemoIds.get(position)+"'", null); 
      if(c.moveToFirst()) 
      { 
       SplashActivity.db.execSQL("DELETE FROM memos WHERE id='"+mMemoIds.get(position)+"'"); 
       mMemoIds.remove(c.getString(0)); 
       mMemoTitle.remove(c.getString(1)); 
       mMemoCreatedDate.remove(c.getString(3)); 
       mMemoAlarmedDate.remove(c.getString(4)); 
       notifyDataSetChanged(); // Insert your notifydatasetchanged here 
      } 

     } 
    }); 
+0

我已經試過這個,但它仍然不會更新我的列表視圖,儘管我的數據被刪除了。但仍沒有運氣:( –

+0

退房的編輯答案。PS你不應該承擔memoIDS,memoTitles等 –

+0

但memoIDS不同的名單,是從我mainactivity創建memoTitles,我不能把它在我的適配器,檢查我的完整適配器 –

相關問題