我在ListView中创建了一个懒惰的图像负载。我遵循了this source的教程,我在Stack Overflow中发现它运行成功。
但是,当我将代码与我的项目结合在一起时,我就会面临一个问题。程序没有执行OnItemClickListener :(
我的项目有一个TabHost,它有5个标签内容。2内容是使用ListActivity并完美运行的。
这是我的代码,Main.java:
public class ProductListing extends Activity {
ListView list;
MyListAdapter adapter;
Controller c;
ImageLoader imageLoader;
TextView select;
//========== JSON ===========
ArrayList<String> strName = new ArrayList<String>();
ArrayList<String> strImage = new ArrayList<String>();
ArrayList<String> strDesc = new ArrayList<String>();
ArrayList<String> strSize = new ArrayList<String>();
JSONObject jsonObject;
String[] listItem;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LoadJSON();
setContentView(R.layout.productlisting_tab);
list=(ListView)findViewById(R.id.ListView01);
c = new Controller(this);
adapter=new MyListAdapter(this,this, strName, strImage,strDesc,strSize);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
System.out.println("Item Clicked");
}
});
}
public void LoadJSON(){
try {
InputStream is = this.getResources().openRawResource(R.raw.premium);
byte[] buffer;
buffer = new byte[is.available()];
while(is.read(buffer) != -1);
String jsonText = new String(buffer);
jsonObject = new JSONObject(jsonText);
JSONObject premium_tab = jsonObject.getJSONObject("premium_tab");
int totalItem = premium_tab.getInt(".total");
for (int i = 1; i <= totalItem; i++) {
JSONObject premium = premium_tab.getJSONObject("premium_"+i);
String tempName =premium.getString(".name").toString();
String tempImg = premium.getString(".image").toString();
String tempDesc = premium.getString(".desc").toString();
String tempSize = premium.getString(".size").toString();
strName.add(tempName);
strImage.add(tempImg);
strDesc.add(tempDesc);
strSize.add(tempSize);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}MyListAdapter.java:
public MyListAdapter(Context b,Activity a, ArrayList<String> strName, ArrayList<String> strImage,
ArrayList<String> strDesc, ArrayList<String> strSize) {
activity = a;
name = strName;
image = strImage;
desc = strDesc;
size = strSize;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return image.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView ProductName,ProductSize, ProductDesc;
public ImageView ProductIcon;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.productlisting, null);
holder=new ViewHolder();
holder.ProductName=(TextView)vi.findViewById(R.id.text);
holder.ProductIcon=(ImageView)vi.findViewById(R.id.image);
holder.ProductDesc=(TextView)vi.findViewById(R.id.textdesc);
holder.ProductSize=(TextView)vi.findViewById(R.id.textsize);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.ProductName.setText(name.get(position));
holder.ProductDesc.setText(desc.get(position));
holder.ProductIcon.setTag(image.get(position));
holder.ProductSize.setText(size.get(position));
imageLoader.DisplayImage(image.get(position), activity, holder.ProductIcon);
return vi;
}
}另一个名为ImageLoader.java的类,请参考上面的源代码链接。我可以知道我的错误在哪里吗?我知道我的代码会很难看,我是android的新手,请帮我解决这个问题。它卡住了我好几天。非常感谢您的回复!
我为我糟糕的英语感到抱歉,希望你们能理解我在说什么。谢谢。
关于Wynix
发布于 2010-08-05 12:58:43
我已经解决了这个问题并解决了它。错误在xml文件上。在ListView中不应该有
android:focusable="true";方法。
无论如何,谢谢你试着解决我的问题。再次感谢。干杯!
关于Wynix
发布于 2010-08-03 16:59:23
我使用一种不同的技术来添加事件侦听器。在OnCreate-method中,我编写了btnAdd.setOnClickListener(onAdd);,并添加了一个独立的方法来连接到事件,如下所示:
private View.OnClickListener onAdd=new View.OnClickListener() {
public void onClick(View v) {
// your code here
}
};这使得搜索代码中的错误变得更容易。
在您的代码中,将event-listener设置为整个列表,而不是每个单独的项。也许你应该尝试将事件添加到单个项目中?
https://stackoverflow.com/questions/3394832
复制相似问题