【发布时间】:2017-03-13 19:33:57
【问题描述】:
我一直在尝试使用 android studio 制作一个待办事项列表应用程序。目的是顶部的“添加新任务”按钮打开一个对话框,我可以通过该对话框输入新任务。然后任务会出现在按钮下方的列表中。
问题是按钮在列表中的每个任务之后不断出现(重复的按钮不可点击)
这是我的代码:
public class ListActivity extends Activity {
// Declare variables
Button button;
private TaskDbHelper mHelper;
private ArrayAdapter<String> mAdapter;
private ListView mTaskListView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.activity_list);
// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.NewTaskButton);
mHelper = new TaskDbHelper(this);
mTaskListView = (ListView) findViewById(R.id.listview);
updateUI();
NewMethod();
}
public void NewMethod() {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final EditText taskEditText = new EditText(ListActivity.this);
AlertDialog alertDialog = new AlertDialog.Builder(ListActivity.this).create();
alertDialog.setTitle("Add a new item");
alertDialog.setMessage("What item would you like to add?");
alertDialog.setView(taskEditText);
alertDialog.setButton("Add item", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
String task = String.valueOf(taskEditText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
updateUI();
}
});
alertDialog.show();
}
});
}
private void updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
null, null, null, null, null);
while (cursor.moveToNext()) {
int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
taskList.add(cursor.getString(idx));
}
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(this,
R.layout.activity_list,
R.id.task_title,
taskList);
mTaskListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(taskList);
mAdapter.notifyDataSetChanged();
}
cursor.close();
db.close();
}
}
这里是xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:divider="#C2AF00"
android:dividerHeight="3sp"
android:layout_below="@+id/task_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp" />
<TextView
android:id="@+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20sp"
android:layout_below="@+id/NewTaskButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp" />
<Button
android:id="@+id/NewTaskButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="@string/NewTaskButton"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#0089BF"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"/>
</RelativeLayout>
【问题讨论】: