Android ListView 长按如何获取item的主键并操作对应数据库元组
============问题描述============
我做的是一个租房管理系统,大概是现在在对房子操作这个界面,我现在是要长按listView中的某一个Item,要求实现修改和删除功能。
This is my house_setting.xml document
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:id="@+id/ll_houseSetting"
>
<Button
android:id="@+id/buildingHouse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:background="#1C1C1C"
android:gravity="center"
android:text="添加新住房" />
<ListView
android:id="@+id/houseSettingList"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</LinearLayout>
This is my houseActivity.java document
package com.example.ren;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.content.ContentUris;
import android.content.Context;
import java.util.List;
import db.RentingDB;
import db.houseSettingDB;
import entity.house;
import android.app.Activity;
import android.app.AlertDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.os.Build;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class houseSettingActivity extends ActionBarActivity {
private ListView houseSettingList;
private List<house> houses;
private static final int ITEM_MODIFY = 1;
private static final int ITEM_DELETE = 2;
private houseSettingDB hDB;
private MyAdapter adapter;
private Cursor cursor;
private SQLiteDatabase db = null;
private Context context;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent intent = new Intent(houseSettingActivity.this,
settingActivity.class);
startActivity(intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.house_setting);
Button btnBuildingHouse = (Button) findViewById(R.id.buildingHouse);
btnBuildingHouse.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(houseSettingActivity.this,
houseBuildingActivity.class);
startActivity(intent);
}
});
hDB= new houseSettingDB(this);
houses = hDB.findAll();
ListView houseSettingList = (ListView) findViewById(R.id.houseSettingList);
adapter=new MyAdapter();
houseSettingList.setAdapter(adapter);
registerForContextMenu(houseSettingList);
}
private class MyAdapter extends BaseAdapter {
/**
* listview总数
*/
@Override
public int getCount() {
return houses.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = new TextView(getApplicationContext());
tv.setTextSize(20);
tv.setTextColor(Color.BLUE);
house house = houses.get(position);
tv.setText(house.toString());
return tv;
}
}
// 长按时显示的菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("请选择操作");
menu.add(0, ITEM_MODIFY, 0, "编辑");
menu.add(0, ITEM_DELETE, 1, "删除");
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
int index = (int)info.id;
// Object itemValue = adapter.getItem(info.position);
switch (item.getItemId()) {
case ITEM_MODIFY:
break;
case ITEM_DELETE:
String[] str = {String.valueOf(index)};
db= (new RentingDB (getApplicationContext())).getWritableDatabase();
db.delete("house","RecNo=?",str);
cursor.requery();
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
I hope someone strong enough to solve my problem.If some problem on understanding my request , please make a comment on me . I will response as soon as possible
============解决方案1============
先从 houses 里删除掉那个数据,然后使用 adapter.notifyDataChanged 提示 Adapter 更新数据
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。