4. mainactivity.java中的Demacre listview
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(mContext, "Postion no." +position, Toast.LENGTH_LONG).show();
}
这是数组适配器类
public class ActorAdapter extends ArrayAdapter<Actors> implements OnClickListener {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
int count=0;
public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
actorList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.ivheart = (ImageView) v.findViewById(R.id.iv_likeheart);
holder.tvadd=(TextView)v.findViewById(R.id.tv_add);
holder.tvreply=(TextView)v.findViewById(R.id.tv_reply);
holder.tvreplyadd=(TextView)v.findViewById(R.id.tv_replyadd);
holder.imageview = (ImageView) v.findViewById(R.id.iv_userpic);
holder.tvName = (TextView) v.findViewById(R.id.tv_username);
holder.tvDOB = (TextView) v.findViewById(R.id.tv_title);
holder.tvCountry = (TextView) v.findViewById(R.id.tv_description);
holder.ivheart.setOnClickListener(this);
holder.tvreply.setOnClickListener(this);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imageview.setImageResource(R.drawable.ic_launcher);
new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
holder.tvName.setText(actorList.get(position).getName());
holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
holder.tvCountry.setText(actorList.get(position).getCountry());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvDOB;
public TextView tvCountry;
public ImageView ivheart;
public TextView tvadd;
public TextView tvreply;
public TextView tvreplyadd;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_likeheart:
Toast.makeText(getContext(), "Hello", Toast.LENGTH_LONG).show();
break;
case R.id.tv_reply:
//not working settext here
//holder.tvadd.setText(count++);
// Toast.makeText(getContext(), "Hello" +count++, Toast.LENGTH_LONG).show();
//Intent i=new Intent(ActorAdapter.this, ReplyActivity.class);
break;
default:
break;
}
}
}
答案 0 :(得分:1)
试试这个: - 而是使用列表View的onItemClick使用convert-view on-click in adapter。
Get-Migrations
希望这会对你有所帮助。
答案 1 :(得分:0)
Jawed,您正在实现OnClickListener,但您应该使用OnItemClickListener。
请参阅下面的示例代码:
final ListView list = (ListView) findViewById(R.id.list);
ArrayList<String> arrayList = new ArrayList<String>();
String[] strings = new String[] {"A", "B", "C", "D", "E"};
for (int i=0; i<strings.length; i++) {
arrayList.add(i, strings[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
Object item = list.getItemAtPosition(i);
intent.putExtra("title", item.toString());
startActivity(intent);
}
});
此代码正常运作。