ListView Android中没有添加新行

时间:2017-04-30 13:22:06

标签: android listview baseadapter

我的应用程序将项目(即名称,年龄和人物图像)添加到列表视图。但每当我点击添加时,它都会替换旧的listView,并且不会添加任何新行。 主要活动

public class MainActivity extends AppCompatActivity {

private AlertDialog.Builder builder;
private Uri uri;
private CustomAdapter customAdapter;
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.listView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==R.id.menuItem)
    {
        builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add to List.");
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setView(R.layout.alert_dialog);
        builder.setCancelable(false);
        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if(uri != null) {

                    EditText nameText = (EditText) ((AlertDialog) dialogInterface).findViewById(R.id.name);
                    EditText ageText = (EditText) ((AlertDialog) dialogInterface).findViewById(R.id.age);
                    customAdapter = new CustomAdapter(getApplicationContext(), nameText.getText().toString(), ageText.getText().toString(), uri);
                    listView.setAdapter(customAdapter);


                }
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
            }
        });

        Dialog dialog = builder.create();
        dialog.show();
    }
    return super.onOptionsItemSelected(item);
}


public void imagePicker(View v) {

    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    i.setType("image/*");
    i.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(i, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && data != null){
        uri = data.getData();

    }
}}

我只需要这个,当我按下Add时,它应该在listView中添加新行。

2 个答案:

答案 0 :(得分:0)

我认为您需要添加notifyDataSetChange

  

如果在应用程序生命周期中,您更改了适配器读取的基础数据,则应调用notifyDataSetChanged()。这将通知附加的视图,数据已被更改,并且应该自行刷新。

答案 1 :(得分:0)

你应该实现的是 notifyDataSetChanged 同样可以做到如下:  创建自定义POJO,UserInfo

public Class UserInfo{
private String name;
private String age;
private Uri uri;

public UserInfo(String name, String age, Uri uri) {
    this.name = name;
    this.age = age;
    this.uri = uri;
}

}

  • 声明UserInfo列表

    private List<UserInfo> userList= new ArrayList<>();

  • 在onCreate方法中添加以下行。

    customAdapter = new CustomAdapter(getApplicationContext(), userList); listView.setAdapter(customAdapter);

  • 通过以下方式更改onChickMethod对话框:

`

if(uri!=null){
EditText nameText = (EditText) ((AlertDialog) dialogInterface).findViewById(R.id.name);
EditText ageText = (EditText) ((AlertDialog) dialogInterface).findViewById(R.id.age);
userList.add(new 
  UserInfo(nameText.getText().toString(),ageText.getText().toString(),uri));
customAdapter.swapData(userList);
}

`

  • 修改您的CustomAdapter

    List<UserInfo> userList;

    public CustomAdapter(Context context,List<UserInfo> userList){ this.context = context; this.userList = userList; this.inflater = LayoutInflater.from(context); }

    `@Override
    public int getCount() {
      if(userList!=null){
         return userList.size();
      }else{
        return 1;
      }
    }
    

    public void swapData(List<UserInfo> userList) { this.userList = userList; notifyDataSetChanged(); }

希望这有帮助,如果有任何问题可以恢复。