Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

android - SimpleCursorAdapter - set 2 columns to 1 view

I have a SQLite Datebase from which I am displaying data in ListView by SimpleCursorAdapter. I have 2 columns but I want to displey them in 1 TextView. How can I do that? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try This Code , it may help you

Method For calling Listview from Oncreate

private void populateListViewFromDB1()
                {
                    Cursor cursor = helper1.getAllData(Sharedemail);



                            startManagingCursor(cursor);

                            String[] productname = new String[]
                                    {
                                    DataBaseHelper.KEY_NAMEVALUE,
                                    DataBaseHelper.KEY_TYPE,


                                    };

                            int[] viewproduct = new int[]
                                    {
                                    R.id.textView1,
                                    };

                            // Create Adapter 
                              MySimpleCursoradapter myCursorAdapter = new MySimpleCursoradapter
                                      (this,
                                       R.layout.listview,
                                       cursor,
                                       productname,
                                       viewproduct);


                            ListView List = (ListView) findViewById(R.id.listView1);
                            List.setAdapter(myCursorAdapter);
                       }


                }

MySimpleCursoradapter.java

public class MySimpleCursoradapter extends SimpleCursorAdapter {

public MySimpleCursoradapter(Context context, int layout,
            Cursor cur, String[] from, int[] to) {
        super(context, layout, cur, from, to);

    }

    @SuppressWarnings("static-access")
    @Override
    public View newView(Context con, Cursor c, ViewGroup arg2) {

        LayoutInflater inflater = (LayoutInflater) con
                .getSystemService(con.LAYOUT_INFLATER_SERVICE);

        View retView = inflater.inflate(R.layout.listview, null);

        return retView;
    }

public void bindView(View v, Context context, Cursor c) {

        String pname = c.getString(c.getColumnIndex(DataBaseHelper.KEY__NAMEVALUE));
        String issuetype = c.getString(c.getColumnIndex(DataBaseHelper.KEY_TYPE));

    TextView name_text = (TextView) v.findViewById(R.id.textView1);

name_text.setText(pname +":"+ issuetype);


}

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...