Android - Dynamically Updating a custom ListView after items in an ArrayList are added

后端 未结 1 1848
别跟我提以往
别跟我提以往 2020-12-10 00:18

I have an activity that displays an initially populated custom ListView taken from an ArrayList, the user can then add to this list which I have no problem storing. I\'m hav

相关标签:
1条回答
  • 2020-12-10 01:01

    Your adapter does not get the new data, because you are initializing it with its own set of data.

    One possibility would be to instantiate a new adapter and assign it to the ListView.

    Add a field for your ListView in your activity:

    public TextView tv;
    private int variantPosition; 
    CustomListAdapter customListAdapter;
    CurrentOrderClass currentOrder = new CurrentOrderClass();
    ListView myListView; //Listview here
    

    In onCreate, set myListView to point to your ListView:

    final ListView lv1 = (ListView) findViewById(R.id.listViewProductOrder)
    myListView = lv1;
    

    Finally, when you change your data, create a new Adapter for the new data:

    myListView.setAdapter(new CustomListAdapter(this, getListData());
    

    Alternatively, modify your Custom adapter to contain a setListData method:

    public class CustomListAdapter extends BaseAdapter {
    
      private ArrayList<CurrentOrderClass> listData;
    
      private LayoutInflater layoutInflater;
    
      public CustomListAdapter(Context context, ArrayList<CurrentOrderClass> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
      }
    
      public void setListData(ArrayList<CurrentOrderClass> data){
        listData = data;
      }
    
      @Override
      public int getCount() {
        return listData.size();
      }
    
    
      @Override
      public Object getItem(int position) {
        return listData.get(position);
      }
    
      @Override
      public long getItemId(int position) {
        return position;
      }
    
      public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
            holder = new ViewHolder();
            holder.variantView = (TextView) convertView.findViewById(R.id.variant);
            holder.unitView = (TextView) convertView.findViewById(R.id.unit);
            holder.quantityView = (TextView) convertView.findViewById(R.id.quantity);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
    
        holder.variantView.setText(listData.get(position).getVariantArray().get(position).toString());
        holder.unitView.setText(listData.get(position).getUnitArray().get(position).toString());
        holder.quantityView.setText(String.valueOf(listData.get(position).getQuantityRow()));
    
        return convertView;
    }
    
      static class ViewHolder {
        TextView variantView;
        TextView unitView;
        TextView quantityView;
      }
    
    
    }
    

    Then, after modifying your data, just call:

    customListAdapter.setListData(getListData());
    customListAdapter.notifyDataSetChanged();
    
    0 讨论(0)
提交回复
热议问题