【问题标题】:Multiple buttons appearing on my list (Android Studio to do list app)我的列表中出现多个按钮(Android Studio 待办事项列表应用)
【发布时间】:2017-03-13 19:33:57
【问题描述】:

我一直在尝试使用 android studio 制作一个待办事项列表应用程序。目的是顶部的“添加新任务”按钮打开一个对话框,我可以通过该对话框输入新任务。然后任务会出现在按钮下方的列表中。

问题是按钮在列表中的每个任务之后不断出现(重复的按钮不可点击)

screenshot of problem

这是我的代码:

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>

【问题讨论】:

    标签: java android


    【解决方案1】:

    当你这样做时......

    mAdapter = new ArrayAdapter<>(this,
                        R.layout.activity_list,
                        R.id.task_title,
                        taskList);
    

    ...您应该传入要显示列表中单个项目的布局作为第二个参数。现在,您正在夸大您为每个列表项发布代码的布局。

    您的每行布局可以是任何东西,但重要的是它应该有一个 TextView,其中包含 idR.id.task_title 以匹配您的构造函数调用,以便 ListView 知道将您提供给它的字符串。对于最简单的示例,这可能是您的行布局:

    <?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" />
    

    但您可以添加 ViewGroup 作为根元素,并构建您想要的任何布局,只要您为 id 指定的 TextView 存在。

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多