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

为什么不在getView()android中更改位置?,android,android-arrayadapter,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

在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;
}
私有上下文;
私人充气机;
私有数组列表数据;
公共项适配器(上下文,
ArrayList(ArrayList){
this.context=上下文;
数据=数组列表;
}
@凌驾
public int getCount(){
//=========此处数据正确===============
对于(int i=0;i
}

我还在主活动中调用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();
dlAddNote=新建对话框(此对话框);
dlAddNote.setTitle(“添加注释”);
dlAddNote.setContentView(R.layout.add\u note\u对话框);
最终编辑文本etTitle=(编辑文本)dlAddNote
.findviewbyd(R.id.etTitle);
最终编辑文本etNotes=(编辑文本)dlAddNote
.findViewById(R.id.etNotes);
按钮btnSave=(按钮)dlAddNote.findviewbyd(R.id.btnSave);
按钮btnCancel=(按钮)dlAddNote.findviewbyd(R.id.btnCancel);
btnSave.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
HashMap HashMap=新的HashMap();
put(“Title”,etTitle.getText().toString());
put(“Note”,etNotes.getText().toString());
System.out.println(“Title”+etTitle.getText().toString());
System.out.println(“Note”+etNotes.getText().toString());
arrayListLeft.add(hashMap);
dlAddNote.disclose();
leftAdapter=new ItemsAdapter(KeepGuiActivity.this,
arrayListLeft);
setAdapter(leftAdapter);
leftAdapter.notifyDataSetChanged();
}
});
btnCancel.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
dlAddNote.cancel();
}
});
dlAddNote.show();

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

问题:

dlAddNote.dismiss();
它所做的是,它将
关闭您的对话框,并且不会在对话框之后执行代码,因此
列表视图
不会更新

解决方案:

dlAddNote.dismiss();
discouse
方法之前调用它,并检查
leftAdapter
是否为
null
,以避免反复重新创建listview

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();
    }
});
btnSave.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
HashMap HashMap=新的HashMap();
put(“Title”,etTitle.getText().toString());
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);
setAdapter(leftAdapter);
}
leftAdapter.notifyDataSetChanged();
dlAddNote.disclose();
}
});

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

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

您的意思是,在您添加项目时,它没有显示??是的。我在适配器中添加数据但不更改位置您可以在添加数据的位置发布代码吗?对话框是否只调用一次?@Rod_Algonquin,它只显示第一项,之后它不显示更新的项,每次位置都返回0:(@EleshBaraiya将整个项目发布到dapterclass@EleshBaraiya你从上面的代码中得到了什么结果?谢谢你的朋友给我答案。这是布局错误。