我的应用在列表视图中显示3个字符串,其中包含3个文字视图。
我的问题是我在列表视图的布局上创建,我无法从 onListItemClick 方法的第一个文本视图中获取该项目。
如何实现这一目标?
ListView的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/outputlayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/ligthgrey">
<TextView
android:id="@+id/txOutputDeparture"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Abfahrt "
android:textColor="@color/black"
android:layout_alignParentLeft="true"
android:maxLength="@android:integer/config_shortAnimTime"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/txOutputDuration"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txOutputDeparture"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dauer"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/txOutputTransition"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txOutputDuration"
android:text="Umstieg"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="5dp" />
</RelativeLayout>
我创建并填充listView及其和ListActivity
的方法 public void getRoute() {
mdbH = new DatabaseHelperActivity(this);
cursor = mdbH.fetchallRoutes(intent.getStringExtra("StartHaltestelle"),intent.getStringExtra("ZielHaltestelle"), intent.getStringExtra("Zeit"));
ArrayList<DefineRouteActivity> route = new ArrayList<DefineRouteActivity>();
while(cursor.moveToNext()) {
route.add(new DefineRouteActivity(cursor.getString(0),cursor.getString(2),cursor.getString(4)));
}
ArrayAdapter<DefineRouteActivity> adapter = new RouteAdapterActivity(this, route);
setListAdapter(adapter);
}
AdapterActivity:
Activity context;
ArrayList<DefineRouteActivity> arraylist;
public RouteAdapterActivity(Activity context, ArrayList list) {
super(context,R.layout.outputlayout,list);
this.context = context;
arraylist = list;
}
@Override
public int getCount() {
return arraylist.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View contentview, ViewGroup viewGroup) {
DefineRouteActivity routeItems = arraylist.get(position);
LayoutInflater inflater = context.getLayoutInflater();
View view = inflater.inflate(R.layout.outputlayout,null);
TextView tvDeparture = (TextView)view.findViewById(R.id.txOutputDeparture);
tvDeparture.setText(routeItems.getAbfahrtszeit());
TextView tvDuration = (TextView)view.findViewById(R.id.txOutputDuration);
tvDuration.setText(routeItems.getDauer());
TextView tvTransition = (TextView)view.findViewById(R.id.txOutputTransition);
tvTransition.setText(routeItems.getUmstieg());
return view;
}
这是我的onClick方法:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
((View)v).
Intent detail = new Intent(getApplicationContext(),DetailOutputActivity.class);
detail.putExtra("StartStop",l.getItemAtPosition(0).toString());
detail.putExtra("EndStop","ich");
detail.putExtra("Time","du");
detail.putExtra("Route","er");
startActivity(detail);
}
这是我的包含listView的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
答案 0 :(得分:0)
您需要另一个包含listView的xml。
例如:sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
在你的mainActivity.java中:
setContentView(R.layout.sample.xml);
lv = (ListView)findViewById(R.id.lv1);
lv.setOnItemClickListener(this);
答案 1 :(得分:0)
您的listActivity必须实现OnItemClickListener。
然后实现onItemClick方法。例如:
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// TODO Auto-generated method stub
int id_object = ((object)a.getAdapter().getItem(position)).getIdInstalacion();
Intent i = new Intent(this, Other.class);
i.putExtra("id_", id_object);
startActivity(i);
}