vila994
vila994

Reputation: 1357

Populating a ListView with an ArrayList - ListView doesn't populate

I want to populate a ListView (with checkbox) with an ArrayList. The ArrayList gets data from a server and contains my Shop's class objects, and it's filled in my function onPostExecute. I'm trying to populate listview with shop name but not populating! Any suggestions?

onPostExecute

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    if (processDialog.isShowing()) {
        processDialog.dismiss();
    }

    if (success == 1) {
        if (null != restulJsonArray) {
            for (int i = 0; i < restulJsonArray.length(); i++) {
                    try {
                        JSONObject jsonObject = restulJsonArray.getJSONObject(i);
                        favouriteShopsList.add(new Shop(jsonObject.getInt("id"),jsonObject.getString("azienda"), false));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }
        }

    }
}

I checked and the list is not empty. In my onCreate function, i execute the AsyncTask to get data from server and then i try to populate my ListView with a custom adapter. This is my onCreate function:

My Activity

public class NearestShopActivity extends AppCompatActivity{

private ProgressDialog processDialog;
private JSONArray restulJsonArray;

private int success = 0;
private Context context;

private ArrayList<Shop> favouriteShopsList = new ArrayList<Shop>();
private ArrayAdapter<Shop> listAdapter;

private ListView listView;
private CheckBox checkBox;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nearest_shop);
    context = NearestShopActivity.this;

    new ServiceStubAsyncTask(this, this).execute();

    listView = (ListView) findViewById(R.id.activityNearestShopList);
    listAdapter = new ShopArrayAdapter(this, favouriteShopsList);
    listView.setAdapter(listAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View item, int position, long id){
            Shop shop = listAdapter.getItem(position);
            shop.toggleChecked();
            ShopViewHolder viewHolder = (ShopViewHolder) item.getTag();
            viewHolder.getCheckBox().setChecked(shop.isChecked());
        }
    });

    final Button continueBtn = (Button) findViewById(R.id.activityNearestShopContinueBtn);
    continueBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            for(int i=0; i<listAdapter.getCount(); i++){
                Shop shop = listAdapter.getItem(i);
                if(shop.isChecked()){
                    Toast.makeText(context, "Checked: " + shop.getAzienda(), Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}
    private class ServiceStubAsyncTask extends AsyncTask<Void, Void, Void> {...}
}

I also post my ShopArrayAdapter class:

  private static class ShopArrayAdapter extends ArrayAdapter<Shop>{

        private LayoutInflater inflater;

        public ShopArrayAdapter(Context context, List<Shop> favouriteShopsList) {
            super(context, R.layout.activity_nearest_shop_item, R.id.activityNearestShopItem, favouriteShopsList);
            inflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            Shop shop = (Shop) this.getItem(position);

            CheckBox checkBox;
            TextView textView;

            if(convertView == null){
                convertView = inflater.inflate(R.layout.activity_nearest_shop_item, null);
                textView = (TextView) convertView.findViewById(R.id.activityNearestShopItem);
                checkBox = (CheckBox) convertView.findViewById(R.id.activityNearestShopCheckbox);

                convertView.setTag(new ShopViewHolder(textView, checkBox));

                checkBox.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        CheckBox cb = (CheckBox) v;
                        Shop shop = (Shop) cb.getTag();
                        shop.setChecked(cb.isChecked());
                    }
                });
            }else{
                ShopViewHolder viewHolder = (ShopViewHolder) convertView.getTag();
                checkBox = viewHolder.getCheckBox();
                textView = viewHolder.getTextView();
            }
            checkBox.setTag(shop);
            checkBox.setChecked(shop.isChecked());
            textView.setText(shop.getAzienda());

            return convertView;
        }
    }

Upvotes: 0

Views: 67

Answers (1)

Levi Moreira
Levi Moreira

Reputation: 12007

Your adapter doesn't know you've added new data here:

 favouriteShopsList.add(new Shop(jsonObject.getInt("id"),jsonObject.getString("azienda"), false));

try calling notifydatasetchanged() after you've added new data:

listAdapter.notifyDataSetChanged()

Upvotes: 2

Related Questions