自定义列表适配器不绑定到TextView的值

自定义列表适配器不绑定到TextView的值

本文介绍了自定义列表适配器不绑定到TextView的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建使用自定义适配器和下面的XML排布置一个机器人名单。不过,我已经实现了以下类,当我运行到我的模拟器的机器人:ID =@ + ID / firstLine中TextView中不会出现被设置文本的!ID / secondLine文本查看是否收到正确的文本!

表行项目XML布局:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =机器人:ATTR /列表preferredItemHeight
    机器人:填充=6DIP>    < ImageView的
        机器人:ID =@ + ID /图标
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginRight =6DIP
        机器人:contentDescription =@字符串/ TODO
        机器人:SRC =@绘制/ ic_launcher/>    <的TextView
        机器人:ID =@ + ID / secondLine
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =26dip
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentRight =真
        机器人:layout_toRightOf =@ ID /图标
        机器人:ellipsize =金字招牌
        机器人:单线=真
        机器人:文字=@字符串/ PURCHASE_DATE
        机器人:TEXTSIZE =12SP/>///这是一个不工作TextView的
    <的TextView
        机器人:ID =@ + ID / firstLine中
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ ID / secondLine
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真
        机器人:layout_alignWithParentIfMissing =真
        机器人:layout_toRightOf =@ ID /图标
        机器人:重力=center_vertical
        机器人:文字=@字符串/ home_depot
        机器人:TEXTSIZE =16SP/>< / RelativeLayout的>

适配器类别:

 公共类SimpleAdapter延伸BaseAdapter {
    私人活动活动;
    私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
    私有静态LayoutInflater吹气= NULL;
    //公共ImageLoader的ImageLoader的;    公共SimpleAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
        活性=一个;
        数据= D;
        吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @覆盖
    公众诠释的getCount(){
        返回data.size();
    }
    @覆盖
    公共对象的getItem(INT位置){
        返回的位置;
    }
    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }
    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看VI = convertView;
        如果(convertView == NULL)
            VI = inflater.inflate(R.layout.list_item,NULL);        TextView中而purchaseDate =(TextView中)vi.findViewById(R.id.secondLine);
        TextView的畅销产品=(TextView中)vi.findViewById(R.id.firstLine);
        HashMap的<字符串,字符串>员工=新的HashMap<字符串,字符串>();
        员工= data.get(位置);  //如果不是这种设置为卖方的TextView文本?
        purchaseDate.setText(employee.get(HistoryListView.KEY_PURCHASE_DATE));
        seller.setText(employee.get(HistoryListView.KEY_SELLER));        返回VI;
    }
}

清单活动:

 公共类HistoryListView延伸活动{    静态最后弦乐KEY_SELLER =卖方;
    静态最后弦乐KEY_PURCHASE_DATE =购买日期;
    静态最后弦乐KEY_SALE_AMMOUNT =sale_ammount;
    静态最后弦乐PURCHASE_DATE =购买日期;
    静态最后弦乐USD =$;    ListView控件列表;
    SimpleAdapter适配器;
    公众的String [] = sellerList {家得宝,十一7,好事多,澳美客牛排馆,Philz Coffee公司};
    公众的String [] = purchaseDateList {13年1月12日,13年1月23日,13年2月16日,13年2月17日,13年2月18日};
    公众的String [] = saleAmmountList {1023.10,243.98,107.54,45.88,21.99};    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.history_list);
        ArrayList的<&HashMap的LT;字符串,字符串>> expenseList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        意向意图= getIntent();        的for(int i = 0;我小于5;我++){
            HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
            (KEY_SELLER,sellerList [I])map.put;
            (KEY_PURCHASE_DATE,PURCHASE_DATE + purchaseDateList [I])map.put;
            //map.put(KEY_SALE_AMMOUNT,saleAmmountList [I]);            expenseList.add(地图);
        }        ListView控件列表=(ListView控件)findViewById(R.id.list);        适配器=新SimpleAdapter(这一点,expenseList);
        list.setAdapter(适配器);
    } // END的onCreate
} // END HistoryListView


解决方案

您在列表行项目的问题试试这个
测试显示它的图标和两行

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>    < ImageView的
        机器人:ID =@ + ID /图标
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:SRC =@绘制/ ic_launcher/>    <的TextView
        机器人:ID =@ + ID / firstLine中
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =5DP
        机器人:layout_marginTop =5DP
        机器人:layout_toRightOf =@ + ID /图标
        机器人:文字=TextView的/>    <的TextView
        机器人:ID =@ + ID / secondLine
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / firstLine中
        机器人:layout_below =@ + ID / firstLine中
        机器人:文字=TextView的/>< / RelativeLayout的>

I'm trying to create an android list using a custom adapter and the following XML row layout. I have implemented the following classes however when I run it onto my emulator the "android:id="@+id/firstLine" TextView does not appear to be setting the text! The "id/secondLine" Text View does receive the correct text!

List Row Item XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="@string/TODO"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="@string/purchase_date"
        android:textSize="12sp" />

///  THIS IS THE TextView that's not working
    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:text="@string/home_depot"
        android:textSize="16sp" />

</RelativeLayout>

Adapter Class:

    public class SimpleAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    //public ImageLoader imageLoader;

    public SimpleAdapter(Activity a, ArrayList<HashMap<String, String>> d){
        activity = a;
        data = d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_item, null);

        TextView purchaseDate = (TextView)vi.findViewById(R.id.secondLine);
        TextView seller = (TextView)vi.findViewById(R.id.firstLine);


        HashMap<String, String> employee = new HashMap<String, String>();
        employee = data.get(position);

  //  Should this not be setting the text for the "seller" TextView?
        purchaseDate.setText(employee.get(HistoryListView.KEY_PURCHASE_DATE));
        seller.setText(employee.get(HistoryListView.KEY_SELLER));

        return vi;
    }
}

List Activity:

public class HistoryListView extends Activity {

    static final String KEY_SELLER="seller";
    static final String KEY_PURCHASE_DATE="purchase_date";
    static final String KEY_SALE_AMMOUNT="sale_ammount";
    static final String PURCHASE_DATE="Purchase Date: ";
    static final String USD="$ ";

    ListView list;
    SimpleAdapter adapter;
    public String[] sellerList = {"Home Depot","7 Eleven","Costco","Outback Steak House","Philz Coffee"};
    public String[] purchaseDateList={"1/12/13", "1/23/13", "2/16/13", "2/17/13", "2/18/13"};
    public String[] saleAmmountList={"1023.10","243.98","107.54","45.88","21.99"};

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_list);
        ArrayList<HashMap<String,String>> expenseList=new ArrayList<HashMap<String, String>>();
        Intent intent = getIntent();

        for(int i=0; i<5; i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(KEY_SELLER, sellerList[i]);
            map.put(KEY_PURCHASE_DATE, PURCHASE_DATE+purchaseDateList[i]);
            //map.put(KEY_SALE_AMMOUNT, saleAmmountList[i]);

            expenseList.add(map);
        }

        ListView list=(ListView)findViewById(R.id.list);

        adapter=new SimpleAdapter(this, expenseList);
        list.setAdapter(adapter);
    }//END onCreate
}//END HistoryListView
解决方案

You have problem in List Row Item try this onetested and it show icon and two lines

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/icon"
        android:text="TextView" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/firstLine"
        android:layout_below="@+id/firstLine"
        android:text="TextView" />

</RelativeLayout>

这篇关于自定义列表适配器不绑定到TextView的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:28