OnItemClickListener 问题

发布于 2024-09-12 10:05:30 字数 4791 浏览 2 评论 0原文

我正在 ListView 中创建图像的延迟加载。 我遵循了 此来源 中的教程,我在 堆栈溢出 运行成功。

但是,当我将代码与我的项目结合在一起时,我遇到了一个问题。该程序没有执行 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:我很抱歉我的英语不好,希望你们明白我在说什么。 谢谢。

看待 维尼克斯

i'm creating a lazy load of image in ListView.
i was followed the tutorial from this source which i found in Stack Overflow
It was run successful.

But, when i join the code together with my project then i face a problem. the program was no perform the OnItemClickListener :(

my project have a TabHost and it had 5 tab contents. 2 contents is using the ListActivity and run perfectly.

here is my coding, 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;
  }    
}

Another class which name ImageLoader.java please refer the source link above.
May i know where is my mistake? i understand my code will very ugly, i'm a new in android please help me to solve the problem. it was stuck me for few days.
your reply is very appreciated !!!

P/S: I'm sorry about my bad english, hope you guys understand what i'm talking about.
Thank you.

Regard
Wynix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-09-19 10:05:30

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

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

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

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

I use a different technique adding event-listeners. In the OnCreate-method i write btnAdd.setOnClickListener(onAdd); and adding a stand-alone method to hook up to the event like this:

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

This makes it easier to search for errors in your code.

From your code, you set the event-listener to the entire list, instead of each individual item. Maybe you should try adding events to individual items instead?

并安 2024-09-19 10:05:30

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

android:focusable="true"; 方法。

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

关注维尼克斯

I have solve the problem and solve it. the erroe is on the xml file. in ListView should NOT have

android:focusable="true"; menthod.

anyway Thanks for trying to fix my problem.
Thanks again.
Cheers!

Regard Wynix

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文