Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

android - Set OnClickListener for items in ListView with 2 Views

I have a ListView, with items with this layout:

<LinearLayout ...>            
    <LinearLayout ...>
       <!--Elements -->
    </LinearLayout>

    <LinearLayout ...>
       <!--Elements -->
    </LinearLayout>            
</LinearLayout>

So my items have two different sections. I want to be able to setup different onclickListener inside each item(one for each LinearLayout). I have tried so far to override the onListItemClick, but it doesnt seem to work properly:

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id);

    ViewGroup vg = (ViewGroup)v;
    vg.getChildAt(0).setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
            Toast.makeText(mCtx, "element1", Toast.LENGTH_SHORT).show();                
        }
    });
    vg.getChildAt(1).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(mCtx, "element2", Toast.LENGTH_SHORT).show();                
        }
    });

}

Any ideas how to tackle the problem?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use set onclicklistener to all elements inside ListView. I have created a listview. set an adapter to the listview. My adapter is reponsible to show each element of the listview. so inside getView() I will assign onclicklistener to all the elemnts to whom i want to get the click event.

Example

public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.text);;
            holder.image=(ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();

        holder.text.setText("item "+position);
        holder.image.setTag(data.get(position));
        holder.image.setOnClickListener(this);
        holder.text.setOnClickListener(this);
        imageLoader.DisplayImage(data.get(position), activity, holder.image);
        return vi;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        System.out.println("...clicked view is...."+arg0);
    }

Thanks Deepak


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...