ListActivity中使用ListView时出错

发布于 2024-12-07 16:38:33 字数 5226 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅黛梨妆こ 2024-12-14 16:38:33

如果您要扩展 ListActivity 那么您必须在 main.xml 中使用 @+android:id/list 获取 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" />
...

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" />
...
半暖夏伤 2024-12-14 16:38:33

可以用吗

public class Warehouse extends Activity implements onListItemClick

Can you use

public class Warehouse extends Activity implements onListItemClick

instead ?

不语却知心 2024-12-14 16:38:33

参考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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文