Listadapter没有将值绑定到listview

时间:2014-11-28 19:20:54

标签: android listview adapter listadapter

我创建了一个演示应用程序来在屏幕上显示产品列表。产品列表来自远程服务器。在日志中我可以看到来自和存储在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(适配器)步骤存在一些同步问题,但我是因为我是Android新手所以不能赌它。我尝试使用postexecute方法调用setAdapter(adapter)方法和adapter.notifyDataSetChanged(),但是它们都抛出错误,说“没有为此类型定义方法”但是如果我将适配器转换为(BaseAdapter),那么adapter.notifyDataSetChanged()不会不会抛出任何错误,但我不知道这个铸造有什么不同,而且铸造也不起作用。

以下是log cat

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>

2 个答案:

答案 0 :(得分:0)

在将适配器设置为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);
    }

答案 1 :(得分:0)

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

了解详情