ListActivity中使用ListView时出错
我在使用 ListActivity 时遇到问题。如果我将类扩展从 ListActivity 更改为 Activity 它工作正常。但问题是我将如何实现 onListItemClick。这可能是我犯的一些小错误,但我无法追踪它。
public class Warehouse extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Inside Warehouse");
setContentView(R.layout.warehous);
LinearLayout linear = (LinearLayout) findViewById(R.id.linearfullview);
drawable = setBitMap(background_image);
linear.setBackgroundDrawable(drawable);
String[] names = new String[] { "Inventory Ageing",
"Inventory Carring Cost", "Inventory Turns" };
LinearLayout linearList = (LinearLayout) findViewById(R.id.linearlist);
drawable = setBitMap(list_image);
linearList.setBackgroundDrawable(drawable);
this.setListAdapter(new ArrayAdapter<String>(this,R.layout.warehouse_list,names));
// ListView lv = getListView(); lv.setTextFilterEnabled(true);
}
public Drawable setBitMap(String imagename) {
Bitmap image = BitmapFactory.decodeStream(getClass()
.getResourceAsStream(("/com/image/" + imagename + ".png")));
drawable = new BitmapDrawable(image);
return drawable;
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_width="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/linearfullview" android:layout_height="400sp"
android:layout_alignParentTop="true" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="180sp" android:layout_alignParentBottom="true"
android:layout_marginTop="20sp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/linearlist" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp" android:gravity="bottom"
android:layout_marginTop="30dp">
<ListView android:id="@android:id/list" android:layout_width="100dip" android:layout_height="wrap_content"></ListView>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/linearFirst"
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:visibility="invisible">
<com.widget.WheelView
android:id="@+id/firstWheel" android:layout_height="wrap_content"
android:layout_width="100dp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/linearSecond"
android:layout_width="wrap_content" android:paddingLeft="2dp"
android:paddingRight="2dp" android:layout_alignParentRight="true"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:visibility="invisible">
<com.widget.WheelView
android:id="@+id/secondWheel" android:layout_height="wrap_content"
android:layout_width="100dp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/linearThird"
android:layout_width="wrap_content" android:paddingLeft="2dp"
android:paddingRight="2dp" android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:layout_alignParentLeft="true"
android:visibility="invisible">
<com.widget.WheelView
android:id="@+id/thirdWheel" android:layout_height="wrap_content"
android:layout_width="200dp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
I am having trouble using ListActivity. If i change class extend from ListActivity to Activity it works fine. But then the trouble is how will i implement the onListItemClick. It muight be some small mistake i might have done but i am not able to trace it.
public class Warehouse extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("Inside Warehouse");
setContentView(R.layout.warehous);
LinearLayout linear = (LinearLayout) findViewById(R.id.linearfullview);
drawable = setBitMap(background_image);
linear.setBackgroundDrawable(drawable);
String[] names = new String[] { "Inventory Ageing",
"Inventory Carring Cost", "Inventory Turns" };
LinearLayout linearList = (LinearLayout) findViewById(R.id.linearlist);
drawable = setBitMap(list_image);
linearList.setBackgroundDrawable(drawable);
this.setListAdapter(new ArrayAdapter<String>(this,R.layout.warehouse_list,names));
// ListView lv = getListView(); lv.setTextFilterEnabled(true);
}
public Drawable setBitMap(String imagename) {
Bitmap image = BitmapFactory.decodeStream(getClass()
.getResourceAsStream(("/com/image/" + imagename + ".png")));
drawable = new BitmapDrawable(image);
return drawable;
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_width="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/linearfullview" android:layout_height="400sp"
android:layout_alignParentTop="true" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="180sp" android:layout_alignParentBottom="true"
android:layout_marginTop="20sp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/linearlist" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp" android:gravity="bottom"
android:layout_marginTop="30dp">
<ListView android:id="@android:id/list" android:layout_width="100dip" android:layout_height="wrap_content"></ListView>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/linearFirst"
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:paddingLeft="2dp" android:paddingRight="2dp"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:visibility="invisible">
<com.widget.WheelView
android:id="@+id/firstWheel" android:layout_height="wrap_content"
android:layout_width="100dp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/linearSecond"
android:layout_width="wrap_content" android:paddingLeft="2dp"
android:paddingRight="2dp" android:layout_alignParentRight="true"
android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:visibility="invisible">
<com.widget.WheelView
android:id="@+id/secondWheel" android:layout_height="wrap_content"
android:layout_width="100dp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/linearThird"
android:layout_width="wrap_content" android:paddingLeft="2dp"
android:paddingRight="2dp" android:paddingTop="4dp" android:gravity="bottom"
android:layout_marginTop="30dp" android:layout_alignParentLeft="true"
android:visibility="invisible">
<com.widget.WheelView
android:id="@+id/thirdWheel" android:layout_height="wrap_content"
android:layout_width="200dp" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要扩展
ListActivity
那么您必须在 main.xml 中使用@+android:id/list
获取 Listviewif you are extending
ListActivity
then you must take a Listview in main.xml with@+android:id/list
可以用吗
?
Can you use
instead ?
参考http://www.vogella.de/articles/AndroidListView/article.html 链接。
你犯了一个小错误,祝你
好运......
refer http://www.vogella.de/articles/AndroidListView/article.html link.
There is a minor mistake ur making
Best of Luck...