使用ArrayList填充ListView - ListView不会填充

时间:2018-03-15 13:15:50

标签: android listview arraylist

我想用ArrayList填充ListView(带复选框)。 ArrayList从服务器获取数据并包含我的Shop的类对象,并且它在我的函数onPostExecute中填充。 我正在尝试使用商店名称填充listview但不填充!有什么建议吗?

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

    }
}

我查了一下,列表不是空的。 在我的onCreate函数中,我执行AsyncTask从服务器获取数据,然后我尝试用自定义适配器填充我的ListView。这是我的onCreate函数:

我的活动

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> {...}
}

我也发布了我的ShopArrayAdapter类:

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

1 个答案:

答案 0 :(得分:2)

您的适配器不知道您在此处添加了新数据:

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

尝试在添加新数据后调用notifydatasetchanged()

listAdapter.notifyDataSetChanged()