I only have a ListView containing TextView. I would like to add an image and a button to each row in the list, such that when i click on the button, it will send the data gotten from SimpleCursorAdapter and bring me to another activity.
I have got two xml files. one that contains the ListView and the other xml file contains the TextView, ImageView and Button.
I have read many articles in stackoverflow and online, but i still cant get it working. I think there is something which is got to do with the getView(), onClickListener for the button but i do not know how to link it together with the SimpleCursorAapter class. As in my case, i did not extend SimpleCursorAdapter but used it directly. I am confused about how to allow it to access the xml file which contains the button and image. my current code is like this:
public class BoardsActivity extends ListActivity {
private static final int ACTIVITY_SUBSCRIBE = 0;
private AnnouncementDbAdapter dbAdapter;
private Cursor cursor;
private Button subscribeButton;
//private LayoutInflater mInflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe_to);
// mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
dbAdapter = new AnnouncementDbAdapter(this);
// dbAdapter.open();
// fillData();
@Override
protected void onResume() {
super.onResume();
dbAdapter.open();
fillData();
@Override
protected void onPause() {
super.onPause();
dbAdapter.close();
private void fillData() {
cursor = dbAdapter.fetchAllBoards();
startManagingCursor(cursor);
String[] from = new String[] {AnnouncementDbHelper.BNAME };
int[] to = new int[] {R.id.subscribeactivity_bname};
SimpleCursorAdapter board = new SimpleCursorAdapter(this,R.layout.subscribe_to_row, cursor, from, to);
setListAdapter(board);
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor details = dbAdapter.fetchBoard(id);
startManagingCursor(details);
Intent i = new Intent(this, BoardDetailsActivity.class);
i.putExtra(AnnouncementDbHelper.BID, id);
startActivityForResult(i, ACTIVITY_SUBSCRIBE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
dbAdapter.open();
fillData();}
How can i get the image from Drawables and when the user clicks on the button, it goes to another activity?
For this you need to create Custom List view that will displays Textview,ImageView and button.
Check this for Custom list view
Also check this List View and Adapters available