当我点击callButton时我需要打开手机应用程序并在点击mapButton时插入地图位置,PS:适配器已正确填充但是当我点击任何对象时它什么都没做
这是我的适配器代码
myMap.get(s)
下面的是包含列表视图的活动
package com.voyage_eg.voyage;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class StoresAdapter extends ArrayAdapter<Stores> {
public StoresAdapter(Context context, ArrayList<Stores> stores) {
super(context, 0, stores);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// super.getView(position, convertView, parent);
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.dist_layout, parent, false);
}
Stores currentStore = getItem(position);
Stores currentStores = getItem(position);
TextView storeName = (TextView) listItemView.findViewById(R.id.storeName);
storeName.setText(currentStores.getStoreName());
ImageView storeImage = (ImageView) listItemView.findViewById(R.id.storeImage);
storeImage.setImageResource(currentStores.getStoreImageResourceId());
ImageButton callButton = (ImageButton) listItemView.findViewById(R.id.callButton);
callButton.setImageResource(currentStores.getStoreMobileButton());
ImageButton mapButton = (ImageButton) listItemView.findViewById(R.id.mapButton );
mapButton.setImageResource(currentStores.getStoreAddressButton());
return listItemView;
}
}
这是我的自定义类
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.distribution_points);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
cartPadgeNumber=findViewById(R.id.cartPadgeNumber);
///////////////////////////////////////////////////////////
// Create an ArrayList of AndroidFlavor objects
final ArrayList<Stores> stores = new ArrayList<Stores>();
stores.add(new Stores("El Badry Serag Mall", R.drawable.badry_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("El Badry Maadi", R.drawable.badry_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("El Badry Hurghada", R.drawable.badry_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Dose Mart Nasr City", R.drawable.dose_nasr_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Dose Mart Heliopolis", R.drawable.dose_hel_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Dose Mart Maadi", R.drawable.dose_maadi_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("ADAMS", R.drawable.adams_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("First Exit Maadi", R.drawable.first_exit_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("First Exit New Cairo", R.drawable.first_exit_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Mahala Vape Club", R.drawable.mahala_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("El Kot Vape El Zamalik", R.drawable.kot_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Ghost Vape Heliopolis", R.drawable.ghost_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Ghost Vape", R.drawable.ghost_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("The Hub El Mohandeseen", R.drawable.hub_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("The Hub El Dokki", R.drawable.hub_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("The Hub El Embaba", R.drawable.hub_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Mashro3 Mazah", R.drawable.mashro3_mazag_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Cloud House", R.drawable.cloud_house_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Cloud Cafe & Restaurant", R.drawable.cloud_cafe_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("King Vape", R.drawable.king_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("City Clouds", R.drawable.city_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Fog Vape", R.drawable.fog_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("O2 Vape", R.drawable.o2_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Zag Vape", R.drawable.zagvap_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Awel Abbas 1", R.drawable.awel_abas_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Awel Abbas 2", R.drawable.awel_abas_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Roma Store October City ", R.drawable.roma_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Roma Store El Haram ", R.drawable.roma_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Miami Vape", R.drawable.miami_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
StoresAdapter adapter = new StoresAdapter(this, stores);
final ListView storesListView = (ListView) findViewById(R.id.stores_listView);
storesListView.setAdapter(adapter);
storesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Stores storeItem = stores.get(position);
// here where my two buttons clicks should be added //
}
});
}
这是包含适配器对象的布局xml
package com.voyage_eg.voyage;
public class Stores {
private int mStoreImageResourceId;
private String mStoreName;
private int mStoreAddressButton;
private int mStoreMobileButton;
// private String mStoreCall;
// private String mStoreMap;
public Stores(String storeName, int storeImageResourceId, int storeAddressButton, int storeMobileButton) {
mStoreName = storeName;
mStoreImageResourceId = storeImageResourceId;
mStoreAddressButton = storeAddressButton;
mStoreMobileButton = storeMobileButton;
// mStoreMap = storeMap;
// mStoreCall = storeCall;
}
public String getStoreName() {
return mStoreName;
}
public int getStoreImageResourceId() {
return mStoreImageResourceId;
}
public int getStoreAddressButton() {
return mStoreAddressButton;
}
public int getStoreMobileButton() {
return mStoreMobileButton;
}
//
// public String getStoreCall() {
// return mStoreCall;
// }
//
// public String getStoreMap() {
// return mStoreMap;
// }
}
答案 0 :(得分:0)
在storeAdapter中创建下面接口的列表视图中定义点击事件的界面..
package require
onCallMapClick onCallMapClick; public interface onCallButtonClick { void onCall(); //如果你想在需要时传递任何数据 } 公共接口onCallMapClick { void onMap(); //如果要在需要时传递任何数据 }
onCallButtonClick onCallButtonClick;
之后点击代码中使用的适配器中的按钮..
public void setOnCallButtonClick(StoresAdapter.onCallButtonClick onCallButtonClick) {
this.onCallButtonClick = onCallButtonClick;
}
public void setOnCallMapClick(StoresAdapter.onCallMapClick onCallMapClick) {
this.onCallMapClick = onCallMapClick;
}
之后,当您在下面的代码中使用的列表视图中绑定adpater以获取click事件时。
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onCallButtonClick.onCall();
}
});
mapButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onCallMapClick.onMap();
}
});
答案 1 :(得分:0)
初始化按钮后,您应该在适配器onClickListener
方法中添加getView()
callButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
不要忘记同时制作listview
项和button
clickable
,这样做。
让ListView
专注于android:focusable="true"
按钮无法在您的自定义列表视图项android:focusable="false"
答案 2 :(得分:0)
您所要做的就是通过在适配器上创建接口来单独处理每个按钮上的单击
商店适配器
public class StoresAdapter extends ArrayAdapter<Stores> {
OnclickListener mListener = null ;
public interface OnclickListener {
void onMapButtonClick(int position);
void onCallButtonClick(int position);
}
public StoresAdapter(Context context, ArrayList<Stores> stores, OnclickListener listener) {
super(context, 0, stores);
mListener = listener ;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// super.getView(position, convertView, parent);
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.dist_layout, parent, false);
}
Stores currentStore = getItem(position);
Stores currentStores = getItem(position);
TextView storeName = (TextView) listItemView.findViewById(R.id.storeName);
storeName.setText(currentStores.getStoreName());
ImageView storeImage = (ImageView) listItemView.findViewById(R.id.storeImage);
storeImage.setImageResource(currentStores.getStoreImageResourceId());
ImageButton callButton = (ImageButton) listItemView.findViewById(R.id.callButton);
callButton.setImageResource(currentStores.getStoreMobileButton());
ImageButton mapButton = (ImageButton) listItemView.findViewById(R.id.mapButton );
mapButton.setImageResource(currentStores.getStoreAddressButton());
mapButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onMapButtonClick(position);
}
});
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onCallButtonClick(position);
}
});
return listItemView;
}
}
在您的活动中,您必须将适配器的初始化更改为此
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.distribution_points);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
cartPadgeNumber=findViewById(R.id.cartPadgeNumber);
///////////////////////////////////////////////////////////
// Create an ArrayList of AndroidFlavor objects
final ArrayList<Stores> stores = new ArrayList<Stores>();
stores.add(new Stores("El Badry Serag Mall", R.drawable.badry_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("El Badry Maadi", R.drawable.badry_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("El Badry Hurghada", R.drawable.badry_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Dose Mart Nasr City", R.drawable.dose_nasr_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Dose Mart Heliopolis", R.drawable.dose_hel_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Dose Mart Maadi", R.drawable.dose_maadi_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("ADAMS", R.drawable.adams_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("First Exit Maadi", R.drawable.first_exit_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("First Exit New Cairo", R.drawable.first_exit_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Mahala Vape Club", R.drawable.mahala_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("El Kot Vape El Zamalik", R.drawable.kot_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Ghost Vape Heliopolis", R.drawable.ghost_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Ghost Vape", R.drawable.ghost_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("The Hub El Mohandeseen", R.drawable.hub_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("The Hub El Dokki", R.drawable.hub_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("The Hub El Embaba", R.drawable.hub_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Mashro3 Mazah", R.drawable.mashro3_mazag_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Cloud House", R.drawable.cloud_house_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Cloud Cafe & Restaurant", R.drawable.cloud_cafe_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("King Vape", R.drawable.king_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("City Clouds", R.drawable.city_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Fog Vape", R.drawable.fog_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("O2 Vape", R.drawable.o2_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Zag Vape", R.drawable.zagvap_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Awel Abbas 1", R.drawable.awel_abas_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Awel Abbas 2", R.drawable.awel_abas_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Roma Store October City ", R.drawable.roma_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Roma Store El Haram ", R.drawable.roma_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
stores.add(new Stores("Miami Vape", R.drawable.miami_logo, R.drawable.ic_call_black_24dp, R.drawable.ic_directions_black_24dp));
final ListView storesListView = (ListView) findViewById(R.id.stores_listView);
StoresAdapter adapter = new StoresAdapter(this, stores, new StoresAdapter.OnclickListener() {
@Override
public void onMapButtonClick(int position) {
Toast.makeText(MainActivity.this, "this is click on map "+position,Toast.LENGTH_LONG).show();
}
@Override
public void onCallButtonClick(int position) {
Toast.makeText(MainActivity.this, "this is click on call "+position,Toast.LENGTH_LONG).show();
}
});
final ListView storesListView = (ListView) findViewById(R.id.stores_listView);
storesListView.setAdapter(adapter);
}
}
希望这有用:)