开发者

Error in using ListView in ListActivity

开发者 https://www.devze.com 2023-04-08 09:04 出处:网络
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

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:开发者_如何学Goid/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> 


if you are extending ListActivity then you must take a Listview in main.xml with @+android:id/list

<?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" />
...


Can you use

public class Warehouse extends Activity implements onListItemClick

instead ?


refer http://www.vogella.de/articles/AndroidListView/article.html link. There is a minor mistake ur making

Best of Luck...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号