如何将侦听器设置为listView中的按钮?

时间:2016-01-24 21:32:52

标签: android listview view onclicklistener android-imagebutton

我正在为一家咖啡店写作和申请。这是观点:

enter image description here 我在列表视图的每一行都有2个按钮。 我的问题是如何听每个按钮。其中一个应该打开咖啡店的细节,另一个应该在地图上打开咖啡店。 这是coffeeShopActivity的代码:

public class CoffeeShopActivity extends AppCompatActivity{
private static final String TAG_STORES = "stores";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_ADDRESS = "address";
private static final String TAG_LONGITUDE = "longitude";
private static final String TAG_LATITUDE = "latitude";
ListView list;
String[] coffeeShopName;
String[] coffeeShopAddress;
String[] coffeeShopLatitude;
String[] coffeeShopLongitude;
Integer[] imageId = {R.drawable.coffee1,R.drawable.coffee2,R.drawable.coffee3,R.drawable.coffee4,
        R.drawable.coffee5,R.drawable.coffee6,R.drawable.coffee7,R.drawable.coffee8,
};

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coffeeshop);

    ImageView image = (ImageView) findViewById(R.id.Logo);
    image.setImageResource(R.drawable.logo);

    /*****************************************/
    ArrayList<CoffeeShop> menus =parsJson(getJsonFromFileIAssets());
    coffeeShopName =new String[menus.size()];
    for (int i = 0; i < menus.size(); i++) {
        coffeeShopName[i]=menus.get(i).getName();
    }

    coffeeShopAddress =new String[menus.size()];
    for (int i = 0; i < menus.size(); i++) {
        coffeeShopAddress[i]=menus.get(i).getAddress();
    }

    coffeeShopLatitude =new String[menus.size()];
    for (int i = 0; i < menus.size(); i++) {
        coffeeShopLatitude[i]=menus.get(i).getLatitude();
    }

    coffeeShopLongitude =new String[menus.size()];
    for (int i = 0; i < menus.size(); i++) {
        coffeeShopLongitude[i]=menus.get(i).getLongitude();
    }

    CoffeeShopList adapter = new CoffeeShopList(CoffeeShopActivity.this, coffeeShopName,null,imageId, coffeeShopAddress, null,null,null,coffeeShopLatitude, coffeeShopLongitude);
    list=(ListView)findViewById(R.id.CoffeeShopList);
    list.setAdapter(adapter); } }

这是适配器类:

public class CoffeeShopList extends ArrayAdapter<String>{
private final Activity context;
private String []coffeeShopName;
private String[][] hours;
private String []address;
private String[][] phone;
private String[] emailAddress;
private String[] webSite;
private final Integer[] imageId;
private String[] latitude;
private String[] longitude;

    public CoffeeShopList(Activity context,String[] coffeeShopName,String[][] hours,   Integer[] imageId,
                      String[] address, String[][] phone,String[] emailAddress,
                      String[] webSite,String[] latitude, String[] longitude)
{
    super(context, R.layout.coffeeshop_list_row, coffeeShopName);
    this.context = context;
    this.coffeeShopName = coffeeShopName;
    this.hours = hours;
    this.address = address;
    this.phone = phone;
    this.emailAddress = emailAddress;
    this.webSite = webSite;
    this.latitude = latitude;
    this.imageId = imageId;
    this.longitude = longitude;

}

@Override
public View getView(final int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.coffeeshop_list_row, null, true);

    TextView coffeeShopTitle = (TextView) rowView.findViewById(R.id.coffeeshopTitle);
    TextView coffeeshopAddress = (TextView) rowView.findViewById(R.id.address);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.coffeeshopimg);
    ImageButton openBtn = (ImageButton) rowView.findViewById(R.id.openCoffeeshopBtn);


    coffeeShopTitle.setText(coffeeShopName[position]);
    coffeeshopAddress.setText(address[position]);
    imageView.setImageResource(imageId[position]);

    return rowView;
}

}

我在这里找不到一个明确的答案:(

Android: ListView elements with multiple clickable buttons

1 个答案:

答案 0 :(得分:1)

ImageButton openBtn = (ImageButton) rowView.findViewById(R.id.openCoffeeshopBtn);
    openBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // Do your stuff here
      }
    });

ImageButton anotherButton = (ImageButton) rowView.findViewById(R.id.anotherButton);
    anotherButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // Do Some other stuff here.
      }
    });