自定义Listview没有显示它应该

时间:2011-12-31 21:35:14

标签: android listview arraylist android-arrayadapter listadapter

我已经为列表项和自定义listadapter创建了格式。问题是我的listview没有像我设计的那样显示。

这是我的每个列表项的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>

3 个答案:

答案 0 :(得分:2)

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

在这段代码中,您提供了导致问题的android:layout_height="0dip"。要么wrap_contentfill_parent,要么给它一些固定的值,例如200dip

请注意,最后一个选项会将列表高度设置为200dip,因此如果您需要精确的尺寸,以使其适合top_control_barbottom_control_bar

尝试从java文件生成你的ui:

 ListView list = new ListView(this);
 list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,getScreenHeight()-(top_control_bar_height + bottom_control_bar_height));
 this.addView(list);

答案 1 :(得分:1)

首先,这在图像视图中不起作用:

android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"

从列表视图中删除android:layout_height="0dip"并将其更改为fill_parent。两者都无法合作。使用android:layout_alignparent="center_vertical"。要获得更好的答案,您应该粘贴所需设计的图像。

答案 2 :(得分:1)

确定。没有人给你一个正确的答案。好吧,我已经解决了你的问题。 你没有得到那个布局,因为在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;
    }

享受。 :)