android-自定义Listview不应该显示

android-自定义Listview不应该显示,第1张

概述我已经为列表项和自定义listadapter创建了格式.问题是我的列表视图没有按我的设计显示.这是每个列表项的XML.<RelativeLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="fill_parent"android:layout_height="?android:attr/l

我已经为列表项和自定义listadapter创建了格式.问题是我的列表视图没有按我的设计显示.

这是每个列表项的XML.

<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/icon1"        androID:layout_wIDth="wrap_content"        androID:layout_height="fill_parent"        androID:layout_alignParentBottom="true"        androID:layout_alignParenttop="true"        androID:layout_marginRight="6dip"        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/icon1"        androID:ellipsize="marquee"        androID:singleline="true"        androID:text="Some more information" />    <TextVIEw        androID:ID="@+ID/firstline"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_above="@ID/secondline"        androID:layout_alignParenttop="true"        androID:layout_alignWithParentIfMissing="true"        androID:layout_toRightOf="@ID/icon1"        androID:gravity="center_vertical"        androID:text="Some information" />    <ImageVIEw        androID:ID="@+ID/icon2"        androID:layout_wIDth="wrap_content"        androID:layout_height="fill_parent"        androID:layout_alignParentRight="true"        androID:layout_alignParenttop="true"        androID:layout_marginRight="6dip"        androID:src="@drawable/ic_launcher" /></relativeLayout>

这是我的代码,如您所见,我正在尝试使用视图持有人,因为我听说这是最有效的方法:

public class FirstLoginActivity extends ListActivity {        Context mContext;        List mList;        String[] testcontacts;        MessageVIEw aa = null;        @OverrIDe        public voID onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            testcontacts = getResources()                    .getStringArray(R.array.testcontacts_array);            aa = new MessageVIEw();            // setContentVIEw(R.layout.firstList);            ListVIEw lv = getListVIEw();            lv.setAdapter(aa);            lv.setTextFilterEnabled(true);            lv.setonItemClickListener(new OnItemClickListener() {                public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw,                        int position, long ID) {                    // When clicked, show a toast with the TextVIEw text                    Toast.makeText(getApplicationContext(),                            ((TextVIEw) vIEw).getText(), Toast.LENGTH_SHORT).show();                }            });        }        class MessageVIEw extends ArrayAdapter<String> {            MessageVIEw() {                super(FirstLoginActivity.this, androID.R.layout.activity_List_item,                        testcontacts);                // Todo auto-generated constructor stub            }            public VIEw getVIEw(int position, VIEw convertvIEw, VIEwGroup parent) {                Log.d("Ebz", "insIDe getVIEw method");                VIEwHolder holder;                VIEw v = convertvIEw;                if (v == null) {                    Log.d("Ebz", "if v == null");                    LayoutInflater inflater = getLayoutInflater();                    v = inflater.inflate(R.layout.List_items, null);                    holder = new VIEwHolder();                    holder.firstline = (TextVIEw) v.findVIEwByID(R.ID.firstline);                    holder.secondline = (TextVIEw) v.findVIEwByID(R.ID.secondline);                    holder.icon1 = (ImageVIEw) v.findVIEwByID(R.ID.icon1);                    holder.icon2 = (ImageVIEw) v.findVIEwByID(R.ID.icon2);                    v.setTag(holder);                } else {                    holder = (VIEwHolder) v.getTag();                }                holder.firstline.setText(testcontacts[position]);                holder.secondline.setText(testcontacts[position]);                holder.icon1.setimageBitmap(null);                holder.icon2.setimageBitmap(null);                // call the images directly?                return v;            }            class VIEwHolder {                TextVIEw firstline;                TextVIEw secondline;                ImageVIEw icon1;                ImageVIEw icon2;            }        }    }

这是我要在调用活动时显示的xml,列表项目的格式与我之前创建的格式相同.

 <?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:orIEntation="vertical" >    <relativeLayout        androID:ID="@+ID/top_control_bar"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content" >        <TextVIEw            androID:ID="@+ID/textVIEw1"            androID:layout_wIDth="fill_parent"            androID:layout_height="wrap_content"            androID:layout_marginBottom="10dp"            androID:layout_weight="1"            androID:background="@drawable/Titlebar"            androID:gravity="center"            androID:paddingBottom="10dp"            androID:paddingleft="10dp"            androID:paddingtop="10dp"            androID:text="Messages"            androID:textAppearance="?androID:attr/textAppearanceLarge"            androID:textcolor="#fff" />    </relativeLayout>    <linearLayout        androID:ID="@+ID/bottom_control_bar"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_alignParentBottom="true" >    </linearLayout>    <ListVIEw        androID:ID="@androID:ID/List"        androID:layout_wIDth="fill_parent"        androID:layout_height="0dip"        androID:layout_above="@ID/bottom_control_bar"        androID:layout_below="@ID/top_control_bar"        androID:choiceMode="multipleChoice"        androID:divIDer="#FFFFFF"        androID:divIDerHeight="14.5dp" >    </ListVIEw></relativeLayout>

解决方法:

好.没有人给您正确的答案.好吧,我已经解决了您的问题.
您没有得到该布局,因为在getVIEw方法中您传递了String [],但正在扩展ArrayAdapter.

您必须为ArrayAdapter传递ArrayList.

参见下面的代码:

package com.exmple.helperStackOverflow;import java.util.ArrayList;import java.util.List;import androID.app.ListActivity;import androID.app.LocalActivityManager;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.Window;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.ArrayAdapter;import androID.Widget.ImageVIEw;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;public class FirstLoginActivity extends ListActivity {             private ArrayList<String> m_orders = null;    private MessageVIEw aa;    private String[] testcontacts = new String[] {"A","B","C","D","E","F","G","H"};    protected static LocalActivityManager mLocalActivityManager;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.firstList);        m_orders = new ArrayList<String>();        this.aa = new MessageVIEw(this, R.layout.List_items, m_orders);        setlistadapter(this.aa);        getorders();    }    private Runnable returnRes = new Runnable() {        @OverrIDe        public voID run() {            if(m_orders != null && m_orders.size() > 0){                aa.notifyDataSetChanged();                for(int i=0;i<m_orders.size();i++)                aa.add(m_orders.get(i));            }            aa.notifyDataSetChanged();        }    };    private voID getorders(){          try{              m_orders = new ArrayList<String>();              for (int i = 0; i < testcontacts.length; i++){                m_orders.add(testcontacts[i]);              }              Thread.sleep(100);              Log.i("ARRAY", ""+ m_orders.size());            } catch (Exception e) {               Log.e("BACKGROUND_PROC", e.getMessage());            }            runOnUiThread(returnRes);        }    private class MessageVIEw extends ArrayAdapter<String> {        private ArrayList<String> items;        public MessageVIEw(Context context, int textVIEwResourceID, ArrayList<String> items) {                super(context, textVIEwResourceID, items);                this.items = items;        }        @OverrIDe        public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {                VIEw v = convertVIEw;                if (v == null) {                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);                    v = vi.inflate(R.layout.List_items, null);                }                String o = items.get(position);                if (o != null) {                        TextVIEw tt = (TextVIEw) v.findVIEwByID(R.ID.secondline);                        //ImageVIEw im = (ImageVIEw)v.findVIEwByID(R.ID.icon);                        if (tt != null) {                              tt.setText(testcontacts[position]);                          }//                                      }                return v;        }    } }

如果您仍然听不到,请告诉我,因为我已经完全解决了您的问题.

更新资料

如果要为此ListVIEw实现VIEwHolder,请参见以下内容:

首先创建VIEwHolder类:

public static class VIEwHolder{    public TextVIEw tt;    public TextVIEw bt;    public ImageVIEw leftimage;    public ImageVIEw rightimage;}

现在,只需将您的getVIEw方法替换为以下方法:

@OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {            VIEw v = convertVIEw;            VIEwHolder holder = null;            if (v == null) {                holder=new VIEwHolder();                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);                v = vi.inflate(R.layout.List_items, null);                holder.tt= (TextVIEw) v.findVIEwByID(R.ID.firstline);                holder.bt= (TextVIEw) v.findVIEwByID(R.ID.secondline);                holder.leftimage = (ImageVIEw) v.findVIEwByID(R.ID.icon1); // For ImageVIEw                holder.rightimage = (ImageVIEw) v.findVIEwByID(R.ID.icon2); // For ImageVIEw                v.setTag(holder);            }            else                holder=(VIEwHolder)v.getTag();            String o = items.get(position);            holder.tt.setText(testcontacts[position]);            holder.bt.setText("First line");            if (o != null) {                                    }      return v;    }

请享用. 总结

以上是内存溢出为你收集整理的android-自定义Listview不应该显示全部内容,希望文章能够帮你解决android-自定义Listview不应该显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1085062.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存