如何在listview中显示两列。列数据来自SQLite数据库

时间:2010-12-28 12:21:19

标签: android listview android-edittext

如何在列表视图中显示列数据来自sqlite数据库的两列?在该列中,第一列是TextView,另一列是EditView。 我试过下面的代码。但是我得到了以下异常

unable to start the activity ComponentInfo:java.lang.NullPointerException.

代码如下所示。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.categorynew);
    lv = (ListView)findViewById(android.R.id.list);
    getList();
}

public void getList()
{  
    int x = 0;
    cur=db.rawQuery("SELECT * FROM detaillist ORDER BY symbol",null);
    ArrayList<String> al = new ArrayList<String>();
    ArrayList<String> al1 = new ArrayList<String>();
    cur.moveToFirst();
    while (cur.isAfterLast() == false) {
        al.add(cur.getString(cur.getColumnIndex("symbol")));
        al1.add(cur.getString(cur.getColumnIndex("position")));

        cur.moveToNext();
    }
    cur.close();
    String[] str = new String[al.size()];
    strArray = al.toArray(str);
    String[] str1 = new String[al1.size()];
    strArray1 = al1.toArray(str1);
    lv.setAdapter(new EfficientAdapter(this));
} 

private static class EfficientAdapter extends BaseAdapter {
    static class ViewHolder {
        TextView text;
        EditText text2;

    }

    private LayoutInflater mInflater;
    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }
    public int getCount() {
        return symbols.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            // convertView = mInflater.inflate(android.R.layout.simple_list_item_1, null);
            convertView = mInflater.inflate(R.layout.editcategorylist, null);
            holder = new ViewHolder();

            holder.text = (TextView) convertView.findViewById(R.id.symbol);
            holder.text2 = (EditText) convertView.findViewById(R.id.postion);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(strArray[position]);

        Log.e("text",holder.text.toString());
        holder.text2.setText(strArray1[position]);
        Log.e("text2",holder.text2.toString());   

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

此链接的实现稍有不同,但您可以使用本教程的各个部分并自行烹饪。如果愿意的话,这是一个起点。它使用数据库并以表格形式显示结果。

http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/