在我的项目中放置notifydatasetchanged()的位置

时间:2016-10-10 10:37:49

标签: android sqlite custom-adapter

美好的一天

当我尝试从我的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
从这个习惯,我试图删除我创建的记录。

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

1 个答案:

答案 0 :(得分: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
            }

        }
    });