Search code examples
androidandroid-arrayadapter

Why not change position in getView() android?


position does not change after add new data in arraylist.

public class ItemsAdapter extends BaseAdapter {

private Context context;
private LayoutInflater inflater;
private ArrayList<HashMap<String, String>> data;

public ItemsAdapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
}

@Override
public int getCount() {
    //=========HERE DATA IS COMING CORRECTLY===============
    for (int i = 0; i < data.size(); i++)
        System.out.println("Get Couunt Size : - "
                + data.get(i).get("Title"));
    return data.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Holder holder;
    if (convertView == null) {
        holder = new Holder();

        convertView = inflater.inflate(R.layout.item, parent, false);
        holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
        holder.tvNotes = (TextView) convertView.findViewById(R.id.tvNotes);
        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }

    //=========HERE POSITION ALWAYS 0===============
    System.out.println("Position:::" + position + " Title : - "
            + data.get(position).get("Title") + ", Notes :- "
            + data.get(position).get("Note"));

    holder.tvTitle.setText(data.get(position).get("Title"));
    holder.tvNotes.setText(data.get(position).get("Note"));
    return convertView;
}

private static class Holder {
    TextView tvTitle, tvNotes;
}

}

i also call notifyDataSetChanged() method in main activity.

add data using dialogbox.

dlAddNote = new Dialog(this);
    dlAddNote.setTitle("Add Note");
    dlAddNote.setContentView(R.layout.add_note_dialog);

    final EditText etTitle = (EditText) dlAddNote
            .findViewById(R.id.etTitle);
    final EditText etNotes = (EditText) dlAddNote
            .findViewById(R.id.etNotes);

    Button btnSave = (Button) dlAddNote.findViewById(R.id.btnSave);

    Button btnCancel = (Button) dlAddNote.findViewById(R.id.btnCancel);

    btnSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("Title", etTitle.getText().toString());
            hashMap.put("Note", etNotes.getText().toString());
            System.out.println("Title" + etTitle.getText().toString());
            System.out.println("Note" + etNotes.getText().toString());
            arrayListLeft.add(hashMap);

            dlAddNote.dismiss();

            leftAdapter = new ItemsAdapter(KeepGuiActivity.this,
                    arrayListLeft);
            listViewLeft.setAdapter(leftAdapter);
            leftAdapter.notifyDataSetChanged();
        }
    });

    btnCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dlAddNote.cancel();
        }
    });

    dlAddNote.show();

i am using custom dialog box and add data in runtime but dose not change data.


Solution

  • problem:

    dlAddNote.dismiss();
    

    What it is doing is that it will dismiss your dialog and wont execute the code after it so that is why the listview is not updating.

    solution:

    call it before the dismiss method and check if leftAdapter is null to avoid recreating your listview over and over again.

    btnSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
    
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("Title", etTitle.getText().toString());
            hashMap.put("Note", etNotes.getText().toString());
            System.out.println("Title" + etTitle.getText().toString());
            System.out.println("Note" + etNotes.getText().toString());
            arrayListLeft.add(hashMap);
    
            if(leftAdapter == null) {
               leftAdapter = new ItemsAdapter(KeepGuiActivity.this,  arrayListLeft);
               listViewLeft.setAdapter(leftAdapter);
            }
            leftAdapter.notifyDataSetChanged();
    
            dlAddNote.dismiss();
        }
    });