Listadapter not binding values to listview

811 Views Asked by At

i created a demo app to display the list of products on screen. the product list comes from remote server. in the logs i can see the data coming and being stored in the arralist but somehow it is not binding with list view. i am making the http call in the background using async class.

below is the activity on create code

   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);

below is postexecute method of

 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);

though i believe as there is some sync issue with the setAdapter(adapter) step but i since i am new to android so couldn't bet on it. i tried calling setAdapter(adapter) method and adapter.notifyDataSetChanged() withing the postexecute method but both of them throw error saying "method is not defined for this type" However if i cast adapter to (BaseAdapter) then adapter.notifyDataSetChanged() doesn't throw any error but i actully dont know what difference does this casting make and also casting didn't work as well.

Below is log cat

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

Please suggest if you see any issues with the code. Thanks for helping in advance.

below are the xml layouts

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

There are 2 best solutions below

18
Carlos J On BEST ANSWER

You need to initialize you adapter before setting it to the ListView. Make ListView global and set the adapter only after getting the data

    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
Ndimofor On

@Pawan Rawat, after implementing the above answer, and if your confident in carrying on, switch to Volley, a networking library which is more robust, performance network calls faster with less code. Please read more at http://www.androidhive.info/2014/05/android-working-with-volley-library-1/