在ListActivity中使用ListView时出错

时间:2011-09-29 06:48:58

标签: android listview listactivity

我在使用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> 

3 个答案:

答案 0 :(得分:0)

如果您要扩展ListActivity,则必须使用@+android:id/list

在main.xml中使用Listview
<?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">
    <ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />
...

答案 1 :(得分:0)

你能用吗

public class Warehouse extends Activity implements onListItemClick

相反?

答案 2 :(得分:0)

参考http://www.vogella.de/articles/AndroidListView/article.html链接。 你犯了一个小错误

最好的运气......