Eman87
Eman87

Reputation: 2745

Press button in listView adapter to get data in this row

I have a listView that shows data that in sqlite, I'm using baseAdapter. The list row contains text and button.

I press the button in each row to give me each data of the selected row from the database (not pressing the row itself)

This is code of listView:

tipsList = new ArrayList<HashMap<String, String>>();

 list = (ListView) rootView.findViewById(R.id.tipList);

 do {
  map = new HashMap<String, String>();

            map.put(DAO.TIP_ID, c.getString(c.getColumnIndex(c.getColumnName(0))));
            map.put(DAO.TIP_CONTENT, c.getString(c.getColumnIndex(c.getColumnName(1))));
            map.put(DAO.TIP_FAVORITE, c.getString(c.getColumnIndex(c.getColumnName(2))));

            // adding HashList to ArrayList
            tipsList.add(map);

        } while (c.moveToNext());


        tipsAdapter = new TipsAdapter(getActivity(), tipsList, tipType, db);
        list.setAdapter(tipsAdapter);

And this is code of TipsAdapter.java

public class TipsAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    Context context;
    Typeface tf;
    String tipID;
    String tipText;
    String isFavorite;
    DAO db;
    HashMap<String, String> quote;
    int selectedTipType;

    public TipsAdapter(Activity a, ArrayList<HashMap<String, String>> d, int selectedTipType, DAO db) {
        activity = a;
        data = d;
        context = a;
        this.selectedTipType = selectedTipType;
        this.db = db;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    // ==============================================================================

    public int getCount() {
        return data.size();
    }

    // ==============================================================================

    public Object getItem(int position) {
        return position;
    }

    // ==============================================================================

    public long getItemId(int position) {
        return position;
    }

    // ==============================================================================

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.tip_list_row, null);

        TextView tipContent = (TextView) vi.findViewById(R.id.tip_text); // tip
        final ImageView favStar = (ImageView) vi.findViewById(R.id.favorite_star);

        tf = Typeface.createFromAsset(activity.getAssets(), "fonts/bauhausm_0.ttf");
        tipContent.setTypeface(tf);

        quote = new HashMap<String, String>();
        quote = data.get(position);

        tipText = quote.get(DAO.TIP_CONTENT).trim();


        favStar.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View view) {

                        // I want here get all data like tipId - tipText - tipFavotite of the current row in toast
              }

            });



        return vi;
    }
     }

How to get the data of the current row when press the button? Hope anyone got my mean. Thanks in advance.

Upvotes: 0

Views: 1215

Answers (2)

Eman87
Eman87

Reputation: 2745

I Solved it by myself by making position as final and put quote = data.get(position); inside the listener of the button.

Upvotes: 0

Keyfe Ang
Keyfe Ang

Reputation: 386

Maybe you can set the tag of your button.

favStar.setTag(position);
favStar.setOnClickListener (new OnClickListener()
{
    @Override
   public void onClick(View view) {

           int position = (int) view.getTag();

           HashMap<String, String>() quote = data.get(position);

           String tipText = qout.get(DAO.TIP_CONTENT);
           String tipContent = ......;

   }
}

Upvotes: 1

Related Questions