这是fragement的onCreateView函数
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = (View) inflater.inflate(R.layout.fragment_club, container, false);
listView = (ListView) v.findViewById(R.id.list);
c = new CustomListAdapter(this, clubList);
listView.setAdapter(c);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i("Infox", "KRIM");
}
});
return v ;
}
这是我的自定义适配器:
public class CustomListAdapter extends BaseAdapter {
private Fragment fragment;
private LayoutInflater inflater;
private List<Club> cItems;
ImageView img;
Context context;
ListView lv;
public CustomListAdapter(Fragment fragment, List<Club> cItems) {
this.fragment = fragment;
this.cItems = cItems;
}
@Override
public int getCount() {
return cItems.size();
}
@Override
public Object getItem(int location) {
return cItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src", src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
context = fragment.getActivity();
inflater = (LayoutInflater) LayoutInflater.from(context);
}
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
img = (ImageView) convertView.findViewById(R.id.thumbnail);
TextView nomC = (TextView) convertView.findViewById(R.id.title);
TextView slogan = (TextView) convertView.findViewById(R.id.slogan);
TextView descipline = (TextView) convertView.findViewById(R.id.desc);
Button join = (Button) convertView.findViewById(R.id.joinB);
View v = (View) inflater.inflate(R.layout.fragment_club,parent,false);
lv = (ListView) v.findViewById(R.id.list);
// getting movie data for the row
final Club m = cItems.get(position);
//Set the button click event
join.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context,
m.getNomC(), Toast.LENGTH_LONG).show();
}
});
/* lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i("Infox", "KRIM");
}
});*/
// thumbnail image
img.setImageBitmap(getBitmapFromURL(m.getThumbnailUrl()));
// Nom du club
nomC.setText(m.getNomC());
// Slogan Club
slogan.setText(m.getSlogan());
//Disciplines des clubs
descipline.setText(m.getDesc());
return convertView;
}
}
PS:就像你在评论代码中看到的那样,我试图在我的自定义适配器中做到这一点,但我认为这是一个绝望的操作答案 0 :(得分:1)
如果您想要实现项目点击以及按钮单击,则执行相同的操作以转换为加入按钮。暗示onClickListener转换为。
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("Infox", "KRIM");
}
});
答案 1 :(得分:1)
您应该从适配器中删除onclick,之后您应该使用setOnItemClickListener