如何在android中显示列表视图的特定列

时间:2011-06-07 09:23:17

标签: android sqlite

我想将setName列显示为列表视图,如何在android中实现。 我正在分享我的代码。

private String lv_arr[];
String selectList = "select setName from Displaysettings";      
DBConnect conn1 = new DBConnect(getApplicationContext(), "colorCode");      
conn1.execNonQuery(selectList);
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));

提前感谢。

2 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情:

String[] setNameValues = new String[] {
            "setName1",
            "setName2",
            "setName3"
    };

    // Create a simple array adapter (of type string) 
    //with the setName values returned by conn1.execNonquery
    ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, setNameValues);

因此,您应该将conn1.execNonQuery(selectList)结果转换为字符串数组

更新

如果你的conn1.execNonQuery返回游标,你可以使用游标适配器。

例如(某些行被删除以使其清楚)

public class DiagramListCursorAdapter extends CursorAdapter {

    private boolean dataValid;
    private boolean autoRequery;
    private boolean autoRequeryInProgress;
    private int count = -1;

    public DiagramListCursorAdapter(Context context, Cursor c, boolean autorequery) {
        super(context, c, autorequery);
    }

    @Override
    public View newView(final Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.diagram_item, null);
        DiagramListCursorWrapper wrapper;
        wrapper = new DiagramListCursorWrapper(v);
        v.setTag(wrapper);
        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor cursor) {
        DiagramListCursorWrapper wrapper = (DiagramListCursorWrapper) v.getTag();
        wrapper.populateFrom(cursor);
    }

    void refresh() {
        if (getCursor() != null) {
            getCursor().requery();
        }
    }


    public class DiagramListCursorWrapper {
        TextView title;

        public DiagramListCursorWrapper(View v) {
            setupViews(v, null);
        }

        public DiagramListCursorWrapper(View v, Context context) {
            setupViews(v, context);
        }

        private void setupViews(View v, Context context) {
            title = (TextView) v.findViewById(R.id.title);
            icon = (ImageView) v.findViewById(R.id.icon);

        }

        public void populateFrom(Cursor cursor) {
            if (cursor != null) {
                try {
                    title.setText(cursor.getString(cursor.getColumnIndex(DiagramsADO.DIAGRAM_TITLE)));
                } catch (Exception e) {
                }
            }

        }

    }

}

我希望这有助于你

答案 1 :(得分:0)

更好的方法是将所有结果提取到Cursor中,然后使用光标并将内容添加到ListView

相关问题