2012-08-22 68 views
16

Tôi đang lập trình một ứng dụng Android nên sử dụng cơ sở dữ liệu để lưu trữ dữ liệu và đọc từ đó. Sử dụng this tutorial (on archive.org) Tôi đã nhận ứng dụng để tạo cơ sở dữ liệu và tôi có thể tạo các mục mới, tuy nhiên, tôi không biết, cách đọc cơ sở dữ liệu để lấy dữ liệu được lưu trữ trong ListView. Tôi biết có rất nhiều câu hỏi tương tự trên trang web này nhưng có vẻ như không có câu hỏi nào trong số họ áp dụng cho cách này, cơ sở dữ liệu từ hướng dẫn hoạt động.Android: Sử dụng SimpleCursorAdapter để lấy Dữ liệu từ Cơ sở dữ liệu sang ListView

Code:

import java.util.Calendar; 

import maturarbeit.nicola_pfister.studenttools.database.DBAdapter; 
import android.app.AlertDialog.Builder; 
import android.app.ListActivity; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v4.widget.SimpleCursorAdapter; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.ListView; 


public class Marks extends ListActivity { 

DBAdapter db = new DBAdapter(this); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.marks); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    db.close(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    db.open(); 

    getData(); 
} 

@SuppressWarnings("deprecation") 
private void getData() { 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_list_item_1, 
      db.getAllMarks(), 
      new String[] { "value" }, 
      new int[] { android.R.id.text1 }); 

    ListView listView = (ListView) findViewById(R.id.marks_list); 
    listView.setAdapter(adapter); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.marks, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_add: 
     Calendar cal = Calendar.getInstance(); 
     int day = cal.get(Calendar.DAY_OF_MONTH); 
     int month = cal.get(Calendar.MONTH); 
     final String date = day + "." + month; 
     Builder builder = new Builder(this); 
     builder 
      .setTitle(R.string.dialog_addmarks) 
      .setItems(R.array.markslist, new OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        @SuppressWarnings("unused") 
        long id; 
        String selection = getResources().getStringArray(R.array.markslist)[which]; 
        id = db.insertMark(date, "Default", selection); 
        } 
      }) 
      .show(); 
     getData(); 
     break; 
    case R.id.menu_delete: 
      //Deleting function yet to be implemented. 
     break; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

Edit: ID ListView đã sai vì nó phải được android: danh sách.

Trả lời

53

Sử dụng định dạng cơ sở dữ liệu trong hướng dẫn mà bạn liên kết đến, mỗi hàng có một _id, isbn, titlepublisher. Bây giờ chúng ta hãy giả sử rằng bạn muốn hiển thị tất cả các danh hiệu trong một ListView:

db = new DBAdapter(this); 
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
     android.R.layout.simple_list_item_1, 
     db.getAllTitles(), 
     new String[] { "title" }, 
     new int[] { android.R.id.text1 }); 

ListView listView = (ListView) findViewById(R.id.list); 
listView.setAdapter(adapter); 

(Bạn không cần phải lặp qua các con trỏ chính mình, một adapter làm việc này cho bạn!)

cuối cùng hai các tham số trong hàm dựng SimpleCursorAdapter của bạn là những gì bạn đang thiếu. Chúng là các tham số "từ" và "đến":

  • Chúng tôi muốn lấy tên của mỗi cuốn sách được lưu trữ trong tên cột title, đây là nơi chúng tôi lấy thông tin "từ".
  • Tiếp theo, chúng ta cần phải cho biết nơi "để" đi: android.R.id.text1 là một TextView trong bố cục android.R.layout.simple_list_item_1. (Bạn có thể khai thác thông qua SDK của bạn và xem file simple_list_item_1.xml mình hay chỉ là tin tưởng tôi cho thời điểm này.)

Bây giờ cả "từ" và "đến" thông số là mảng bởi vì chúng tôi có thể vượt qua nhiều hơn một cột thông tin, hãy thử bộ điều hợp này:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
     android.R.layout.simple_list_item_2, 
     db.getAllTitles(), 
     new String[] { "title", "publisher" }, 
     new int[] { android.R.id.text1, android.R.id.text2 }); 

Với bộ điều hợp này, sách trong cơ sở dữ liệu của họ sẽ được hiển thị theo tên, sau đó nhà xuất bản. Tất cả những gì chúng tôi phải làm là sử dụng bố cục android.R.layout.simple_list_item_2 có hai trường và xác định cột nào sẽ đến TextView nào.

Tôi hy vọng điều đó sẽ giúp ích một chút. Có rất nhiều thứ để tìm hiểu nhưng có lẽ điều này sẽ cung cấp cho bạn một số vấn đề cơ bản.


The last

Off đỉnh đầu của tôi, để làm mới ListView sau khi thêm dữ liệu mới thử điều này:

public void onClick(DialogInterface dialog, int which) { 
    String selection = getResources().getStringArray(R.array.markslist)[which]; 
    db.insertMark(date, "Default", selection); 
    cursor.requery(); 
    adapter.notifyDataSetChanged(); 
} 

Bạn sẽ phải xác định adapter và tạo ra một biến cho cursor nhưng điều đó đơn giản:

public class Marks extends ListActivity { 
    SimpleCursorAdapter adapter; 
    Cursor cursor; 
    DBAdapter db = new DBAdapter(this); 
    ... 

Và thay đổi getData() cho phù hợp:

private void getData() { 
    cursor = db.getAllMarks(); 
    adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_list_item_1, 
      cursor, 
      new String[] { "value" }, 
      new int[] { android.R.id.text1 }); 
    ... 
} 

Chúc may mắn!

+0

Cảm ơn bạn. Tôi nghĩ rằng tôi nhận được, những gì bạn đang cố gắng giải thích. Tôi đã sử dụng bộ điều hợp của bạn nhưng tôi vẫn nhận được ngoại lệ làm treo ứng dụng của tôi. Tôi cho rằng lỗi ở đâu đó khác? – BlueHazard

+0

Sao chép và dán LogCat (sử dụng liên kết chỉnh sửa ở phía dưới bên trái câu hỏi của bạn). Tôi sẽ xem qua một chút. Gắn thẻ tôi trong nhận xét (với "@Sam") khi bạn đã thực hiện việc này. – Sam

+0

Tôi đã thêm LogCat vào câu hỏi của mình. Cảm ơn vì đã giúp tôi. – BlueHazard

0

Bộ điều hợp không được sử dụng trên mỗi mục trong con trỏ, một bộ điều hợp được tạo cho toàn bộ con trỏ.Bạn có thể đặt chế độ xem danh sách này để sử dụng con trỏ đó. Hãy thử một số hướng dẫn SimpleCursorAdapter như this