0

My Listview app gets its data and background color of itemview from custom adapter ListAdapter.class.i also need to set the currently selected list items value in a textview below listview,but the setOnItemClickListener in MAinActivity is not executing.pls help.

This is my list view app:

Layout image

MainActivity.java

public class MainActivity extends Activity {
private static ListAdapterclass adapter;
    ListView lv;
    TextView tv2;
    private  final String android_versions[]={
            "Donut",
            "Eclair",
            "Froyo",
            "Gingerbread",
            "Honeycomb",
            "Ice Cream Sandwich",
            "Jelly Bean",
            "KitKat",
            "Lollipop",
            "Marshmallow"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }
    private  void initViews() {
        lv = (ListView) findViewById(R.id.listView1);
        tv2 = (TextView) findViewById(R.id.selected);

        adapter = new ListAdapterclass(getApplicationContext(), android_versions);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), "hiiiiiiiii", Toast.LENGTH_SHORT).show();
                System.out.println("********************** INSIDE  ONITEMCLICKLISTNER IN MAIN ACTIVITY ******************");
                String ver_name = (lv.getItemAtPosition(i)).toString();


                tv2 = (TextView) findViewById(R.id.selected);
                tv2.setText(ver_name);
            }
        });
    }
}

ListAdapter.class

public class ListAdapterclass extends ArrayAdapter implements AdapterView.OnItemClickListener{

    private String android_versionNames[];
    Context mContext;
    public int row_index=-1;

    @Override
    public void onItemClick(AdapterView<?> adapterView, View v, int i, long l) {
        int position=(Integer)v.getTag();
        String ver_name=getItem(position).toString();
    }

    private static class ViewHolder{
        TextView tv;
       LinearLayout LL;
        TextView tv2;
    }



    public ListAdapterclass(Context context,String android_versionnames[]) {
        super(context, R.layout.list_item,android_versionnames);
        this.android_versionNames=android_versionnames;
        this.mContext=context;

        System.out.println(" ????????????????????????  Inside dataadapter,Android names :  ?????????????????????????????\n  ");
        for(int i=0;i<android_versionnames.length;i++){
            System.out.println("\n"+android_versionnames[i]);
        }

    }
    private int lastPosition=-1;

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

        String ver_name=getItem(position).toString();

        final ViewHolder viewHolder;
        final View result;

        if(convertView==null){
            viewHolder=new ViewHolder();
            LayoutInflater inflater=LayoutInflater.from(getContext());
            convertView=inflater.inflate(R.layout.list_item,parent,false);
            viewHolder.tv=(TextView)convertView.findViewById(R.id.label);
            viewHolder.LL=(LinearLayout) convertView.findViewById(R.id.linearLayout_1);
            viewHolder.tv2=(TextView)convertView.findViewById(R.id.selected);
            result=convertView;
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
            result=convertView;
        }
        lastPosition=position;

        viewHolder.tv.setText(ver_name);

        viewHolder.LL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                row_index=position;
                notifyDataSetChanged();
            }
        });
        if(row_index==position){
            viewHolder.LL.setBackgroundColor(Color.parseColor("#409de1"));
            viewHolder.tv.setTextColor(Color.parseColor("#ffffff"));
        }
        else
        {
            viewHolder.LL.setBackgroundColor(Color.parseColor("#ffffff"));
            viewHolder.tv.setTextColor(Color.parseColor("#000000"));

        }
        return convertView;
    }
}

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical"
    tools:context="com.cybraum.test.listviewcolorchange.MainActivity"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
android:clickable="true"
    android:layout_weight="1"
    >
 <ListView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/listView1"
     >
 </ListView>


</LinearLayout>

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight=".2"
     android:id="@+id/linearLayout_2"
     android:orientation="horizontal"

     >
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Selected : "
      android:textStyle="bold"
      android:layout_gravity="center"/>
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textStyle="bold"
      android:text="text"
      android:id="@+id/selected"
      android:layout_gravity="center"/>

 </LinearLayout>

</LinearLayout>

listitem.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/linearLayout_1"
    android:padding="10dp">
<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:textColor="#000000"
    android:gravity="center">
</TextView>
</LinearLayout>

what is the problem?

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
c0der
  • 2,751
  • 2
  • 14
  • 21

6 Answers6

1

If you take click event from adapter then listview itemclick could not work if you need adapter click event and listview item click please refer the link, How to make imageView clickable from OnItemClickListener?

  @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    long viewId = view.getId();

    if (viewId == R.id.button1) {
        Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
    } else if (viewId == R.id.button2) {
        Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "ListView clicked" + id, Toast.LENGTH_SHORT).show();
    }
}

In adapter:

viewHolder.Btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
Community
  • 1
  • 1
Vadivel
  • 780
  • 1
  • 6
  • 21
1

Remove viewHolder.LL.setOnClickListener listener from adapter and In your adapter add a method to update row_index:

public void changeIndex(int rowIndex){
this.row_index = rowIndex;
notifyDataSetChanged();
}

Call this method from onItemClickListener event:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.changeIndex(i);//This will give you the same result of viewHolder.LL.setOnClickListener as you are doing
                //Do whatever you are doing previously
            }
        }); 
Ready Android
  • 3,529
  • 2
  • 26
  • 40
0

Use:

 lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), "hiiiiiiiii", Toast.LENGTH_SHORT).show();
                System.out.println("********************** INSIDE  ONITEMCLICKLISTNER IN MAIN ACTIVITY ******************");
                String ver_name = (lv.getItemAtPosition(i)).toString();


                tv2 = (TextView) findViewById(R.id.selected);
                tv2.setText(ver_name);
            }
        });

And from adapter remove

implements AdapterView.OnItemClickListener

Arun kumar
  • 1,894
  • 3
  • 22
  • 28
  • See Above answer that may useful to you,Because we can take onClicke vent and itemclick from Listview – Vadivel Nov 22 '16 at 07:01
0

You need to remove your adaptor from the setOnClickListner() Try to change Your method with

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
   Object o = prestListView.getItemAtPosition(position);
   prestationEco str=(prestationEco)o;//As you are using Default String Adapter
   Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
    }
});
Pushpendra
  • 1,492
  • 1
  • 17
  • 33
0

change listview xml

    <ListView

         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:clickable="true"   
         android:focusable="true">

 </ListView>

remove AdapterView.OnItemClickListener from adapter class

public class ListAdapterclass extends ArrayAdapter {

}

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

change listview xml

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"></ListView>

remove AdapterView.OnItemClickListener from adapter class

public class ListAdapterclass extends ArrayAdapter {

}

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37