How to capture click events on different views in a list item?

I have a list view with an image view and text in a linear layout. I want to perform different operations when user clicks on the Image view and the Text view. I have tried onItemClickListener but it's capturing on the whole linear layout. I tried onClickListener in custom adapter, but were unable to get its position.

How can I resolve this problem?

Answers


Here i m sharing my code.i have three Textviews in each list item and on one of them i put a listener.This code is for reference.Learning from the below code U can put listeners on different list items.Now i hope u can write code as u want.

static class ViewHolder {
    public TextView tv1,tv2,tv3;

        }

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = null;
    final ViewHolder holder = new ViewHolder();
    if (convertView == null) {

        LayoutInflater inflator = context.getLayoutInflater();
        rowView = inflator.inflate(R.layout.attach_pic, null);

        holder.tv1 = (TextView) rowView.findViewById(R.id.defectpic);
        holder.tv2 = (TextView) rowView.findViewById(R.id.no_of_uploded_pics);
        holder.tv3 = (TextView) rowView.findViewById(R.id.camera);

        holder.tv3.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub  

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(TAG, position);
                context.startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

            }
        });

        rowView.setTag(holder);
        List itemVal1 = (List)getItem(position);
        String st1 = (String)itemVal1.get(0);
        holder.tv1.setText(st1);

        List itemVal2 = (List)getItem(position);
        String st2 = (String)itemVal2.get(1);
        holder.tv2.setText(st2);

    } else {
        rowView = convertView;
        ((ViewHolder) rowView.getTag()).tv1.setTag(list.get(position));
        ((ViewHolder) rowView.getTag()).tv2.setTag(list.get(position));
        ((ViewHolder) rowView.getTag()).tv3.setTag(list.get(position));
    }

    return rowView;
}

set tag for each view you are going to click as its position & get that using gettag


Firstly make both your textview and the imageview clickable and focusable.

Secondly Inside Your adapter class within getView add events to both textview and imageview.


Need Your Help

SharedPreferences not working

java android string sharedpreferences

I'm saving two strings fro another activity, when the user logs in, so I can save their data to use another time (Ease of use for the end user). Anyway, I've set up the code and ran it and the stri...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.