1

我正在使用以下代码在数据库的列表视图中显示详细信息:

ListView lv=getListView();
            ListAdapter adapter=new ArrayAdapter<String>(DisplayDetails.this, R.layout.view_animal_entry, R.id.animalName, listvalues);
            lv.setAdapter(adapter);

这里的listvalues是一个数组列表...

获取数组列表中的值

    WayDataBase way=new WayDataBase(getApplicationContext());           
        ArrayList<String> listvalues=way.getListDetails(appno);

获取列表详细信息()

public List<String[]> getListDetails(String rowid)
{
    openOrCreateDatabase();
    createAllValuesTable();

    List<String[]> recentdata=new ArrayList<String[]>();
    //String selectQuery="select * from allvaluestable where ROWID="+rowid;
    Cursor c=db.rawQuery("select * from allvaluestable where _id ='"+rowid+"'", null);
    String[] values = null;
    if (c != null) {
        c.moveToLast();

        while (c.isBeforeFirst() != true) {
            for (int i = 0; i < 280; i++) {
                values[i]=c.getString(i);
                System.out.println("Db print**" + recentdata.get(i));
            }
            c.moveToPrevious();
        }
        recentdata.add(values);
        c.close();
    } else {
        recentdata.add(values);
    }
    closeDatabase();

    return recentdata;
}

是否可以在列表视图的每一行中显示两个项目???就像我想显示属性名称和该属性的值一样,比如名字-abc 中间名-xyz 姓氏-pqr 我该怎么做???我必须在 xml 文件中进行哪些更改?也请告诉我!!请帮忙!提前致谢

4

7 回答 7

2

您需要制作一个自定义适配器并覆盖 getView()

public class AdapterWine extends ArrayAdapter<Wine> {

private Context context;
private int layoutResourceId;
List<Wine> wines;
List<Wine> originalValues;

List<Wine> arrayList;      

public AdapterWine(Context context, int layoutResourceId, List<Wine> wines) {
    super(context, layoutResourceId, wines);

    this.context = context;
    this.layoutResourceId = layoutResourceId;
    this.wines = wines;
    this.originalValues = wines; 
}

@Override
public int getCount() {
    return wines.size();
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ViewHolder holder = null;

    if(row == null) {

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ViewHolder();
        holder.iconImg = (ImageView)row.findViewById(R.id.iconImg);
        holder.topText = (TextView)row.findViewById(R.id.topText);
        holder.bottomText = (TextView)row.findViewById(R.id.bottomText);

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

    Wine wine = wines.get(position);

    switch(wine.getType()) {

        case blanc:
            holder.iconImg.setImageResource(R.drawable.wineiconwhite);
            break;

        case rouge:
            holder.iconImg.setImageResource(R.drawable.wineiconrouge);
            break;

        case rose:
            holder.iconImg.setImageResource(R.drawable.wineiconrose);
            break;
    }

    holder.topText.setText(wine.getName());
    holder.bottomText.setText(wine.getCepage()+" - "+wine.getMillesime());

    return row;
}

private static class ViewHolder {
    ImageView iconImg;
    TextView topText, bottomText;
}

}

于 2013-05-07T07:39:48.587 回答
0

为 listView 创建一个 Adapter 并覆盖 getView() 函数。在 getView() 函数中,您必须在每次自动调用它时创建两个 TextView 并将两个值放入您要显示的值中。

        public class ListViewAdapter extends BaseAdapter 
        {

               private Context context;
               private View listView; 
               List<String> name;

        public ListViewAdapter (Context c, List<String> name,View v) 
        {
               context = c;
               this.listView = listView;
               this.name = name;
        } 

        public int getCount() {
           return name.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
                 LinearLayout row=new LinearLayout(context);

         TextView name=new TextView(context);
         name.setText(//firstName);
         name.setTextSize(18);
         name.setTextColor(theme.getItemColorGrid());

         TextView mname=new TextView(context);
         mname.setText(//middleName);
         mname.setTextSize(18);
         mname.setTextColor(theme.getItemColorGrid());

         TextView lname=new TextView(context);
         lname.setText(//lastName);
         lname.setTextSize(18);
         lname.setTextColor(theme.getItemColorGrid());

                 row.addView(fname);
                 row.addView(mname);
                 row.addView(lname);

                 return row;

}

     //Calling the adapter

              ListView listView=new ListView(ActivityMName.this);
              listView.setAdapter(new ListViewAdapter(ActivityName.this,listNames,listView); 

您需要找到名称的子字符串作为名字,中间名,姓氏。并将它们放在文本视图中。

于 2013-05-07T07:55:29.997 回答
0

尝试这个。

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

WayDataBase way=new WayDataBase(getApplicationContext());           
List<String[]> listvalues=way.getListDetails(appno);

for (String[] name : listvalues) {

     HashMap<String, String> map = new HashMap<String, String>();

                                 map.put("firstname", name[0]);
                                 map.put("middlename", name[1]); 
                                 mylist.add(map);

            // Keys used in Hashmap
                     String[] from1 = {"firstname", "middlename"};

                        // Ids of views in listview_layout
                   int[] to1 = { R.id.one, R.id.two};

                // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item

   SimpleAdapter adapter1 = new SimpleAdapter(getBaseContext(), mylist, R.layout.row1, from1, to1);

            lv.setAdapter(adapter1);

希望这会给你一些解决方案。更多参考

于 2013-05-07T09:41:44.697 回答
0

创建自定义适配器并覆盖 getView()

安卓列表视图

于 2013-05-07T07:41:37.990 回答
0

在 view_animal_entry.xml 中放置两个 TextView,覆盖适配器中的 getView 并设置这两个 TextView

于 2013-05-07T07:42:32.780 回答
0

您可以查看我写的关于如何从 ArrayList 创建自定义适配器的指南:

可多次点击的 ListView

于 2013-05-07T07:42:48.140 回答
0

在 res/layout/ 文件夹中创建一个新的 xml 文件,其中包含两个TextViewsLinearLayout并将此布局的 id 作为构造函数中的第二个参数传递ArrayAdapter。这应该够了吧

于 2013-05-07T07:45:12.400 回答