Java 我的列表上出现多个按钮(Android Studio待办事项列表应用程序)
我一直在尝试使用android studio制作一个待办事项列表应用程序。其目的是在顶部的“添加新任务”按钮打开一个对话框,我可以通过该对话框输入新任务。然后,该任务出现在按钮下方的列表中 问题在于,该按钮在列表中的每个任务之后都会不断出现(重复的按钮不可点击) 这是我的密码:Java 我的列表上出现多个按钮(Android Studio待办事项列表应用程序),java,android,Java,Android,我一直在尝试使用android studio制作一个待办事项列表应用程序。其目的是在顶部的“添加新任务”按钮打开一个对话框,我可以通过该对话框输入新任务。然后,该任务出现在按钮下方的列表中 问题在于,该按钮在列表中的每个任务之后都会不断出现(重复的按钮不可点击) 这是我的密码: public class ListActivity extends Activity { // Declare variables Button button; private TaskDbH
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();
}
}
公共类ListActivity扩展活动{
//声明变量
按钮;
私人任务助手mHelper;
私人ArrayaAdapter mAdapter;
私有ListView mTaskListView;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//从listview_main.xml获取视图
setContentView(R.layout.activity_列表);
//在activity_main.xml中找到该按钮
按钮=(按钮)findViewById(R.id.NewTaskButton);
mHelper=新TaskDbHelper(此);
mTaskListView=(ListView)findViewById(R.id.ListView);
updateUI();
NewMethod();
}
公共方法(){
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
最终编辑文本任务编辑文本=新编辑文本(ListActivity.this);
AlertDialog AlertDialog=新建AlertDialog.Builder(ListActivity.this).create();
alertDialog.setTitle(“添加新项”);
setMessage(“您想添加什么项目?”);
alertDialog.setView(taskEditText);
alertDialog.setButton(“添加项”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface dialog,int which){
//在这里可以添加函数
String task=String.valueOf(taskEditText.getText());
SQLiteDatabase db=mHelper.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(TaskContract.TaskEntry.COL_TASK_TITLE,TASK);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
无效的
价值观
SQLiteDatabase.CONFLICT\u REPLACE);
db.close();
updateUI();
}
});
alertDialog.show();
}
});
}
私有void updateUI(){
ArrayList taskList=新建ArrayList();
SQLiteDatabase db=mHelper.getReadableDatabase();
Cursor Cursor=db.query(TaskContract.TaskEntry.TABLE,
新字符串[]{TaskContract.TaskEntry.\u ID,TaskContract.TaskEntry.COL\u TASK\u TITLE},
空,空,空,空,空,空);
while(cursor.moveToNext()){
int idx=cursor.getColumnIndex(TaskContract.TaskEntry.COL\u TASK\u TITLE);
taskList.add(cursor.getString(idx));
}
if(mAdapter==null){
mAdapter=新阵列适配器(此,
R.layout.activity_列表,
R.id.任务名称,
任务清单);
mTaskListView.setAdapter(mAdapter);
}否则{
mAdapter.clear();
mAdapter.addAll(任务列表);
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>
当您执行此操作时
mAdapter = new ArrayAdapter<>(this,
R.layout.activity_list,
R.id.task_title,
taskList);
但是您可以添加一个ViewGroup
作为根元素,并构建您想要的任何布局,只要您为其指定了id
的文本视图存在
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/task_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />