将SQLite条目打印到ListView中

时间:2014-11-02 15:23:28

标签: java android sqlite listview

我在学习SQLite世界的细节方面遇到了一些麻烦。我有一些代码允许我将数据输入数据库。但我想要做的是将这些数据返回到列表视图中。目前,我想要做的就是在添加新条目后将每行打印在一个吐司中。有人可以告诉我如何更改我的代码以在列表视图中打印它?或者甚至看看我的代码,看看我是以正确的方式进行的。感谢

这是我正在使用的调用显示记录功能的代码

    //---get all Records---
    com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
    db.open();
    Cursor c = db.getAllRecords();
    if (c.moveToFirst())
    {
        do {
            DisplayRecord(c);
        } while (c.moveToNext());
    }
    db.close();

这是显示记录功能

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

我知道我需要更改第二个功能,但我不知道如何将其打印成列表视图

3 个答案:

答案 0 :(得分:1)

创建ListView。然后将cursorAdapter提供给ListView作为适配器,将数据库中的数据绑定到ListView

您在项目中下载的SDK示例文件夹中有一些示例ApiDemos

答案 1 :(得分:1)

这是从数据库获取数据并插入Arraylist并插入arrayAdapter而不是在listview中显示的代码。

我刚刚对您现有的代码进行了一些编辑。

com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
    db.open();

ArrayList<String> data_list=new ArrayList<String>();
ListView lv=(ListView)findViewById(R.id.listView1);
Cursor c = db.getAllRecords();
    if (c.moveToFirst())
    {
        do {
            data_list.add(c.getString(0));
            DisplayRecord(c);
        } while (c.moveToNext());
    }
ArrayAdapter<String> aa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data_list);
        lv.setAdapter(aa);

lv - 是ListView的对象。

答案 2 :(得分:0)

您需要拥有,1)listview,2)对象类,3)自定义适配器

我刚刚尝试按照您的要求实施。 由于我没有db我没有尝试运行。要点就是五个你的想法。 因为listview是我们在android中更频繁使用的小部件。根据我的知识,这是最好的方法。

需要的布局,

activity_list.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ListActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

row_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >
    <TextView
        android:id="@+id/Item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item" />

    <TextView
        android:id="@+id/Litres"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="litres" />
</LinearLayout>

ListActivity.java:

public class ListActivity extends Activity {

    ArrayList<RowData> rowDataArrayList = new ArrayList<RowData>();
    ListView list;
    ListAdapter listAdapter;

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

        list = (ListView) findViewById(R.id.list);
        listAdapter = new ListAdapter(ListActivity.this, rowDataArrayList);
        list.setAdapter(listAdapter);
        getDataFromDB();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list, menu);
        return true;
    }

    private void getDataFromDB() {
        rowDataArrayList.clear();

        com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(
                this);
        db.open();
        Cursor c = db.getAllRecords();
        if (c.moveToFirst()) {
            do {
                /**
                 * Set your data in class
                 */
                RowData rowData = new RowData();
                rowData.setId(c.getString(0));
                rowData.setItem(c.getString(1));
                rowData.setLitres(c.getString(2));
                rowDataArrayList.add(rowData);

            } while (c.moveToNext());
        }
        db.close();
        /**
         * To reflect new data set change in listview
         */
        listAdapter.notifyDataSetChanged();
    }
}

RowData.java:模型[pojo]类,用于保存数据并在自定义适配器中绑定。

public class RowData {
    String Id;
    String Item;
    String Litres;

    public String getId() {
        return Id;
    }
    public void setId(String id) {
        Id = id;
    }
    public String getItem() {
        return Item;
    }
    public void setItem(String item) {
        Item = item;
    }
    public String getLitres() {
        return Litres;
    }
    public void setLitres(String litres) {
        this.Litres = litres;
    }

}

ListAdapter.java:要在listview中绑定的自定义适配器

public class ListAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<RowData> rowDataArrayList = new ArrayList<RowData>();

    public ListAdapter(Context context, ArrayList<RowData> rowData) {
        mContext = context;
        rowDataArrayList = rowData;
    }

    @Override
    public int getCount() {
        return rowDataArrayList == null ? 0 : rowDataArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        if (convertView == null) {
            Holder holder = new Holder();
            view = View.inflate(mContext, R.layout.row_item, null);
            holder.tvtItem = (TextView) view.findViewById(R.id.Item);
            holder.tvtLitres = (TextView) view.findViewById(R.id.Litres);
            view.setTag(holder);
        } else {
            view = convertView;
        }

        Holder holder = (Holder) view.getTag();
        holder.tvtItem.setText(rowDataArrayList.get(position).getItem());
        holder.tvtLitres.setText(rowDataArrayList.get(position).getLitres());

        return view;
    }

    class Holder {
        TextView tvtItem;
        TextView tvtLitres;
    }
}
public class ListAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<RowData> rowDataArrayList = new ArrayList<RowData>();

    public ListAdapter(Context context, ArrayList<RowData> rowData) {
        mContext = context;
        rowDataArrayList = rowData;
    }

    @Override
    public int getCount() {
        return rowDataArrayList == null ? 0 : rowDataArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        if (convertView == null) {
            Holder holder = new Holder();
            view = View.inflate(mContext, R.layout.row_item, null);
            holder.tvtItem = (TextView) view.findViewById(R.id.Item);
            holder.tvtLitres = (TextView) view.findViewById(R.id.Litres);
            view.setTag(holder);
        } else {
            view = convertView;
        }

        Holder holder = (Holder) view.getTag();
        holder.tvtItem.setText(rowDataArrayList.get(position).getItem());
        holder.tvtLitres.setText(rowDataArrayList.get(position).getLitres());

        return view;
    }

    class Holder {
        TextView tvtItem;
        TextView tvtLitres;
    }
}