Android 带有SimpleCorsOrAdapter的listview中的按钮
你能帮我做些简单的事情吗?我有一个带有文本框和2个iagebutton的listitem,我可以将监听器绑定到我的按钮,而不必通过null编写新的自定义适配器(我希望只覆盖SimpleCorsorAdapter)。 对不起,我的英语太难了,我希望你能给我一些简单明了的例子Android 带有SimpleCorsOrAdapter的listview中的按钮,android,listview,simplecursoradapter,Android,Listview,Simplecursoradapter,你能帮我做些简单的事情吗?我有一个带有文本框和2个iagebutton的listitem,我可以将监听器绑定到我的按钮,而不必通过null编写新的自定义适配器(我希望只覆盖SimpleCorsorAdapter)。 对不起,我的英语太难了,我希望你能给我一些简单明了的例子 public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> { ListView lvForms;
public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
ListView lvForms;
dbForm table_form;
SimpleCursorAdapter scAdapter;
/**
* Called when the activity is first created.
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
table_form=new dbForm(this);
table_form.open();
String[] from = new String[]{DBHelper.FORM_NAME, DBHelper.FORM_TITLE};
int[] to = new int[]{R.id.tvFormName, R.id.tvFormTitle};
scAdapter = new SimpleCursorAdapter(this, R.layout.listform_item, null, from, to, 0);
lvForms = (ListView) findViewById(R.id.lvForms);
lvForms.setAdapter(scAdapter);
registerForContextMenu(lvForms);
getSupportLoaderManager().initLoader(0, null, this);
}
// обработка нажатия кнопки
public void onButtonClick(View view) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
protected void onDestroy() {
super.onDestroy();
table_form.close();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
return new MyCursorLoader(this, table_form);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
scAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
static class MyCursorLoader extends CursorLoader {
dbForm table_form;
public MyCursorLoader(Context context, dbForm table_form) {
super(context);
this.table_form = table_form;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = table_form.getAllData();
return cursor;
}
}
但是在
返回LayoutInflater.from(context).充气(R.layout.listform_项)时出错
您能告诉我现在如何使用listener for按钮(例如,用于删除项)吗?如何获取我们单击按钮的项目数?您必须子类化SimpleCursorAdapter
。
覆盖两个方法getView
和bindView
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
return convertView;
}
return LayoutInflater.from(context).inflate( R.layout.listform_item);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
String name = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_NAME));
TextView text = (TextView) viewfindViewById(R.id.tvFormName);
text.setText(name );
Button yourButton = (Button) viewfindViewById(R.id.magic_button);
yourButton.setOnClickListener(new View.OnClickListener(){ //implement listener here});
}
不要在重写简单游标适配器和其他游标适配器时使用getView。必须重写newView和bindView方法。 这是我的工作代码
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listform_item, parent, false);
return view;
}
@Override
public void bindView(View view, Context Context, Cursor cursor) {
String name = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_NAME));
String title = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_TITLE));
TextView formname = (TextView) view.findViewById(R.id.tvFormName);
formname.setText(name);
TextView formtitle = (TextView) view.findViewById(R.id.tvFormTitle);
formtitle.setText(title);
ImageButton yourButton = (ImageButton) view.findViewById(R.id.ibtnDelete);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view != null) {
Object obj = view.getTag();
//if(obj != null && obj instanceof Integer) {
dbForm form = new dbForm(context);
form.open();
String st = obj.toString();
form.deleteForm(Long.valueOf(st).longValue());
Toast.makeText(context, "Delete row with id = " + st, Toast.LENGTH_LONG).show();
}
}
});
Object obj = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_ID));
yourButton.setTag(obj);
}
非常感谢,但是imagebutton呢?我不知道按钮的连接处理器(onButtonClick或其他)在哪里?请告诉我/谢谢-太好了!哦,我是早上做的,因为现在是凌晨3:55。对不起,我在返回布局时出错。从(上下文)开始。充气(R.layout.listform_项);使用方法充气(layoutId,null);是的,我在第二个小时前解决了这个问题,如果您有时间,您能回答我关于更新的问题吗?是的,根据您的要求,您必须覆盖
newView()
和bindView()
方法。
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listform_item, parent, false);
return view;
}
@Override
public void bindView(View view, Context Context, Cursor cursor) {
String name = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_NAME));
String title = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_TITLE));
TextView formname = (TextView) view.findViewById(R.id.tvFormName);
formname.setText(name);
TextView formtitle = (TextView) view.findViewById(R.id.tvFormTitle);
formtitle.setText(title);
ImageButton yourButton = (ImageButton) view.findViewById(R.id.ibtnDelete);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view != null) {
Object obj = view.getTag();
//if(obj != null && obj instanceof Integer) {
dbForm form = new dbForm(context);
form.open();
String st = obj.toString();
form.deleteForm(Long.valueOf(st).longValue());
Toast.makeText(context, "Delete row with id = " + st, Toast.LENGTH_LONG).show();
}
}
});
Object obj = cursor.getString(cursor.getColumnIndex(DBHelper.FORM_ID));
yourButton.setTag(obj);
}