我是使用 sqlite 数据库的新手。我创建了一个数据库作为测试。和表作为测试。在测试表中,我创建了两列,一列是key_id
另一列menuname
。
我在测试表中插入了一些menunames
和 key_id。然后我menunames
使用 simplecursor 适配器从 sqlite 数据库获取到自定义列表视图,然后附加到 edittext:
该网址包含我的输出屏幕截图。如果我menunames
在点击android中的菜单按钮后在edittext中更改了一些内容,则意味着它显示了保存按钮。如果我点击保存按钮意味着我正在编辑的菜单名称被旧的menunames
或使用的替换key_id
:
如何将自定义列表视图中的菜单项名称更新为 android 中的 sqlite 数据库?
我试过这段代码:
public class EditMainMenu extends Activity {
private SQLiteAdapter mySQLiteAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.editmainmenu);
ListView listContent = (ListView) findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
final Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
int[] to = new int[] { R.id.text };
EditMainMenuListview adapter = new EditMainMenuListview(this,
R.layout.editmainmenulistview, cursor, from, to);
listContent.setAdapter(adapter);
System.out.println("gouthampra");
}
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.save, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.save:
break;
case R.id.back:
break;
default:
break;
}
return true;
}
}
EditMainMenuListview.java
public class EditMainMenuListview extends SimpleCursorAdapter{
Cursor c;
Context context;
Activity activity;
public EditMainMenuListview(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context=context;
this.activity=(Activity) context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
convertView = View.inflate(context, R.layout.editmainmenulistview, null);
View row = convertView;
c.moveToPosition(position);
EditText menuname = (EditText) convertView.findViewById(R.id.text);
menuname.setFocusable(true);
menuname.setText(c.getString(1));
return(row);
}