为什么不在getView()android中更改位置?

时间:2014-07-22 07:02:11

标签: android android-arrayadapter

在arraylist中添加新数据后,

位置不会改变。

公共类ItemsAdapter扩展了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;
}

}

我还在主要活动中调用notifyDataSetChanged()方法。

使用对话框添加数据。

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

我正在使用自定义对话框并在运行时添加数据,但不会更改数据。

2 个答案:

答案 0 :(得分:1)

<强>问题:

dlAddNote.dismiss();

它正在做的是它将dismiss你的对话框并且不会在它之后执行代码,这就是listview没有更新的原因。

<强>溶液

dismiss方法之前调用它,并检查leftAdapter是否为null,以避免一遍又一遍地重新创建列表视图。

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

答案 1 :(得分:1)

我测试你的代码并且运行良好。它显示我在对话框中输入的所有数据。 (仅供参考:我在活动范围内声明了您的所有变量,因为您没有在示例代码中显示它)。我把活动的代码放在下面:

public class KeepGuiActivity extends Activity {

private Dialog dlAddNote;
private ListView listViewLeft;
private ArrayList<HashMap<String, String>> arrayListLeft;
private ItemsAdapter leftAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listViewLeft = (ListView)findViewById(R.id.listView1);

    arrayListLeft = new ArrayList<HashMap<String, String>>();

    Button button = (Button)findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog();
        }

    });
}

private void showDialog() {
    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();
}

如果上述代码不适合您,请告诉我您是否使用代码从适配器返回特定项目。此外,您可以更新ItemsAdapter的以下方法以返回实际数据:

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

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