Sampath Kumar
Sampath Kumar

Reputation: 4463

Android how to call onitemclicklisner from custom view onclicklisner?

I am customized an adapter for listview and its word fine. But when i set onclicklistener to an view in custom adapter row view the onitemclicklistner not working.

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    try{
        if(convertView==null)
        {
            convertView=mInflater.inflate(R.layout.marker_row_view, null);
            holder=new ViewHolder();
            holder.Name=(TextView)convertView.findViewById(R.id.ProeprtyName);
            holder.image=(ImageView)convertView.findViewById(R.id.RightArrow);
            convertView.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }

      holder.Name.setText(propertyNames[position]);
      if(selectedPosition == position){
          Log.d("", "selected");
          convertView.setBackgroundColor(Color.BLUE);
          convertView.setBackgroundColor(Color.parseColor("#3B79FF"));
          holder.Name.setTextColor(Color.WHITE);
      }else{
          //convertView.setBackgroundResource(R.drawable.savsearch_bg_district);
          holder.Name.setTextColor(Color.BLACK);
      }
      holder.Name.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("fdsf", "on");
                Globalclass global = (Globalclass) ((Activity)context).getApplication();
                global.setMarkerTextClick(true);
            }
      });
      holder.image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("fdsf", "on");
                Globalclass global = (Globalclass) ((Activity)context).getApplication();
                global.setMarkerTextClick(false);
            }
      });

    }catch (Exception e) {
        e.printStackTrace();
    }

    return convertView;
}

and the code for listview onitemclicklisner is

final MarkerInfoAdapter adapter = new MarkerInfoAdapter(PropertyMapList.this, names);
                                final ListView list = (ListView) CustomMarker.findViewById(R.id.listView1);
                                list.setAdapter(adapter);list.setOnItemClickListener(new OnItemClickListener() {

                                    @Override
                                    public void onItemClick(
                                            AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                                        Log.d("", "fsdfdsfds");
                                        ((MarkerInfoAdapter)adapter).setSelected(arg2);
                                    }
                                });

Upvotes: 0

Views: 204

Answers (2)

NaserShaikh
NaserShaikh

Reputation: 1576

you can try this:

convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               setSelected(position); 
            }
      });

Upvotes: 1

Rachita Nanda
Rachita Nanda

Reputation: 4659

Since you are adding the same code in in onClick listener ,there no point of using on click listener on row views.Instead you can add the following code in onItemClick

 Globalclass global = (Globalclass) ((Activity)context).getApplication();
   global.setMarkerTextClick(false);

If you still want to implement on click on specific views ,make sure

  • views are not clickable

  • android:focusable="false"

  • android:focusableInTouchMode="false"

Add the following attributes in your listview tag

 android:clickable="true"
 android:descendantFocusability="beforeDescendants"

This link will solve your problem Android: Grid view with clickable grid items and nested views (buttons, checkboxes)

Upvotes: 0

Related Questions