Bah,我在这里做错了我为列表视图和onClick设置了onItemClickListener 把它带到不同的活动,但它告诉我它必须使用方法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;
}
}
答案 0 :(得分:2)
点击这样注册:
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);
}
});
答案 1 :(得分:1)
我检查了你的代码,有几个问题
1)循环遍历元素并为每个元素创建适配器
2)你在onCreate
里面实现了onItemClick我粘贴了你的代码的修订版但是我用ObjectAdapter更改了RowItem和Object以及CustomListViewAdapter,所以在那里使用你的对象,但这应该是代码的结构:)
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.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
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<Object> rowItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screj);
rowItems = new ArrayList<Object>();
for (int i = 0; i < titles.length; i++) {
Object item = new Object();
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
ListAdapter adapter = new ListAdapter(this,R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
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;
}
}
}