gpt4 book ai didi

android - 带有 BaseAdapter 的 Android ListView 中的 ImageButton

转载 作者:行者123 更新时间:2023-11-29 00:45:39 25 4
gpt4 key购买 nike

我已经实现了一个 BaseAdapter 并将该 BaseAdapter 绑定(bind)到我的 ListView。 ListView 每一行的布局都包含一个 ImageButton。我如何将该 ImageButton 绑定(bind)到点击监听器,然后使用该 ImageButton 调用新 Activity 。

最佳答案

这是一个示例适配器类。

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.amazon.mp3.AlbumDetail.Details;



public class CustomListAdapter extends BaseAdapter {
private ArrayList<Details> allElementDetails;
private Context con;
private LayoutInflater mInflater;
String temp;
public CustomListAdapter(Context context, ArrayList<Details> results) {
allElementDetails = results;
mInflater = LayoutInflater.from(context);
con=context;
}
public int getCount() {
return allElementDetails.size();
}
public Object getItem(int position) {
return allElementDetails.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
//select ur xml file
convertView = mInflater.inflate(R.layout.listview_elements, null);

TextView textview1 = (TextView) convertView.findViewById(R.id.TextView01);
TextView textview2 = (TextView) convertView.findViewById(R.id.TextView02);
TextView textview3 = (TextView) convertView.findViewById(R.id.TextView03);
Button buy=(Button)convertView.findViewById(R.id.buy_song_button);
buy.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent intent=new Intent(con,Buy_song.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

con.startActivity(intent);


}
else
Toast.makeText(con,"Not available for purchase", Toast.LENGTH_SHORT).show();

}
});

if(allElementDetails.get(position).songname.contains("&amp;"))
{
temp=allElementDetails.get(position).songname.replace("&amp;", "and");
textview1.setText(temp);
}
else
textview1.setText(allElementDetails.get(position).songname);

textview2.setText(allElementDetails.get(position).runtime);
if(allElementDetails.get(position).price.startsWith("$"))
textview3.setText(allElementDetails.get(position).price);
else
textview3.setText("Album only");
return convertView;
}

}

在此适配器中寻找购买按钮。

关于android - 带有 BaseAdapter 的 Android ListView 中的 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com