我是使用sqlite数据库的新手。我创建了一个数据库作为测试。和表作为测试。在测试表中,我创建了两列,一列是key_id
,另一列是menuname
。
我在test表中插入了一些menunames
和key_id。然后我使用simplecursor适配器从sqlite数据库中获取menunames
到自定义列表视图,然后附加到edittext:
网址包含我的输出屏幕截图。如果我在android中单击菜单按钮后在edittext中更改了一些menunames
,则表示它显示了保存按钮。如果单击“保存”按钮,则表示我编辑的菜单名称被旧menunames
或key_id
替换:
如何将自定义列表视图中的menuitem名称更新为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);
}