编辑
得到该错误,原来是关于模拟器的API版本的问题。在更高版本的API中工作正常
我是Android开发的新手,我正在尝试建立一个简单的待办事项列表,在其中单击按钮时会将项目添加到ListView。问题是,尽管已添加和删除了项目,但它们未在列表视图中显示。请帮助我确定我在做什么错。
这是我的activity_main.xml文件
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.erfan.todolist.MainActivity">
<RelativeLayout
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<EditText
android:id="@+id/editTextAddItem"
android:hint="Enter Item"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_weight="4"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/buttonAddItem"
android:text="Add"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/editTextAddItem"
android:layout_marginTop="23dp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
这是我的MainActivity.java文件
package com.example.erfan.todolist;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText vEditTextAddItem;
Button vButtonAddItem;
ListView vListViewItemList;
ArrayList<String> items;
ArrayAdapter<String> adapter;
public static final String FILENAME = "data.dat";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vEditTextAddItem = (EditText) findViewById(R.id.editTextAddItem);
vButtonAddItem = (Button) findViewById(R.id.buttonAddItem);
vListViewItemList = (ListView) findViewById(R.id.listView);
items = readData(getApplicationContext());
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, items);
vListViewItemList.setAdapter(adapter);
vButtonAddItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonAddItem:
String itemEntered = vEditTextAddItem.getText().toString();
adapter.add(itemEntered);
vEditTextAddItem.setText("");
writeData(items, getApplicationContext());
Toast.makeText(getApplicationContext(), "Item is added", Toast.LENGTH_SHORT).show();
break;
}
}
});
vListViewItemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Item Deleted", Toast.LENGTH_SHORT).show();
}
});
}
public static void writeData(ArrayList<String> items, Context context) {
try {
FileOutputStream fileOutputStraStream = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStraStream);
objectOutputStream.writeObject(items);
objectOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList<String> readData(Context context) {
ArrayList<String> itemList = null;
try {
FileInputStream fileInputStream = context.openFileInput(FILENAME);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
itemList = (ArrayList<String>) objectInputStream.readObject();
} catch (FileNotFoundException e) {
itemList = new ArrayList<>();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
itemList = new ArrayList<>();
e.printStackTrace();
}
return itemList;
}
}
我也附上了模拟器的屏幕截图
添加项目后,ListView中没有显示项目。
得到该错误,原来是关于模拟器的API版本的问题。在更高版本的API中工作正常