0

我的 MainActivity 中有一个列表视图作为侧边栏。我想在 onCreate() 中将列表视图项颜色设置为自定义颜色。所以我正在使用 BaseAdapter 并覆盖它的视图。我正在将 listview 设置为 ArrayAdapter。但是我的应用程序在启动时崩溃了。任何人都可以帮我解决这个问题。

这是我的MainActivity代码

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         ListView listview=(ListView)findViewById(R.id.listView);

            ArrayList<String > list = new ArrayList<String>();
            list.add("sohail");
            list.add("aziz");
            list.add("hassan");
            CustomAdapter adapter=new CustomAdapter(this,list);
            listview.setAdapter(adapter);


            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {


                    // Show Alert




                }

            });      



         }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}

这是我要覆盖我的视图的基本适配器。

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

/**
 * Created by polak_000 on 10/2/13.
 */
public class CustomAdapter extends BaseAdapter{

    Context mycontext;
    ArrayList<String> contactsList;
    LayoutInflater minflater;

    public CustomAdapter(Context context, ArrayList<String> list)
    {
        this.mycontext=context;
        contactsList=list;
        minflater =LayoutInflater.from(context);

    }
@Override
    public int getCount()
{
    return contactsList.size();
}


    @Override
    public Object getItem(int position)
    {
        return contactsList.get(position);
    }


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


@Override
    public View getView(int position,View convertView,ViewGroup parent)
{
     if (convertView !=null)
    {
        if (position ==1)
        {
             convertView.setBackgroundColor(Color.BLUE);

        }
    }
    return  convertView;
}

}

这是我的 MainActivity 布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <ListView
         android:id="@+id/listView"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:background="@android:color/holo_green_light"
         android:minHeight="50dp" 
        />

</RelativeLayout>

这是我的目录

10-02 14:11:22.125: D/AndroidRuntime(2036): 关闭 VM 10-02 14:11:22.125: W/dalvikvm(2036): threadid=1: 线程退出未捕获异常 (group=0x41465700) 10 -02 14:11:22.215:D/dalvikvm(2036):GC_FOR_ALLOC 释放 80K,5% 释放 2940K/3072K,暂停 34ms,总共 36ms 10-02 14:11:22.225:E/AndroidRuntime(2036):致命例外:主要 10-02 14:11:22.225: E/AndroidRuntime(2036): java.lang.NullPointerException 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.AbsListView.obtainView(AbsListView.java :2179) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.ListView.measureHeightOfChildren(ListView.java:1247) 10-02 14:11:22.225: E/AndroidRuntime(2036):在 android.widget.ListView.onMeasure(ListView.java:1159) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view。View.measure(View.java:15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:728) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View.measure(View.java :15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 10-02 14:11:22.225: E/AndroidRuntime(2036):在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View.measure(View.java:15848) 10-02 14 :11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 10-02 14:11:22。225: E/AndroidRuntime(2036): 在 com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View .measure(View.java:15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 10-02 14:11:22.225: E /AndroidRuntime(2036): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 com.android.internal.policy.impl.PhoneWindow$ DecorView.onMeasure(PhoneWindow.java:2189) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.View.measure(View.java:15848) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905) 10-02 14:11:22.225:E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java :1284) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 10-02 14:11:22.225: E/AndroidRuntime(2036):在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.view.Choreographer.doCallbacks(Choreographer.java:562) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android. view.Choreographer.doFrame(Choreographer.java:532) 10-02 14:11:22.225:E/AndroidRuntime(2036): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.os.Handler.handleCallback(Handler .java:730) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.os.Handler.dispatchMessage(Handler.java:92) 10-02 14:11:22.225: E/AndroidRuntime(2036) ): 在 android.os.Looper.loop(Looper.java:137) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 android.app.ActivityThread.main(ActivityThread.java:5103) 10- 02 14:11:22.225: E/AndroidRuntime(2036): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 java.lang.reflect .Method.invoke(Method.java:525) 10-02 14:11:22.225: E/AndroidRuntime(2036): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:737)10-02 14:11:22.225:E/AndroidRuntime(2036):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-02 14:11: 22.225:E/AndroidRuntime(2036):在 dalvik.system.NativeStart.main(本机方法)

4

4 回答 4

0

如果你想为你的列表视图创建自定义视图,那么你必须在你的getView()方法中设置自定义布局,LayoutInflater并在 getView() 方法中返回该视图,那么只有你才能在该视图上设置颜色。否则 getView 将始终返回 null in convertViewgetView()在您的方法中尝试如下。

     @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {
        ViewHolder holder;
         View m_view=convertView ;
        if (convertView == null)
        {
            holder = new ViewHolder();
            m_view= LayoutInflater.from(mycontext).inflate(R.layout.list_item,null,false);
            holder.item= (LinearLayout) m_view.findViewById(R.id.lnrListItem);
            holder.textView = (TextView) m_view.findViewById(R.id.textview);
            m_view.setTag(holder);
        }else{
            holder = (ViewHolder)m_view.getTag();
        }
        if (position==1) 
        {
            holder.item.setBackgroundColor(Color.CYAN); 
        }else{ 
            holder.item.setBackgroundColor(Color.BLUE); 
        }
        holder.textView.setText(contactsList.get(position));
        return  m_view;
    }
    class ViewHolder{
        LinearLayout item;
        TextView textView;
    }
于 2013-10-03T05:50:16.823 回答
0
// try this 

1. list item layout **list_item.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:id="@+id/lnrListItem"
              android:layout_width="match_parent"
              android:padding="5dp"
              android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


</LinearLayout>

2. list adapter
 public class CustomAdapter extends BaseAdapter {

        Context mycontext;
        ArrayList<String> contactsList;
        LayoutInflater minflater;

        public CustomAdapter(Context context, ArrayList<String> list)
        {
            this.mycontext=context;
            contactsList=list;
            minflater =LayoutInflater.from(context);

        }
        @Override
        public int getCount()
        {
            return contactsList.size();
        }


        @Override
        public Object getItem(int position)
        {
            return contactsList.get(position);
        }


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


        @Override
        public View getView(int position,View convertView,ViewGroup parent)
        {
            ViewHolder holder;
            if (convertView == null)
            {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mycontext).inflate(R.layout.list_item,null,false);
                holder.lnrListItem = (LinearLayout) convertView.findViewById(R.id.lnrListItem);
                holder.textView = (TextView) convertView.findViewById(R.id.textview);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
            if ((position%2)==0) 
            {
                holder.lnrListItem.setBackgroundColor(Color.CYAN); // odd item background are cyan

            }else{ 
                holder.lnrListItem.setBackgroundColor(Color.BLUE); // even item background are blue
            }
            holder.textView.setText(contactsList.get(position));

            return  convertView;
        }

        class ViewHolder{
            LinearLayout lnrListItem;
            TextView textView;
        }

    }
于 2013-10-03T04:25:36.270 回答
0

您正在从适配器中的 getView 函数返回 null。您需要确保根据需要实现 getView 功能。这是有关如何操作的指南:http ://www.androidhive.info/2011/10/android-listview-tutorial/

于 2013-10-02T19:06:40.147 回答
0

更清楚的是,您需要在 getView() 中创建视图。

就像是:

        public View getView(int position, View convertView, ViewGroup parent) {
        try {
            View v = convertView;
            if (v == null) {
                v = layoutInflater.inflate(R.layout.yadda_yadda, null);
            }
        ...
        }
于 2013-10-02T20:11:35.743 回答