1

I have a class public class StoreListViewActivity extends Activity implements OnItemClickListener that has a method public void onItemClick(AdapterView<?> parent, View view, int position, long id) that I don't think I have a use for because this is a listener for the entire area of the list. What I want is to have buttons with different listeners and images in place of where the button is, but with my implementation (of the onBuyButtonClick method), I only have one listener and all the other "Buy!" buttons all use that same listener because I'm using the Button bb = (Button) findViewById(R.id.buy_button); so they are all the same button. What I want is:

---------------------------------

[image] Fruit Ninja is..... [Buy!] --> buy button has its own listener (i.e. decrease balance)

---------------------------------

[image] Subway Surfers is.. [Buy!] --> buy button has its own listener (i.e. takes you to play store)

---------------------------------

and if possible, could I have images in place of that Buy button with my implementation? I have a ButtonView on the right side of the ListView and when I try to add an ImageView there too, the app crashes

I have a button on the right of each list that has different functions. In my StoreListViewActivity class, I have objects:

public static final Integer[] images = 
    { R.drawable.fruit_ninja,
      R.drawable.subway_surfers };

public static final Spanned[] titles = new Spanned[]
    { Html.fromHtml("<b>" + "Fruit Ninja" + "</b>" + " is selling " +
                    "<b>" + "20 Starfruits" + "</b>" + " for " + "<b>" +
                    "100" + "</b>" + " Coins!"),
      Html.fromHtml("<b>" + "Subway Surfers" + "</b>" + " is selling " +
                    "<b>" + "5 Paint-Powered Jetpacks" + "</b>" + " for " + "<b>" +
                    "150" + "</b>" + " Coins!") };

public static final String[] buy =
    { "Buy!", "Buy!" };

These objects go in an ArrayList and get displayed in a ListView in the onCreate method:

// Android ID
private String android_id;

ListView storeListView;
List<StoreRowItem> storeRowItems;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // no title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // set full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // inflate ListView
    setContentView(R.layout.store);
    // get android id
    android_id = 
            Secure.getString(getBaseContext().getContentResolver(), 
                             Secure.ANDROID_ID);
    // set text for balance
    TextView tv = (TextView) findViewById(R.id.store_header2);
    try {
        tv.setText("  My Balance:                                     "
                    + //static method to grab balance);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    // store items for current game
    storeRowItems = new ArrayList<StoreRowItem>();
    for (int i = 0; i < titles.length; i++) {
        StoreRowItem item = new StoreRowItem(images[i], titles[i], buy[i]);
        storeRowItems.add(item);
    }

    size = storeRowItems.size();
    storeListView = (ListView) findViewById(R.id.store_list2);
    StoreListViewAdapter adapter = new StoreListViewAdapter(this,
                                    R.layout.store_list_row, storeRowItems);
    // can select buttons
    storeListView.setItemsCanFocus(true);
    // disable focus on the list items
    storeListView.setFocusable(false);
    storeListView.setFocusableInTouchMode(false);
    storeListView.setClickable(false);

    storeListView.setAdapter(adapter);
    storeListView.setOnItemClickListener(this);
}

for reference, this is my store_list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="10dp" >

<!--  Application Icon -->
<LinearLayout 
    android:id="@+id/store_thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:layout_alignParentLeft="true"
    android:background="@drawable/image_bg"
    android:layout_marginRight="5dp">

    <ImageView
        android:id="@+id/store_list_image"
        android:layout_width="50dp"
        android:layout_height="50dp" />

</LinearLayout>

<!-- Title-->
<TextView
    android:id="@+id/store_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_toRightOf="@id/store_thumbnail"
    android:paddingLeft="10dp"
    android:paddingRight="75dp"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15sp" />

 <!-- Rightend Button -->
 <Button 
    android:id="@+id/buy_button"
    android:layout_width="70dp"
    android:layout_height="25dp"
    android:textSize="15sp"
    android:textColor="#ffffff"
    android:background="@drawable/buy_button_gradient"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" />

</RelativeLayout>

And this is my adapter:

public class StoreListViewAdapter extends ArrayAdapter<StoreRowItem> {

Context storeContext;

public StoreListViewAdapter(Context context, int resourceId,
        List<StoreRowItem> items) {
    super(context, resourceId, items);
    this.storeContext = context;
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    Button buttonView;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    StoreRowItem storeRowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) storeContext
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.store_list_row, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.store_title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.store_list_image);
        holder.buttonView = (Button) convertView.findViewById(R.id.buy_button);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.buttonView.setText(storeRowItem.getBuy());
    holder.txtTitle.setText(storeRowItem.getTitle());
    holder.imageView.setImageResource(storeRowItem.getImageId());

    return convertView;
    }
}

1 Answer 1

1

You can do something like this

In the getView method, set a tag for each button so that you can identify the button later when the button is clicked.

  holder.buttonView.setTag(storeRowItem.getTitle());

And when the button is clicked, get the tag of the button clicked, and do suitable action based on the tag.

holder.buttonView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String title = v.getTag().toString();

                    if (title.equals("Title1")) {
                        //Do something
                    }

                    else if (title.equals(("Title2")) {
                        //Do something
                    }

                }
            });

Here is the complete code for Adapter

public class StoreListViewAdapter extends ArrayAdapter<StoreRowItem> {

        Context storeContext;

        public StoreListViewAdapter(Context context, int resourceId,
                List<StoreRowItem> items) {
            super(context, resourceId, items);
            this.storeContext = context;
        }

        /*private view holder class*/
        private class ViewHolder {
            ImageView imageView;
            TextView txtTitle;
            Button buttonView;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            StoreRowItem storeRowItem = getItem(position);

            LayoutInflater mInflater = (LayoutInflater) storeContext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.store_list_row, null);
                holder = new ViewHolder();
                holder.txtTitle = (TextView) convertView.findViewById(R.id.store_title);
                holder.imageView = (ImageView) convertView.findViewById(R.id.store_list_image);
                holder.buttonView = (Button) convertView.findViewById(R.id.buy_button);



                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();

            holder.buttonView.setText(storeRowItem.getBuy());
            holder.txtTitle.setText(storeRowItem.getTitle());
            holder.imageView.setImageResource(storeRowItem.getImageId());
            holder.buttonView.setTag(storeRowItem.getTitle());

            holder.buttonView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String title = v.getTag().toString();

                    if (title.equals("Title1")) {
                        //Do something
                    }

                    else if (title.equals(("Title2")) {
                        //Do something
                    }

                }
            });

            return convertView;
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.