其他分享
首页 > 其他分享> > android-自定义Listview不应该显示

android-自定义Listview不应该显示

作者:互联网

我已经为列表项和自定义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;
    }

请享用.

标签:arraylist,listview,android-arrayadapter,listadapter,android
来源: https://codeday.me/bug/20191101/1986843.html