我在Android编程中非常新,我真的需要你的帮助。我的listview中有项目,所有项目都保存在mysql中。我想通过单击删除特定行。我的问题是我的项目在点击“删除项目”后没有被删除。 “删除项目”来自上下文菜单,如果我长按所述项目将弹出。我知道已经有一个与我的相似的线程,但所提供的解决方案并没有解决我的问题。
以下是相关代码。希望你们可以帮助我。
列出活动
public class ScheduleListActivity extends Activity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
SchedDbHelper schedDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_schedule_list);
listView =(ListView) findViewById(R.id.ListView);
listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(listDataAdapter);
schedDbHelper = new SchedDbHelper(getApplicationContext());
sqLiteDatabase = schedDbHelper.getReadableDatabase();
cursor = schedDbHelper.getInformations(sqLiteDatabase);
if(cursor.moveToFirst())
{
do{
String day,time,amount;
day = cursor.getString(0);
time = cursor.getString(1);
amount = cursor.getString(2);
DataProvider dataProvider = new DataProvider(day,time,amount);
listDataAdapter.add(dataProvider);
registerForContextMenu(listView);
}while(cursor.moveToNext());
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId())
{
case R.id.delete_id:
String position = String.valueOf(info.position);
schedDbHelper = new SchedDbHelper(getApplicationContext());
sqLiteDatabase = schedDbHelper.getReadableDatabase();
schedDbHelper.deleteinformation(position, sqLiteDatabase);
Object toRemove = listDataAdapter.getItem(info.position);
listDataAdapter.remove(toRemove);
listDataAdapter.notifyDataSetChanged();
Toast.makeText(getBaseContext(), "Deleted" , Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
}
SchedDBHELPER
public class SchedDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "SCHEDINFO.DB";
private static final int DATABASE_VERSIONS = 1;
private static final String CREATE_QUERY =
"CREATE TABLE "+ScheduleContract.NewSchedInfo.TABLE_NAME+"("+ ScheduleContract.NewSchedInfo.SCHED_DAY+" TEXT,"+
ScheduleContract.NewSchedInfo.SCHED_TIME+" TEXT,"+ ScheduleContract.NewSchedInfo.SCHED_AMOUNT+" TEXT);";
public SchedDbHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSIONS);
Log.e("DATABASE OPERATIONS", "Database created / opened... ");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS", "Table created... ");
}
public void addInformations(String day, String time, String amount, SQLiteDatabase db)
{
ContentValues contentValues = new ContentValues();
contentValues.put(ScheduleContract.NewSchedInfo.SCHED_DAY, day);
contentValues.put(ScheduleContract.NewSchedInfo.SCHED_TIME, time);
contentValues.put(ScheduleContract.NewSchedInfo.SCHED_AMOUNT, amount);
db.insert(ScheduleContract.NewSchedInfo.TABLE_NAME,null,contentValues);
Log.e("DATABASE OPERATIONS", "One row inserted... ");
}
public Cursor getInformations(SQLiteDatabase db)
{
Cursor cursor;
String[] projections = {ScheduleContract.NewSchedInfo.SCHED_DAY, ScheduleContract.NewSchedInfo.SCHED_TIME,
ScheduleContract.NewSchedInfo.SCHED_AMOUNT};
cursor = db.query(ScheduleContract.NewSchedInfo.TABLE_NAME, projections, null, null, null, null, null);
return cursor;
}
public void deleteinformation(String position, SQLiteDatabase sqLiteDatabase)
{
String table = ScheduleContract.NewSchedInfo.TABLE_NAME;
String key = ScheduleContract.NewSchedInfo.SCHED_DAY;
String[] whereargs = {position};
sqLiteDatabase.delete(table,key + "=?", whereargs);
}
列表数据适配器
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler
{
TextView DAY,TIME,AMOUNT;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public void remove(Object object) {
super.remove(object);
list.remove(object);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.DAY = (TextView)row.findViewById(R.id.text_day);
layoutHandler.TIME = (TextView)row.findViewById(R.id.text_time);
layoutHandler.AMOUNT = (TextView)row.findViewById(R.id.text_amount);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider)this.getItem(position);
layoutHandler.DAY.setText(dataProvider.getDay().toString());
layoutHandler.TIME.setText(dataProvider.getTime().toString());
layoutHandler.AMOUNT.setText(dataProvider.getAmount().toString());
return row;
}
}