我遇到问题,将OnClickListener设置为我的listview。 我想在点击不同的listview项目时打开不同的活动。但我无法完成它。任何帮助都是赞赏的。
我的活动
public class ListviewActivity extends AppCompatActivity {
private ListView listview;
int number = 1;
private int image;
private String title;
private String description;
private String price;
public int[] IMAGE = {R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white, R.drawable.white};
public String[] TITLE = {"A","B","C","D","E","F","G","H"};
public String[] DESCRIPTION = {"CEO", "CEO", "CEO", "CEO", "CEO", "CEO","CEO","CEO"};
private ArrayList<BeanClassForListView> beanClassArrayList;
private listViewAdapter listViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_listview);
listview = (ListView) findViewById(R.id.listview);
beanClassArrayList = new ArrayList<BeanClassForListView>();
for (int i = 0; i < TITLE.length; i++) {
BeanClassForListView beanClass = new BeanClassForListView(IMAGE[i], TITLE[i], DESCRIPTION[i]);
beanClassArrayList.add(beanClass);
}
listViewAdapter = new listViewAdapter(ListviewActivity.this, beanClassArrayList);
listview.setAdapter(listViewAdapter);
}
}
我的Listview适配器
public class listViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<BeanClassForListView> beanClassArrayList;
public listViewAdapter(Context context, ArrayList<BeanClassForListView> beanClassArrayList) {
this.context = context;
this.beanClassArrayList = beanClassArrayList;
}
@Override
public int getCount() {
return beanClassArrayList.size();
}
@Override
public Object getItem(int position) {
return beanClassArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHoder viewHoder;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listview, parent, false);
viewHoder = new ViewHoder();
viewHoder.banar1 = (ImageView) convertView.findViewById(R.id.banar1);
viewHoder.title = (TextView) convertView.findViewById(R.id.title);
viewHoder.description = (TextView) convertView.findViewById(R.id.description);
convertView.setTag(viewHoder);
} else {
viewHoder = (ViewHoder) convertView.getTag();
}
//NavigationItems rowItem = (NavigationItems) getItem(position);
BeanClassForListView beanClass = (BeanClassForListView) getItem(position);
viewHoder.title.setText(beanClass.getTitle());
viewHoder.description.setText(beanClass.getDescription());
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), beanClass.getImage());
viewHoder.banar1.setImageBitmap(getRoundedCornerBitmap(bitmap, 20));
// viewHoder.plus.setImageResource(beanClass.getImage());
// viewHoder.min.setImageResource(beanClass.getImage());
//viewHoder.no.setText(beanClass.getNo());
return convertView;
}
private class ViewHoder{
ImageView banar1;
TextView title;
TextView description;
}
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}}
BeanClassForListView
public class BeanClassForListView {
private int image;
private String title;
private String description;
public BeanClassForListView(int image, String title, String description) {
this.image = image;
this.title = title;
this.description = description;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}}
答案 0 :(得分:0)
你必须使用OnItemClickListener。
public class MyBaseController : Controller
{
[Inject]
public WallService WallService { get; set; }
// Other Services ..
}
答案 1 :(得分:0)
在您的活动中执行此操作:
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ItemClicked item = adapter.getItemAtPosition(position);
//Use switch case to call different activities for different cases
Intent intent = new Intent(Activity.this,destinationActivity.class);
startActivity(intent);
} });
答案 2 :(得分:0)
我不知道你为OnClickListener
设置View
的位置。在ListAdapter
getView()
内,您应该
View v = super.getView(position, convertView, parent);
if(v != null) {
v.setOnClickListener(new View.OnClickListener() {
Intent intent = new Intent(YourContext, ActivityToStart.class);
startActivity(intent);
});
}
我希望有所帮助。
答案 3 :(得分:0)
convertView = layoutInflater.inflate(R.layout.listview, parent, false);
在它下面,添加
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (position == 1){
getActivity().startActivity(new Intent(MainActivity.this,secondActivity.class));
}else if (position == 2){
getActivity().startActivity(new Intent(MainActivity.this,secondActivity.class));
}else if (position == 3){
getActivity().startActivity(new Intent(MainActivity.this,secondActivity.class));
}
}
});