新行未添加到 ListView Android
New row is not adding in ListView Android
我的应用程序将项目(即姓名、年龄和人物形象)添加到列表视图。但是每当我单击添加时,它都会替换旧的 listView 并且不会添加新行。
主要Activity
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();
}
}}
我只需要这个,当我按添加时,它应该会在 listView 中添加新行。
我认为你需要添加 notifyDataSetChange
If, during the course of your application's life, you change the underlying data that is read by your adapter, you should call notifyDataSetChanged(). This will notify the attached view that the data has been changed and it should refresh itself.
你应该实现的是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);
- 通过以下方式更改对话框的 onCLickMethod :
`
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();
}
希望对您有所帮助,如有问题请回复。
我的应用程序将项目(即姓名、年龄和人物形象)添加到列表视图。但是每当我单击添加时,它都会替换旧的 listView 并且不会添加新行。 主要Activity
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();
}
}}
我只需要这个,当我按添加时,它应该会在 listView 中添加新行。
我认为你需要添加 notifyDataSetChange
If, during the course of your application's life, you change the underlying data that is read by your adapter, you should call notifyDataSetChanged(). This will notify the attached view that the data has been changed and it should refresh itself.
你应该实现的是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);
- 通过以下方式更改对话框的 onCLickMethod :
`
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(); }
希望对您有所帮助,如有问题请回复。