将数据库条目添加到列表视图

时间:2015-03-26 20:02:46

标签: android listview arraylist

我在我的Android应用程序中创建了一个数据库,允许用户输入作业信息。目前信息已存储但未按我的意愿列出。我希望将功能添加到View Assignments按钮,以便它返回到AssignmentsManager页面并列出输入的作业。

我相信我必须使用类似的东西;

listView=(ListView)findViewById(R.id.listView1); //initialise the listview
listAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0); //initialise an ArrayAdapter
listView.setAdapter(listAdapter); //set the adapter to the listview

并将分配添加到列表中;

  listAdapter.add(c.getString(0)+c.getString(1)+c.getString(2)+c.getString(3)+c.getString(4));

我不确定如何实现这一点。下面是我要添加作业的课程;

public class addassignment extends Activity {

DBAdapter db = new DBAdapter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

}

public void addAssignment(View v) {
    Log.d("test", "adding");
    // get data from form
    EditText nameTxt = (EditText) findViewById(R.id.editTitle);
    EditText dateTxt = (EditText) findViewById(R.id.editDuedate);
    EditText courseTxt = (EditText) findViewById(R.id.editCourse);
    EditText notesTxt = (EditText) findViewById(R.id.editNotes);

    db.open();
    long id = db.insertRecord(nameTxt.getText().toString(), dateTxt
            .getText().toString(), courseTxt.getText().toString(), notesTxt
            .getText().toString());
    db.close();

    nameTxt.setText("");
    dateTxt.setText("");
    courseTxt.setText("");
    notesTxt.setText("");
    Toast.makeText(addassignment.this, "Assignment Added",
            Toast.LENGTH_LONG).show();

}

public void viewAssignments(View v) {
    Intent i = new Intent(this, AssignmentManager.class);
    startActivity(i);
 }

}

这是应该显示列表的Assignments类;

public class AssignmentManager extends Activity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.assignmentmanager);

    Button addBtn = (Button) findViewById(R.id.add);
    addBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(AssignmentManager.this,
                    addassignment.class);
            startActivity(i);
        }
    });

    try {
        String destPath = "/data/data/" + getPackageName()
                + "/databases/AssignmentDB";
        File f = new File(destPath);
        if (!f.exists()) {
            CopyDB(getBaseContext().getAssets().open("mydb"),
                    new FileOutputStream(destPath));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    DBAdapter db = new DBAdapter(this);

    // ---add an assignment---

    db.open();
    long id = db.insertRecord("Android App", "14/02/2015", "Networks",
            "First Android Project");
    id = db.insertRecord("Java Development", "5/02/2015", "Java",
            "Complete Assignment");
    db.close();

    // ---get all Records---

    db.open();
    Cursor c = db.getAllRecords();
    if (c.moveToFirst()) {
        do {
            DisplayRecord(c);
        } while (c.moveToNext());
    }
    db.close();

    /*
     * //---get a Record--- db.open(); Cursor c = db.getRecord(2); if
     * (c.moveToFirst()) DisplayRecord(c); else Toast.makeText(this,
     * "No Assignments found", Toast.LENGTH_LONG).show(); db.close();
     */

    // ---update Record---
    /*
     * db.open(); if (db.updateRecord(1, "Android App", "29/02/2015",
     * "Networks", "First Android Project")) Toast.makeText(this,
     * "Update successful.", Toast.LENGTH_LONG).show(); else
     * Toast.makeText(this, "Update failed.", Toast.LENGTH_LONG).show();
     * db.close();
     */

    /*
     * //---delete a Record--- db.open(); if (db.deleteRecord(1))
     * Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG).show();
     * else Toast.makeText(this, "Delete failed.",
     * Toast.LENGTH_LONG).show(); db.close();
     */
}

public void CopyDB(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    // ---copy 1K bytes at a time---
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}

public void DisplayRecord(Cursor c) {
    Toast.makeText(
            this,
            "id: " + c.getString(0) + "\n" + "Title: " + c.getString(1)
                    + "\n" + "Due Date:  " + c.getString(2),
            Toast.LENGTH_SHORT).show();
}

public void addAssignment(View view) {

    Intent i = new Intent("addassignment");
    startActivity(i);
    Log.d("TAG", "Clicked");
 }

}

有人能告诉我应该在哪里实施添加功能的列表吗?

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上。如果我是你,我会简化流程,而不是最初单独添加作业,在创建适配器时添加ArrayList作业:

listAdapter= new ArrayAdapter<String>(this, 
             android.R.layout.simple_list_item_1, assignmentArrayList);

// now set the adapter to the ListView
listView.setAdapter(listAdapter); //set the adapter to the listview

设置适配器并对其进行任何更改(添加,删除等)后,请确保调用.notifyDataSetChanged()以便它知道刷新列表。

listAdapter.notifyDataSetChanged(); // notify the list adapter