How do i make my listview clickable? I tried many ways but i still do not understand

132 Views Asked by At

So i have a simpleadapter using the listadapter, the rows will show, the data is there, but the listview is not clickable! Did i miss out any codes or something? Please help! I need the listview to be able to go to another page using intent. Any help given is appreciated!

    ListAdapter adapter = new SimpleAdapter(
                        AllFTemployeesActivity.this, employeesList,
                        R.layout.list_item_ft, new String[] { TAG_PID,
                                TAG_NAME,TAG_DESCRIPTION},
                        new int[] { R.id.pid, R.id.name, R.id.description});

                // updating listview
                setListAdapter(adapter);

                ListView lv = getListView();
                lv.setTextFilterEnabled(true);


                //inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

               //  View header = inflater.inflate(R.layout.planets_heade_view, null);

               // lv.addHeaderView(header);

                 // on seleting single fulltime employee
                 // launching Edit single fulltime employee Screen
                lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                     public void onItemClick(AdapterView<?> parent, View view,
                             int position, long id) {
                         // getting values from selected ListItem
                       //  String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                        //         .toString();


                         // Starting new intent
                         Intent i = new Intent(getApplicationContext(),
                                 EmployeePayslip.class);
                         // sending pid to next activity
                       //  in.putExtra(TAG_PID, pid);

                         // starting new activity and expecting some response back
                         startActivity(i);

                     }
                 });

this is the following xml file of the listview; Sorry for the messiness of the code!

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" 
      android:weightSum="100" 
      android:background="#ffffff"
      android:clickable="true">
     <LinearLayout   
     android:clickable="true"
     android:orientation="horizontal"
     android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="5"
    android:paddingBottom="10dp"

    >
      <TextView  android:id="@+id/pid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:focusable="false"
    android:focusableInTouchMode="false"
      />

         <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </View>

 </LinearLayout>

    <LinearLayout   
     android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="20"
    android:paddingBottom="10dp" >


     <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/thorb"/>

    </LinearLayout>

 <!-- text1 and text2 -->
<LinearLayout   
    android:layout_width="0dp"
     android:layout_height="match_parent"
    android:layout_weight="60"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    >



     <LinearLayout   
    android:layout_width="match_parent"
     android:layout_height="match_parent"
    android:weightSum="100"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:orientation="vertical">

           <LinearLayout   
     android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"
    android:paddingBottom="10dp"

    >

 <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </View>

   </LinearLayout>

             <LinearLayout   
    android:layout_width="match_parent"
     android:layout_height="0dp"
    android:layout_weight="20"
    >
        <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_gravity="center"
        android:textColor="@android:color/black"
        />
        </LinearLayout>

         <!-- text1 -->
         <LinearLayout   
    android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="20"
    >

              <TextView  android:id="@+id/pid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"/>



     </LinearLayout>



     <!-- text2 -->
          <LinearLayout   
    android:layout_width="match_parent"
     android:layout_height="0dp"
    android:layout_weight="20"
    >
        <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:textColor="@android:color/black"

        />
        </LinearLayout>




           <LinearLayout   
     android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"
    android:paddingBottom="10dp"

    >

     <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </View>

    </LinearLayout>


        </LinearLayout>

     </LinearLayout>





    <!-- image2 -->
   <LinearLayout   
     android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="10"
    android:paddingBottom="10dp"

    >


     <ImageView
        android:id="@+id/img1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:scaleType="fitXY"
     android:adjustViewBounds="true"
     android:layout_gravity="center"
     android:src="@drawable/plushourslight"
    />

    </LinearLayout>

    <!-- space -->
     <LinearLayout   
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="5"
     android:paddingBottom="10dp"

    >

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </View>

    </LinearLayout>

    </LinearLayout>
3

There are 3 best solutions below

1
On

Use Listview item click listener

getListView().setOnItemClickListener(new OnItemClickListener() {    
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        // your code is here on item click
    }
});
0
On

just instead of toast msg add intent code....and everything will be fine

ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View
view,int position, long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView)
view).getText(),Toast.LENGTH_SHORT).show();
}
});
1
On
    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,a);
    sp.setAdapter(adapter);
    sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub


                Toast.makeText(getBaseContext(), "Redirecting..."+a[arg2], Toast.LENGTH_LONG).show();
                Intent i = new Intent(getBaseContext(),EmployeePayslip.class);
                startActivity(i);


        }