Listview 添加第二项后仅显示一项
Posted
技术标签:
【中文标题】Listview 添加第二项后仅显示一项【英文标题】:Listview only display one item after add the second item 【发布时间】:2020-02-28 11:21:24 【问题描述】:为什么我的自定义列表视图仅显示通过单击该窗口中的“添加”按钮在 EditText 中键入的最新数据(列表视图中只有一行),右侧它应该附加在列表视图项的下一行中和保存数据并将其读入 .dat 文件。任何人都可以帮助我使用下面的代码吗?
这是我的 MainActivity 代码:
public class MainActivity extends AppCompatActivity implements
View.OnClickListener
private EditText edText;
private Button btnAdd;
private DatePickerDialog.OnDateSetListener dpdDateTimePicker;
private TextView tvDate;
private TextView tvListItem;
private TextView tvListDate;
private ArrayList<Items> arrItem;
private ItemListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edText = findViewById(R.id.edText);
btnAdd = findViewById(R.id.btnAdd);
tvDate = findViewById(R.id.tvDate);
tvListItem = findViewById(R.id.tvItem);
tvListDate = findViewById(R.id.tvDate);
ListView lvItemList = (ListView) findViewById(R.id.lvItem);
//Date start
tvDate.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
android.R.style.Theme_Holo_Dialog_MinWidth,
dpdDateTimePicker,
year, month,day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
);
dpdDateTimePicker = new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
month = month + 1;
String date = dayOfMonth + "/" + month + "/" + year;
tvDate.setText(date);
;
//Date end
//Read Data
arrItem = FileHelper.readData(this);
adapter = new ItemListAdapter(this, R.layout.adapter_view_layout, arrItem);
lvItemList.setAdapter(adapter);
btnAdd.setOnClickListener(this);
@Override
public void onClick(View v)
switch (v.getId())
case R.id.btnAdd:
ListView lvItemList = (ListView) findViewById(R.id.lvItem);
String itementered = edText.getText().toString();
String dateentered = tvDate.getText().toString();
Items item1 = new Items(itementered, dateentered);
ArrayList<Items> ItemList = new ArrayList<>();
ItemList.add(item1);
FileHelper.writeData(ItemList, this);
adapter = new ItemListAdapter(this, R.layout.adapter_view_layout, ItemList);
lvItemList.setAdapter(adapter);
//Clear TextView and EditText
edText.setText("");
tvDate.setText("");
Toast.makeText(this,"Task Added", Toast.LENGTH_SHORT).show();
break;
FileHelper 类将项目读/写到 .dat
public class FileHelper
public static final String FILENAME = "ItemList.dat";
public static void writeData(ArrayList<Items> item, Context context)
try
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(item);
oos.close();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
public static ArrayList<Items> readData(Context context)
ArrayList<Items> arItemList = null;
try
FileInputStream fis = context.openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
arItemList = (ArrayList<Items>) ois.readObject();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (ClassNotFoundException e)
e.printStackTrace();
return arItemList;
我的自定义 ItemListAdapter
public class ItemListAdapter extends ArrayAdapter<Items>
private Context mContext;
int mResource;
public ItemListAdapter(Context context, int resource, ArrayList<Items> objects)
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
//get the Item info
String task = getItem(position).getTask();
String date = getItem(position).getDate();
//Create the task object with the information
Items item = new Items(task, date);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView tvDate = (TextView) convertView.findViewById(R.id.tvDate);
TextView tvTask = (TextView) convertView.findViewById(R.id.tvItem);
tvDate.setText(date);
tvTask.setText(task);
return convertView;
物品类别
public class Items implements Serializable
private String task;
private String date;
public Items(String task, String date)
this.task = task;
this.date = date;
public String getTask()
return task;
public void setTask(String task)
this.task = task;
public String getDate()
return date;
public void setDate(String date)
this.date = date;
【问题讨论】:
【参考方案1】:在您的onClick
函数中,您总是会创建一个新的ArrayList<Items> ItemList
并向其中添加一个Items
,然后创建一个新的ItemListAdapter
,它会在@ 中显示这个新的(单项)列表987654326@.
代码应该更像下面。
@Override
public void onClick(View v)
switch (v.getId())
case R.id.btnAdd:
ListView lvItemList = (ListView) findViewById(R.id.lvItem);
String itementered = edText.getText().toString();
String dateentered = tvDate.getText().toString();
Items item1 = new Items(itementered, dateentered);
//do not create a new ArrayList
//ArrayList<Items> ItemList = new ArrayList<>();
//ItemList.add(item1);
//append item1 to the existing one instead
arrItem.add(item1);
//you might have to change this line to handle addition of single item
//FileHelper.writeData(ItemList, this);
//however after a quick look at FileHelper it seems that
FileHelper.writeData(arrItem, this); //might work
//now you also don't need to/shouldn't create a new adapter
//adapter = new ItemListAdapter(this, R.layout.adapter_view_layout, ItemList);
//lvItemList.setAdapter(adapter);
//just notify that arrItem has changed and the adapter will update the ListView
adapter.notifyDataSetChanged();
//Clear TextView and EditText
edText.setText("");
tvDate.setText("");
Toast.makeText(this,"Task Added", Toast.LENGTH_SHORT).show();
break;
请注意,我还包括adapter.notifyDataSetChanged()
,因为这确实是适配器方便的原因。
您首先将onCreate
中的adapter
设置为lvItemList
:
adapter = new ItemListAdapter(this, R.layout.adapter_view_layout, arrItem);
lvItemList.setAdapter(adapter);
这会在lvItemList
中显示arrItem
的内容。如果您稍后希望添加项目,请将其添加到 arrItem
并调用 adapter.notifyDataSetChanged()
以显示更改。
关于FileHelper
的注释。看起来FileHelper.writeData()
重写了整个文件,因此写入arrItem
应该会产生所需的结果,其中lvItemList
中显示的所有项目也都存储在文件中。
【讨论】:
以上是关于Listview 添加第二项后仅显示一项的主要内容,如果未能解决你的问题,请参考以下文章