Question

can you help me with simple eample? I have listitem with textboxes and 2 iagebutton, how i can bind listeners to my buttons without writing new custom adapter by null(i hope override just simplecursoradapter). Sorry for my hard english, and i hope, that you give me clear and simple for understanding examples.

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: i write custom class

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) {

            }
        });


    }
}

but have error on return LayoutInflater.from(context).inflate(R.layout.listform_item); and can you tell me how now use listener for button(for delete item, for example)? how get number of item where we click on button?

Was it helpful?

Solution

Don`t use getView in overriding the simple cursor adapter and other cursor adapters. You must override the newView and bindView methods. Here is my working code

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

OTHER TIPS

You have to subclass SimpleCursorAdapter. Override two methods getView and 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});
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top