如何使listview可点击?我尝试了很多方法,但我仍然不明白

时间:2014-01-03 05:36:17

标签: android listview android-listview clickable simpleadapter

所以我有一个simpleadapter使用listadapter,行将显示,数据在那里,但listview不可点击!我错过了任何代码或东西吗?请帮忙!我需要listview才能使用intent转到另一个页面。任何帮助表示赞赏!

    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);

                     }
                 });

这是listview的以下xml文件;对不起代码的混乱!

     <?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 个答案:

答案 0 :(得分:0)

    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);


        }

答案 1 :(得分:-1)

使用Listview项目单击侦听器

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

答案 2 :(得分:-1)

而不是toast msg添加意图代码....一切都会好的

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();
}
});
相关问题