从数据库

时间:2016-07-16 06:56:40

标签: android listview

我正在删除标题2列表项目

enter image description here

但最后一个标题被删除,即标题8被删除

enter image description here

然后我返回上一个屏幕并重新打开列表。正确的第二项标题2已被删除。

enter image description here

这是DataAdapter

public class DataAdapter extends BaseAdapter {
String a[];
Context ctx;
View.OnClickListener onClickListener;
MyDBHandler dbHelper;
AlertDialog alertDialog;
ArrayList<Calendars> arrayList;

public DataAdapter(Context reminderList, ArrayList<Calendars> arrayList, MyDBHandler dbHelper) {
    this.ctx = reminderList;
    this.arrayList = arrayList;
    this.dbHelper = dbHelper;
}

public void Swap(ArrayList<Calendars> arrayList) {
    this.arrayList.clear();
    this.arrayList.addAll(arrayList);
    this.notifyDataSetChanged();
    Log.e("arraylist", arrayList.toString());
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return arrayList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflate = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflate.inflate(R.layout.activity_view_record, parent, false);
        final ViewHolder holder = new ViewHolder();
        holder.dateTextView = (TextView) convertView.findViewById(R.id.datelist);
        holder.timeTextView = (TextView) convertView.findViewById(R.id.timelist);
        holder.titleTextView = (TextView) convertView.findViewById(R.id.titlelist);
        holder.idTextview = (TextView) convertView.findViewById(R.id.idlist);
        holder.deleteButton = (Button) convertView.findViewById(R.id.deletelist);
        holder.dateTextView.setText(arrayList.get(position).get_reminderdate());
        holder.timeTextView.setText(arrayList.get(position).get_remindertime());
        holder.titleTextView.setText(arrayList.get(position).get_remindertitle());
        holder.idTextview.setText(arrayList.get(position).get_id() + "");
        holder.deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
                adb.setTitle("Delete");
                adb.setMessage("Are you sure you want to delete  this reminder?");
                //final int positionToRemove = view.getId();
                adb.setNegativeButton("Cancel", null);

                adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        dbHelper.remove(arrayList.get(position).get_id());
                        Log.e("position", position + "");

                        arrayList.remove(position);
                        arrayList = dbHelper.databaseToArrayList();
                        notifyDataSetInvalidated();
                        notifyDataSetChanged();

                        //Swap(dbHelper.databaseToArrayList());


                    }
                });
                adb.show();
            }
        });
    }
    return convertView;
}

class ViewHolder {
    TextView titleTextView, dateTextView, timeTextView, idTextview;
    Button deleteButton;
}
}

这是ListActivity

public class ReminderList extends ActionBarActivity {
Calendars calendars;
Button deleteButton;
private MyDBHandler dbHelper;
private ListView listView;
private ArrayList<Calendars> arrayList;
private SimpleCursorAdapter adapter;

int pos;
DataAdapter dataAdapter;

final String[] from = new String[]{MyDBHandler.COLUMN_REMINDER_TITLE,
        MyDBHandler.COLUMN_REMINDER_DATE, MyDBHandler.COLUMN_REMINDER_TIME, MyDBHandler.COLUMN_ID,
        MyDBHandler.COLUMN_REMINDER_DESCRIPTION, MyDBHandler.COLUMN_REMINDER_SNOOZE, MyDBHandler.COLUMN_REMINDER_REPEAT, MyDBHandler.COLUMN_ID};

final int[] to = new int[]{R.id.titlelist, R.id.datelist, R.id.timelist, R.id.idlist,
        R.id.descriptionlist, R.id.snoozelist, R.id.repeatlist, R.id.deletelist};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_reminder_list);
    deleteButton = (Button) findViewById(R.id.deletelist);
    // adapter=new DataAdapter(ReminderList.this,from);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);


    return true;
}

@Override
protected void onStart() {
    arrayList = new ArrayList<Calendars>();
    dbHelper = new MyDBHandler(this);
    Cursor cursor = dbHelper.fetch();
    arrayList = dbHelper.databaseToArrayList();

    listView = (ListView) findViewById(R.id.list_view);
    listView.setEmptyView(findViewById(R.id.empty));

    dataAdapter = new DataAdapter(this, arrayList, dbHelper);
    listView.setAdapter(dataAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {

            String title = arrayList.get(position).get_remindertitle();
            Log.d("title", title);
            String date = arrayList.get(position).get_reminderdate();
            String time = arrayList.get(position).get_remindertime();
            String id = arrayList.get(position).get_id() + "";
            String description = arrayList.get(position).get_reminderdescription();
            String snooze = arrayList.get(position).get_remindersnooze();
            String repeat = arrayList.get(position).get_reminderrepeat();

            Intent modify_intent = new Intent(getApplicationContext(), AlarmActivity.class);

            modify_intent.putExtra("id", id);
            modify_intent.putExtra("title", title);
            modify_intent.putExtra("time", time);
            modify_intent.putExtra("date", date);
            modify_intent.putExtra("description", description);
            modify_intent.putExtra("snooze", snooze);
            modify_intent.putExtra("repeat", repeat);

            startActivity(modify_intent);
            dataAdapter.notifyDataSetChanged();
            listView.invalidateViews();
        }
    });
    super.onStart();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home: {
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        case R.id.add_record: {
            Intent add_mem = new Intent(this, AlarmActivity.class);
            startActivity(add_mem);

        }
        default:
            return super.onOptionsItemSelected(item);
    }
}


}

DatabasetoArraylist功能

    public ArrayList<Calendars> databaseToArrayList() {

    ArrayList<Calendars> arrayList = new ArrayList();

    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_REMINDER;
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("_reminderdate")) != null) {

            Calendars calendars = new Calendars();
            calendars.set_id(c.getInt(c.getColumnIndex(COLUMN_ID)));
            calendars.set_reminderdate(c.getString(c.getColumnIndex(COLUMN_REMINDER_DATE)));
            calendars.set_remindertime(c.getString(c.getColumnIndex(COLUMN_REMINDER_TIME)));
            calendars.set_remindertitle(c.getString(c.getColumnIndex(COLUMN_REMINDER_TITLE)));
            calendars.set_reminderdescription(c.getString(c.getColumnIndex(COLUMN_REMINDER_DESCRIPTION)));
            calendars.set_reminderrepeat(c.getString(c.getColumnIndex(COLUMN_REMINDER_REPEAT)));
            calendars.set_remindersnooze(c.getString(c.getColumnIndex(COLUMN_REMINDER_SNOOZE)));

            arrayList.add(calendars);
        }
        c.moveToNext();
    }
    c.close();
    db.close();
    return arrayList;

}

1 个答案:

答案 0 :(得分:0)

通过以下代码更改适配器的getView()方法:

 @Override
 public View getView(final int position, View convertView, ViewGroup parent) 
 {
    ViewHolder holder = null;
    final Calendars calendars = arrayList.get(position);

    if (convertView == null) 
    {
        LayoutInflater inflate = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflate.inflate(R.layout.activity_view_record, parent, false);

        holder = new ViewHolder();
        holder.dateTextView = (TextView) convertView.findViewById(R.id.datelist);
        holder.timeTextView = (TextView) convertView.findViewById(R.id.timelist);
        holder.titleTextView = (TextView) convertView.findViewById(R.id.titlelist);
        holder.idTextview = (TextView) convertView.findViewById(R.id.idlist);
        holder.deleteButton = (Button) convertView.findViewById(R.id.deletelist);
        convertView.setTag(holder);
   }
   else
   {
       holder = (ViewHolder) convertView.getTag();
   }

    holder.dateTextView.setText(calendars.get_reminderdate());
    holder.timeTextView.setText(calendars.get_remindertime());
    holder.titleTextView.setText(calendars.get_remindertitle());
    holder.idTextview.setText(calendars.get_id() + "");

    holder.deleteButton.setTag(calendars);
    holder.deleteButton.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {

            AlertDialog.Builder adb = new AlertDialog.Builder(v.getRootView().getContext());
            adb.setTitle("Delete");
            adb.setMessage("Are you sure you want to delete  this reminder?");
            //final int positionToRemove = view.getId();
            adb.setNegativeButton("Cancel", null);

            adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    final selectedCalendar = (Calendars) holder.deleteButton.getTag();

                    dbHelper.remove(selectedCalendar.get_id());
                    arrayList = dbHelper.databaseToArrayList();
                    notifyDataSetInvalidated();
                    notifyDataSetChanged();

                    //Swap(dbHelper.databaseToArrayList());

                }
            });
        adb.show();
        }
    });
}
return convertView;
}