所以我有一个使用listadapter的简单适配器,将显示行,数据在那里,但列表视图不可点击!我错过了任何代码或其他东西吗?请帮忙!我需要列表视图才能使用意图转到另一个页面。任何帮助,不胜感激!
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);
}
});
这是列表视图的以下 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>
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);
}
使用列表视图项单击侦听器
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// your code is here on item click
}
});
只是代替 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();
}
});