我有一个班级public class StoreListViewActivity extends Activity implements
OnItemClickListener
有一个方法public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
,我不认为我有用,因为这是一个整个区域的监听器。我想要的是让按钮具有不同的听众和图像来代替按钮的位置,但是通过我的实现(onBuyButtonClick方法),我只有一个监听器和所有其他“购买!”按钮都使用相同的监听器,因为我使用Button bb = (Button) findViewById(R.id.buy_button);
所以它们都是相同的按钮。我想要的是:
---------------------------------
[image] Fruit Ninja is..... [Buy!]
- &gt;购买按钮有自己的倾听者(即减少余额)
---------------------------------
[image] Subway Surfers is.. [Buy!]
- &gt;购买按钮有自己的听众(即带你去商店)
---------------------------------
如果可能的话,我可以用我的实施代替那个购买按钮的图像吗?我在ListView的右侧有一个ButtonView,当我尝试在那里添加一个ImageView时,应用程序崩溃
每个列表右侧都有一个具有不同功能的按钮。在我的StoreListViewActivity
课程中,我有对象:
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!" };
这些对象进入ArrayList并在onCreate方法的ListView中显示:
// 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);
}
供参考,这是我的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>
这是我的适配器:
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;
}
}
答案 0 :(得分:1)
你可以做这样的事情
在getView
方法中,为每个按钮设置tag
,以便稍后在单击按钮时识别该按钮。
holder.buttonView.setTag(storeRowItem.getTitle());
点击该按钮后,点击该按钮的tag
,然后根据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
}
}
});
以下是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;
}
}