如何在listview的行中显示两个值?

时间:2013-05-07 07:36:02

标签: android listview

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

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

listvalues 这里是一个arraylist ......

对arraylist中的值进行格式化

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

getListDetails()

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;
}

是否可以在listview的每一行显示两个项目?  就像我想要显示属性名称和该属性的值一样 名字 - abc 中间名-XYZ 姓氏,PQR 我怎么做??? 我需要在xml文件中进行更改吗?请让我知道! 请帮忙!提前谢谢

7 个答案:

答案 0 :(得分: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;
}

}

答案 1 :(得分:0)

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

AndroidListView

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

Multi-clickable ListView

答案 4 :(得分:0)

在res / layout /文件夹中创建一个新的xml文件,在TextView内有两个LinearLayout s,并将此布局的id作为ArrayAdapter构造函数中的第二个参数传递。那应该是诀窍

答案 5 :(得分: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); 

您需要找到Name的子字符串作为名字,中间名,姓氏。并将它们放在textview中。

答案 6 :(得分: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);

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