JMRboosties
JMRboosties

Reputation: 15740

Question on logistics of a multiple button listview item which can use the id of the item in an SQLite DB

So this is a bit complicated situation, so I will try to explain what is going on as best I can. I have a sqlite db which populates a listview. Each item in the listview contains the name of the item, along with two buttons. I want these two buttons to be able to use the sqlite db row id of the item they're contained in order to execute their purpose. So I need to get the id of the item, but I don't think using onListItemClick(ListView l, View v, int position, long id) will work for me, becuase I'm not clicking the entire item, rather, buttons within it. If I'm misunderstanding this point, please let me know. In order for the buttons to be clicked, I've created a class which extends SimpleCursorAdapter, which correctly handles clicks. Here are the two classes in question. First, the listactivity, and second, the custom listadapter.

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;

public class FriendRequestList extends ListActivity implements OnClickListener {

    private Button m_closeButton;
    private TagDBAdapter mDbAdapter;
    private Cursor mCursor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbAdapter = new TagDBAdapter(this);
        mDbAdapter.open();
        this.initLayout();
        fillData();

    }

    private void initLayout() {
        setContentView(R.layout.request_list);
        m_closeButton = (Button)findViewById(R.id.request_window_close_button);
        m_closeButton.setOnClickListener(this);
    }

    private void fillData() {
        SharedPreferences prefs = this.getSharedPreferences("Prefs", MODE_WORLD_READABLE);
        String username = prefs.getString("keyUsername", "");
        RestClient.getRequests(username, this);

        mCursor = mDbAdapter.fetchAllRequests();
        startManagingCursor(mCursor);
        String[] from = new String[] {TagDBAdapter.KEY_NAME};
        int[] to = new int[] {R.id.requester};

        SimpleCursorAdapter requests = new FriendRequestListAdapter(this, R.layout.request_view, mCursor, from, to);
        this.setListAdapter(requests);
    }

    public String getRequester() {

        return null;
    }

    @Override
    public void onClick(View v) {
        SharedPreferences prefs = this.getSharedPreferences("Prefs", MODE_WORLD_READABLE);
        String username = prefs.getString("keyUsername", "");
        RestClient.getUpdates(username, this);
        Intent intent = new Intent(this, MainMenu.class);
        this.startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mDbAdapter.close();
    }

}

And the adapter class:

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;

public class FriendRequestListAdapter extends SimpleCursorAdapter implements OnClickListener {

    private Context mContext;


    public FriendRequestListAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        mContext = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        Button confirm = (Button)view.findViewById(R.id.confirm_request);
        confirm.setOnClickListener(this);
        Button deny = (Button)view.findViewById(R.id.deny_request);
        deny.setOnClickListener(this);


        return view;
    }

    @Override
    public void onClick(View v) {
        SharedPreferences prefs = mContext.getSharedPreferences("Prefs", Activity.MODE_WORLD_READABLE);
        String username = prefs.getString("keyUsername", "");

        //want to get the id here so i can do stuff with the buttons

        switch(v.getId()) {
        case R.id.confirm_request :
            String confirmState = "2";
            //these buttons both work
            break;
        case R.id.deny_request :
            String denyState = "3";
            //these buttons both work
            Log.e("TESTING", "deny");
            break;
        }
    }
}

I hope I've made my question clear enough. To summarize, I don't know how I would get the rowId of the item a pair of buttons is nested in, because I'm not ever using the onListItemClick method, which is, as far as I'm aware, the way one would normally get the id of the item. Thank you for reading and thank you for your answers. Please let me know if there is any clarifications I can make.

Upvotes: 2

Views: 944

Answers (3)

Andrew Chen
Andrew Chen

Reputation: 368

 public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            Button confirm = (Button)view.findViewById(R.id.confirm_request);
            confirm.setOnClickListener(this);
            view.setTag(position);
            Button deny = (Button)view.findViewById(R.id.deny_request);
            deny.setOnClickListener(this);
            view.setTag(position);
            return view;
        }

@Override
public void onClick(View v) {

      // get the position here
      Integer position = (Integer)v.getTag();

      switch(v.getId()) {
        case R.id.confirm_request :
            break;
        case R.id.deny_request :
            String denyState = "3";
            break;
        }
    }

Upvotes: 0

Labeeb Panampullan
Labeeb Panampullan

Reputation: 34833


Create your a class by implements OnClickListener

class myCustomClass implements OnClickListener {
        int rowid;
        public myCustomClass(int rowid){
            this.rowid=rowid;
        }

        @Override
        public void onClick(View v) {
            //do you code in 'rowid' will have the current row position 

        }

    }

So in your

getView

function you can set listener like this

  //your other codes 
 confirm.setOnClickListener(new myCustomClass(position));
  //your other codes 
 deny.setOnClickListener(new myCustomClass(position));
  //your other codes

Hope this give you an idea.

Upvotes: 1

tinny_bug
tinny_bug

Reputation: 41

you maybe can do like this,in your custom adapter the getView method

public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        //you can set view's tag here
        //and you should do the reflect on your position to your rowid
        view.setTag(customMethodForGetId(position));
        //end
        Button confirm = (Button)view.findViewById(R.id.confirm_request);
        confirm.setOnClickListener(this);
        Button deny = (Button)view.findViewById(R.id.deny_request);
        deny.setOnClickListener(this);
        return view;
    }

and in your click event you can get the id by view.getTag();

Upvotes: 0

Related Questions