0

我正在 ListView 中创建图像的延迟加载。我遵循了这个来源的教程,我在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 新手,请帮我解决问题。它困扰了我好几天。非常感谢您的回复!!!!

P/S:对不起,我的英语不好,希望你们明白我在说什么。谢谢你。

关注威力士

4

2 回答 2

0

我使用了一种不同的技术来添加事件监听器。在 OnCreate 方法中,我编写btnAdd.setOnClickListener(onAdd);并添加了一个独立的方法来连接到这样的事件:

private View.OnClickListener onAdd=new View.OnClickListener() {
    public void onClick(View v) {
        // your code here
    }
};

这使得在代码中搜索错误变得更加容易。

从您的代码中,您将事件侦听器设置为整个列表,而不是每个单独的项目。也许您应该尝试将事件添加到单个项目?

于 2010-08-03T08:59:23.070 回答
0

我已经解决了问题并解决了。错误在 xml 文件上。在 ListView 中不应该有

android:focusable="true";方法。

无论如何感谢您尝试解决我的问题。再次感谢。干杯!

关注威力士

于 2010-08-05T04:58:42.570 回答