【问题标题】:Listadapter not binding values to listviewListadapter 未将值绑定到列表视图
【发布时间】:2014-11-28 19:20:54
【问题描述】:

我创建了一个演示应用程序来在屏幕上显示产品列表。产品列表来自远程服务器。在日志中,我可以看到数据传入并存储在 arralist 中,但不知何故,它没有与列表视图绑定。我正在使用异步类在后台进行 http 调用。

下面是创建代码的活动

   ListAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dbtest2_all_products);
    Log.i("Entrey level=","entere dbtest2allproducts class");
    // Hashmap for ListView
    productsList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
   new LoadAllProducts().execute();

    // Get listview
    ListView lv = new ListView(DBtest2AllProducts.this);

    // updating listview
    lv.setAdapter(adapter);

下面是

的postexecute方法
 protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
    // ((BaseAdapter) adapter).notifyDataSetChanged();
        Log.i("before adapter",productsList.toString());
        adapter = new SimpleAdapter(
                DBtest2AllProducts.this, productsList,
                R.layout.activity_dbtest2_list_items, new String[] { TAG_PID,
                        TAG_NAME},
                new int[] { R.id.pid, R.id.name }); 
            //setAdapter(adapter);

虽然我相信 setAdapter(adapter) 步骤存在一些同步问题,但由于我是 android 新手,所以不能打赌。我尝试使用 postexecute 方法调用 setAdapter(adapter) 方法和 adapter.notifyDataSetChanged() 但它们都抛出错误说“没有为这种类型定义方法”但是如果我将适配器转换为 (BaseAdapter) 那么 adapter.notifyDataSetChanged() 不会'不要抛出任何错误,但我实际上不知道这种铸造有什么区别,而且铸造也不起作用。

下面是日志猫

I/before adapter(32729): [{pid=1, name=iphone 4s}, {pid=2, name=samsung}]

如果您发现代码有任何问题,请提出建议。感谢您提前提供帮助。

下面是xml布局

all_product.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

  <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

  </LinearLayout>

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >


    <TextView
    android:id="@+id/pid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

  <!-- Name Label -->
  <TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:paddingTop="20dip"
    android:paddingLeft="10dip"
    android:textSize="17sp"
    android:textStyle="bold" />

</RelativeLayout>

【问题讨论】:

  • 移动 lv.setAdapter(adapter);在 onPostExecute 之后,adapter = new SimpleAdapter(...
  • 我试过了,但它抛出错误“lv 无法解析”我认为 lv 超出了后执行方法的范围。

标签: android listview adapter listadapter


【解决方案1】:

您需要在将适配器设置为ListView 之前对其进行初始化。让ListView全局化,获取数据后才设置适配器

    ListView lv;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbtest2_all_products);
        Log.i("Entrey level=","entere dbtest2allproducts class");
        // Hashmap for ListView
        productsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllProducts().execute();

        // Get list view from xml file 
        lv = findViewById(R.id.listview);
        FrameLayout rootView = (FrameLayout) findViewById(android.R.id.content);

        //Add this line to make sure your list is using the whole screen width
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rootView.addView(lv);
    }


    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        adapter = new SimpleAdapter(
            DBtest2AllProducts.this, productsList,
            R.layout.activity_dbtest2_list_items, new String[] { TAG_PID,
                    TAG_NAME},
            new int[] { R.id.pid, R.id.name }); 
        lv.setAdapter(adapter);
    }

【讨论】:

  • 谢谢卡洛斯!我猜你跳过了我在问题中的最后几行。我在执行后尝试了adapter.notifyDataSetChange(),但它抛出错误说“该方法未定义类型listAdapter”,我尝试了第二个解决方案。虽然它没有引发任何错误,但在对话框消失后,我没有看到产品列表:(
  • 知道为什么 notifyDataSetChange() 在执行后不起作用?
  • 我想我找到了你的问题。您的 ListView 在 activity_dbtest2_all_products.xml 文件上吗?如果是,那么您没有正确初始化,并且如果您以编程方式添加它,那么您还需要将其添加到主视图中。这就是为什么你什么都看不到的原因
  • 感谢卡洛斯,第二个解决方案奏效了。但是,如果我可以使用 adapter.notifyDataSetChanged() 方法会很棒,但我认为它仅适用于 baseadaper 类,因为它不会自动出现在适配器中(如果我错了,请纠正我)。我正在使用 2 个 xml 文件。首先是我在 oncreate 方法中设置为内容的 all_products 活动。它只有一个 listview 元素。另一个 xml 文件解释了如何为我在适配器实例化“activity_dbtest2_list_items.xml”中传递的每个列表项显示数据。感谢您的帮助:)
  • btw framLayout 尽管我在代码中使用了 android:layout_centerHorizo​​ntal="true" 和 android:gravity="center_horizo​​ntal" 导致列表粘在左上角。是因为框架布局吗?如果我的文本以任何方式出现在水平中心?
【解决方案2】:

@Pawan Rawat,在实现上述答案后,如果您有信心继续,请切换到 Volley,这是一个更强大的网络库,性能网络调用更快,代码更少。 请在http://www.androidhive.info/2014/05/android-working-with-volley-library-1/阅读更多内容

【讨论】:

  • 谢谢 Trey,一旦我解决了手头的问题,我一定会看看它!
猜你喜欢
  • 2017-09-22
  • 2020-01-21
  • 2013-04-05
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多