gpt4 book ai didi

java - OnItemClickListener 无法识别

转载 作者:行者123 更新时间:2023-12-01 23:29:25 24 4
gpt4 key购买 nike

啊,我在这里做错了什么我已经为 ListView 和 onClick 设置了 onItemClickListener将它带到不同的 Activity ,但它告诉我它必须使用 OnItemClicklistener 方法,我有...

package org.iimed.www;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import org.iimed.www.R;
import org.iimed.www.CustomListViewAdapter;
import org.iimed.www.RowItem;

public class ImageTextListViewActivity extends Activity implements
OnItemClickListener {

public static final String[] titles = new String[] { "Penicillins",
"Cephalosporins", "Monobactams", "Carbapenems","Macrolides","Tetracyclines",};

public static final String[] descriptions = new String[] {
"It is an aggregate accessory fruit",
"It is the largest herbaceous flowering plant", "Citrus Fruit",
"Mixed Fruits","none","none" };

public static final Integer[] images = { R.drawable.antibiotic_buton,
R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,R.drawable.antibiotic_buton, };

ListView listView;
List<RowItem> rowItems;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screj);

rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);


listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
{
Intent intent = null ;
switch (arg2) {
case 0:
intent = new Intent(ImageTextListViewActivity.this, MainActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(ImageTextListViewActivity.this, Miipet.class);
startActivity(intent);
break;
default:
break;



}}}}

适配器:

package org.iimed.www;


import java.util.List;
import org.iimed.www.R;
import org.iimed.www.RowItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

Context context;

public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}


private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());

return convertView;
}
}

最佳答案

像这样点击注册:

 lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub

Intent intent = new Intent(getApplicationContext(), ImageGridActivity.class);

startActivity(intent);
}
});

关于java - OnItemClickListener 无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19558124/

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