在列表视图中显示数据 [英] Displaying data in listview

查看:246
本文介绍了在列表视图中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个列表视图,其中我必须为每一列的行显示两到三年的数据,但我不能这样做,我得到的数据在图像的,但我想显示在图像b,我可以每column.For例如列国要显示的项目数量和款式数量值的每一行中仅显示一个数据,我能够只显示项目编号而不是风格号。

  ** DataDisplay XML **
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>
    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_gravity =中心
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =25dp
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / textView2
        机器人:layout_gravity =中心
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =10dp
        机器人:TEXTSIZE =25dp
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / textView3
        机器人:layout_gravity =中心
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =10dp
        机器人:TEXTSIZE =25dp
        机器人:文字=TextView的/>
    <的TextView
        机器人:ID =@ + ID / textView4
        机器人:layout_gravity =中心
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =10dp
        机器人:TEXTSIZE =25dp
        机器人:文字=TextView的/>
    <的TextView
        机器人:ID =@ + ID / textView5
        机器人:layout_gravity =中心
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =10dp
        机器人:TEXTSIZE =25dp
        机器人:文字=TextView的/>< / LinearLayout中>
**列表视图XML **
  <的LinearLayout的android:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        < ListView的机器人:ID =@ + ID /的ListView
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_weight =1
            机器人:分=#00000000
            机器人:dividerHeight =5DP
           />
< / LinearLayout中>

适配器

 公共类CustomAdapter延伸BaseAdapter {
    的String []的结果;
    的String [] RESULT1;
    的String [] RESULT2;
    的String [] result3;
    的String [] result4;
    上下文语境;
    INT [] imageId;
    私有静态LayoutInflater吹气= NULL;
    公共CustomAdapter(ItemActivity mainActivity,字符串[] prgmNameList,字符串[] prgmNameList1,字符串[] prgmNameList2,字符串[] prgmNameList3,字符串[] prgmNameList4){
        // TODO自动生成构造函数存根
        结果= prgmNameList;
        RESULT1 = prgmNameList1;
        结果2 = prgmNameList2;
        result3 = prgmNameList3;
        result4 = prgmNameList4;        上下文= mainActivity;        充气=(LayoutInflater)上下文。
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回result.length;
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    公共类持有人
    {
        TextView的电视;
        TextView的TV1;
        TextView中TV2;
        TextView的TV3;
        TextView的TV4;
    }
    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
        // TODO自动生成方法存根
        持有人持有人=新的持有人();
        查看rowView;
        rowView = inflater.inflate(R.layout.tabitem,NULL);
        holder.tv =(TextView中)rowView.findViewById(R.id.textView1);
        holder.tv1 =(TextView中)rowView.findViewById(R.id.textView2);
        holder.tv2 =(TextView中)rowView.findViewById(R.id.textView3);
        holder.tv3 =(TextView中)rowView.findViewById(R.id.textView4);
        holder.tv4 =(TextView中)rowView.findViewById(R.id.textView5);
        holder.tv.setText(结果[位置]);
        holder.tv1.setText(RESULT1 [位置]);
        holder.tv2.setText(RESULT2 [位置]);
        holder.tv3.setText(result3 [位置]);
        holder.tv4.setText(result4 [位置]);
        rowView.setBackgroundColor(Color.parseColor(#F1F1FF));        rowView.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                Toast.makeText(背景下,你点击+结果[位置],Toast.LENGTH_LONG).show();
            }
        });
        返回rowView;
    }}

从活动名为

 公共静态的String [] = prgmNameList {123456,456665,123445,5675343};
    公共静态的String [] = prgmNameList1 {15.56,15.562,15.67,15.455};
    公共静态的String [] = prgmNameList2 {9999,9999,9999,9999};
    公共静态的String [] = prgmNameList3 {99%,99%,99%,99%};
    公共静态的String [] = prgmNameList4 {9999,9999,9999,9999};
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);        的setContentView(R.layout.itemlist);        。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.item);
        上下文=这一点;        LV =(ListView控件)findViewById(R.id.listView);
        lv.setAdapter(新CustomAdapter(这一点,prgmNameList,prgmNameList1,prgmNameList2,prgmNameList3,prgmNameList4));
    }


解决方案

下面是我做的。做一个xml文件是这样的:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向
    机器人:layout_weight =1>    <的LinearLayout
        机器人:layout_width =100dp
        机器人:layout_height =match_parent
        机器人:方向=垂直
        >        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#FF0000/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#00FF00/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#0000FF/>
    < / LinearLayout中>    <的LinearLayout
        机器人:layout_width =100dp
        机器人:layout_height =match_parent
        机器人:layout_marginLeft =10dp
        机器人:方向=垂直>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#FF0000/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#00FF00/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#0000FF/>
    < / LinearLayout中>
    <的LinearLayout
        机器人:layout_width =100dp
        机器人:layout_height =match_parent
        机器人:layout_marginLeft =10dp
        机器人:方向=垂直>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#FF0000/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#00FF00/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#0000FF/>
    < / LinearLayout中>
    <的LinearLayout
        机器人:layout_width =100dp
        机器人:layout_height =match_parent
        机器人:layout_marginLeft =10dp
        机器人:方向=垂直>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#FF0000/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#00FF00/>        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:背景=#0000FF/>
    < / LinearLayout中>
< / LinearLayout中>

这是您自定义的列表视图 XML文件。现在,在getView方法的setText这一切TextView的像你一样的XML文件。

看看下面的屏幕:

I am trying to design a listview wherein i have to display two to three data for rows of each column,but i am not able to do so,i am getting the data has in image a but i want to display has in image b,i can display only single data in each row of each column.For example the column states to display the value of item number and style number ,i am able to display only item number but not style number.

**DataDisplay xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="25dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="25dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView4"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="25dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView5"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="25dp"
        android:text="TextView" />

</LinearLayout>
**Listview xml** 
  <LinearLayout  android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="#00000000"
            android:dividerHeight="5dp"
           />
</LinearLayout>

adapter

public class CustomAdapter extends BaseAdapter{
    String [] result;
    String [] result1;
    String [] result2;
    String [] result3;
    String [] result4;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomAdapter(ItemActivity mainActivity, String[] prgmNameList,String[] prgmNameList1,String[] prgmNameList2,String[] prgmNameList3,String[] prgmNameList4) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        result1=prgmNameList1;
        result2=prgmNameList2;
        result3=prgmNameList3;
        result4=prgmNameList4;

        context=mainActivity;

        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.tabitem, null);
        holder.tv=(TextView) rowView.findViewById(R.id.textView1);
        holder.tv1=(TextView)rowView.findViewById(R.id.textView2);
        holder.tv2=(TextView) rowView.findViewById(R.id.textView3);
        holder.tv3=(TextView) rowView.findViewById(R.id.textView4);
        holder.tv4=(TextView) rowView.findViewById(R.id.textView5);
        holder.tv.setText(result[position]);
        holder.tv1.setText(result1[position]);
        holder.tv2.setText(result2[position]);
        holder.tv3.setText(result3[position]);
        holder.tv4.setText(result4[position]);
        rowView.setBackgroundColor(Color.parseColor("#F1F1FF"));

        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
            }
        });
        return rowView;
    }

}

called from activity

 public static String [] prgmNameList={"123456","456665","123445","5675343"};
    public static String [] prgmNameList1={"15.56","15.562","15.67","15.455"};
    public static String [] prgmNameList2={"9999","9999","9999","9999"};
    public static String [] prgmNameList3={"99%","99%","99%","99%"};
    public static String [] prgmNameList4={"9999","9999","9999","9999"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.itemlist);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.item);
        context=this;

        lv=(ListView) findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmNameList1,prgmNameList2,prgmNameList3,prgmNameList4));
    }

解决方案

Here is what i did. Make a xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FF00" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#0000FF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FF00" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#0000FF" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FF00" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#0000FF" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FF00" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#0000FF" />
    </LinearLayout>
</LinearLayout>

This is your custom listview xml file. Now in getView method setText to this all textview as you did for your xml file.

Check out below screen:

这篇关于在列表视图中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆