如何使用行和列创建列表视图并使用ArrayAdapter填充它?

时间:2018-04-06 10:39:51

标签: android android-layout android-arrayadapter

我正在尝试创建一个简单的任务应用程序,用于在android上学习arrayadapter和sql db。

我在使用ArrayAdapter和我的Task Class以及设计自定义listview xml以显示像表这样的任务时遇到了问题。

这是我试图设计的布局: mockup

我在布局文件夹中创建了2个xml文件

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.xcrix.taskmanager.MainActivity">


    <ListView
        android:id="@+id/tasks"

        android:layout_width="match_parent"
        android:layout_height="match_parent" />




</RelativeLayout>

我无法将新任务部分添加到activity_main.xml,因为当我放置listview时它会自动扩展所有屏幕。我需要在main relativelayout标记内添加另一个布局吗?

另一个布局文件是row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/taskItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginStart="48dp"
        android:layout_marginTop="33dp"
        android:text="TextView" />

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:text="Delete"
        android:onClick="deleteTask"
        />
</RelativeLayout>

数据库类:

public class database extends SQLiteOpenHelper {


    private static final int DATABASE_VERSION = 1;


    private static final String DATABASE_NAME = "taskManager";

    private static final String TABLE_TASKS = "tasks";

    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_DESC = "desc";
    private static final String KEY_DATE = "date";


    public database(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TASKS_TABLE = "CREATE TABLE " + TABLE_TASKS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_DESC + " TEXT," + KEY_DATE+"TEXT,"+ ")";
        db.execSQL(CREATE_TASKS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);


        onCreate(db); //create tables again
    }



    void addTask(Task task) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, task.getName());
        values.put(KEY_DESC, task.getDesc());
        values.put(KEY_DATE, task.getDate());

        db.insert(TABLE_TASKS, null, values);
        db.close();
    }

    public List<Task> getTasks() {
        List<Task> taskList = new ArrayList<Task>();

        String selectQuery = "SELECT  * FROM " + TABLE_TASKS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Task task = new Task();
                task.setId(Integer.parseInt(cursor.getString(0)));
                task.setName(cursor.getString(1));
                task.setDesc(cursor.getString(2));
                task.setDate(cursor.getString(3));
                taskList.add(task);
            } while (cursor.moveToNext());
        }
        return taskList;
    }

    public void deleteTask(Task task) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_TASKS, KEY_ID + " = ?",
                new String[] { String.valueOf(task.getId()) });
        db.close();
    }


}

任务类:

public class Task {
    int id;
    String name;
    String desc;
    String date;

    public Task(){
this.id = id;
this.name = name;
this.desc = desc;
this.date = date;

    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDesc() {
        return desc;
    }

    public String getDate() {
        return date;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

MainActivity类:

public class MainActivity extends AppCompatActivity {

database helper;
ArrayAdapter<String> adapter;
ListView tasklist;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
helper = new database(this);
tasklist = (ListView) findViewById(R.id.tasks);

displayTasks();
    }

    public void displayTasks(){
        List<Task> taskList = helper.getTasks();
if(adapter == null){
//adapter = new ArrayAdapter<Task> im not sure how to implement this
    tasklist.setAdapter(adapter);
}else{
adapter.clear();
//adapter.addAll();       not sure what to write inside addAll
adapter.notifyDataSetChanged();
}

    }
}

正如你现在所看到的,我正在尝试在mainactivity中编写displayTasks方法,但我不能这样做

1 个答案:

答案 0 :(得分:0)

我认为你可以尝试在ScrollView中创建一个TableLayout,并使用ArrayList或对象数组填充数据。 试试这个 Dynamic TableLayout