mercredi 29 juin 2016

Listview not updating after arraylist is updated?

I've seen many other posts in this context, but none helped. Problem is when i click on the addField button, the listview inside dialog adds new view just once. But at other clicks it doesn't get updated though The adapter works correctly (I mean the getView is called and also the arrayList in the adapter is changed in size).

I've used notifyDataSetChanged() in the adapter class. No result! I used an instance of adpater class in activity and called myAdapter.notifyDataSetChanged(). No result!

here is my code:

public class MainActivity extends Activity {

ListView lvfid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //some code
    showFid();
}

private void showFiD(){
    final ArrayList <HashMap<String,String>> al = new ArrayList<HashMap<String,String>>();
    final Dialog dialog = new Dialog(MainActivity.this , R.style.DialogTheme);
    dialog.setContentView(R.layout.field_dialog);
    dialog.setCancelable(true);
    dialog.show();
    Button addField = (Button) dialog.findViewById(R.id.btnfidField);
    lvfid = (ListView) dialog.findViewById(R.id.lvfid);
    addField.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            HashMap<String,String> h = new HashMap<String,String>();
            h.put("name", "");
            h.put("value", "");
            al.add(h);
            lvfid.setAdapter(new FieldAdapter(dialog.getContext(), al));

        }
    });
}

public class FieldAdapter extends BaseAdapter{

private ArrayList <HashMap <String,String>> arrayList;
private LayoutInflater inflater;

public FieldAdapter(Context context, ArrayList<HashMap <String,String>> arrayList) {
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.arrayList = arrayList;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    View view = null;
    if (convertView == null)
        view = inflater.inflate(R.layout.field_inflater, null);
     else 
        view = convertView;
    Holder holder = new Holder();
    holder.edName     = (EditText) view.findViewById(R.id.edfidName);
    holder.edValue     = (EditText) view.findViewById(R.id.edfidValue);
    //holder.edName.setText(arrayList.get(position).get("name"));
    //holder.edValue.setText(arrayList.get(position).get("value"));
    return view;
}

private class Holder {
    private EditText edName;
    private EditText edValue;
}


}

}

Aucun commentaire:

Enregistrer un commentaire