listview中的按钮与simplecursoradapter

时间:2014-02-07 20:31:18

标签: android listview simplecursoradapter

你能用简单的例子帮我吗?我有listitem文本框和2个iagebutton,我怎么能把监听器绑定到我的按钮而不用null写新的自定义适配器(我希望覆盖justcursoradapter)。 对不起我的英语,我希望,你能让我清楚,简单地理解这些例子。

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;
    }

}

}

UPD:我写自定义类

class MySimpleCursorAdapter extends SimpleCursorAdapter {
    public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

    @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) findViewById(R.id.tvFormName);
        text.setText(name);

        Button yourButton = (Button) findViewById(R.id.ibtnDelete);
        yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });


    }
}

但在return LayoutInflater.from(context).inflate(R.layout.listform_item);上有错误,你能告诉我现在如何使用监听器按钮(例如删除项目)?如何获取点击按钮的项目数量?

2 个答案:

答案 0 :(得分:2)

不要使用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);
}

答案 1 :(得分:0)

您必须继承SimpleCursorAdapter。 覆盖两种方法getViewbindView

@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});
}