Search code examples
androidlistviewandroid-custom-viewcustom-adapter

ListView in android is not highlighting the row i am selecting


ListView is not showing any effect when i tap a row in ListView i want to show some effect or any colour etc like in iOS when we select or tap on a row in i am using custom view and a custom adapter for ListView and infalting a custom view

here i am creating a custom adapter with custom view

CustomContactsHomeAdapter customContactsAdapter = new CustomContactsHomeAdapter(HomeView.this, R.layout.custom_contact_cell, userBean_home_Search.getData(), 
listView.setAdapter(customContactsAdapter);

And this is my custom adapter

public class CustomContactsHomeAdapter extends ArrayAdapter<UserBean_Home.DataBean> {

    public UserBean_Home userBean;
    Context mContext;

    public CustomContactsHomeAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public CustomContactsHomeAdapter(Context context, int resource, List<UserBean_Home.DataBean> need, UserBean_Home abc) {
        super(context, resource , need);
        this.mContext = context;
        this.userBean = abc;
    }

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

        if(convertView==null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(mContext);
            convertView = vi.inflate(R.layout.custom_contact_cell, null);

            TextView contactName = (TextView) convertView.findViewById(R.id.contactName);
            TextView contactDesc = (TextView) convertView.findViewById(R.id.contactAddress);
            ImageView profileImage = (ImageView) convertView.findViewById(R.id.profileImage);

            UserBean_Home.DataBean dataBean = userBean.getData().get(position);

            if(dataBean.getR_type().equalsIgnoreCase("1")) {
                contactName.setText(dataBean.getFull_name());
                contactDesc.setText("@"+dataBean.getUser_name());
                Picasso.with(mContext)
                        .load(GlobalBean.IMAGES_URL+dataBean.getImage())
                        .placeholder(R.drawable.noimage)
                        .into(profileImage);
            } else {

                contactName.setText(dataBean.getGroup_title());
                contactDesc.setText("@"+dataBean.getGroup_description());
                Picasso.with(mContext)
                        .load(GlobalBean.IMAGES_URL+dataBean.getGroup_cover_image())
                        .placeholder(R.drawable.noimage)
                        .into(profileImage);
            }
//            if(dataBean.getUnread_messages().equalsIgnoreCase("0") == false) {
//                TextView undreadMessages = (TextView) convertView.findViewById(R.id.undreadMessages);
//                undreadMessages.setVisibility(View.VISIBLE);
//                undreadMessages.setText(dataBean.getUnread_messages());
//            }
    }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    @Override
    public int getItemViewType(int position) {
        return 
   }

and this is ListView in XML

<ListView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/listView"
     android:layout_below="@id/L1 />

anyone please ??


Solution

  • just use

    listview.setOnItemClickListener

    after setting the adapter like bellow

        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override 
            public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 
                Toast.makeText(yourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                arg1.setBackgroundColor(Color.GREEN); 
            }
        });