0

我正在尝试用 LinkedList 填充 ListView。我认为我的实施有问题..但我不知道是什么。

这是我设置适配器的方法:

public void refreshListView() {
    //pManager.simpleSort();
    // pManager.getProfiles()

    LinkedList<Profile> temp = new LinkedList<Profile>();
    temp.add(new Profile("hello", "link", true)); // im kind of testing here
    adapter = new CustomAdapter(getApplicationContext(), temp);
    list.setAdapter(adapter);
}

这是我使用几个教程编写的 CustomAdapter。

public class CustomAdapter extends BaseAdapter {

    private LinkedList<Profile> profiles;
    private Context context;

    public CustomAdapter(Context context, LinkedList<Profile> input) {
        this.context = context;
        profiles = input;
    }

    @Override
    public int getCount() {
        if (profiles == null) {
            return 0;
        } else {
            return profiles.size();
        }
    }

    @Override
    public Profile getItem(int arg0) {
        return profiles.get(arg0);
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        View out = v;
        if (out == null){
            out = new ProfileItemView(context, getItem(position));
        }
        return out;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }
}

这是我写来显示我的个人资料的自定义视图:

public class ProfileItemView extends RelativeLayout {

    public ProfileItemView(Context context, Profile profile) {
        super(context, null);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.list_item, null, true);
        ImageView icon = (ImageView) findViewById(R.id.default_icon_view);
        if (profile.checkDefault()) {
            icon.setImageResource(R.drawable.ic_launcher);
        } else {
            icon.setImageResource(R.drawable.android_icon);
        }
        TextView name = (TextView) findViewById(R.id.name_view);
        name.setText(profile.getName());
    }
}

这里还有一个logcat:

Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x419c52a0)
FATAL EXCEPTION: main
java.lang.NullPointerException
   at com.exp.test.ProfileItemView.<init>(ProfileItemView.java:18)
   at com.exp.test.CustomAdapter.getView(CustomAdapter.java:37)
   at android.widget.AbsListView.obtainView(AbsListView.java:2402)
   at android.widget.ListView.makeAndAddView(ListView.java:1769)
   at android.widget.ListView.fillDown(ListView.java:672)
   at android.widget.ListView.fillFromTop(ListView.java:733)
   at android.widget.ListView.layoutChildren(ListView.java:1622)
   at android.widget.AbsListView.onLayout(AbsListView.java:2237)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
   at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
   at android.view.View.layout(View.java:13840)
   at android.view.ViewGroup.layout(ViewGroup.java:4372)
   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1948)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1758)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1042)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4329)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
   at android.view.Choreographer.doFrame(Choreographer.java:525)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5118)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
   at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

您需要使用膨胀的视图对象来初始化您的视图。findViewById在当前的膨胀布局中查找视图。

同时传递活动上下文

  adapter = new CustomAdapter(ActivityName.this, temp);   

使用视图支架模式

static class ViewHolder
{
ImageView iv;
TextView tv;
}

在构造函数中

LayoutInflater mInflater;
 public CustomAdapter(Context context, LinkedList<Profile> input) {
        mInflater = LayoutInflater.fromf(context;
        profiles = input;
    }

然后在getView

public View getView(int position, View convertView, ViewGroup parent) { 
 ViewHolder holder;      
 if (convertView == null) { 
 convertView = mInflater.inflate(R.layout.lis_item,parent, false);
 holder = new ViewHolder(); 
 holder.tv = (TextView) convertView.findViewById(R.id.name_view);  
 holder.iv = (ImageView)convertView.findViewById(R.id.default_icon_view);              
 convertView.setTag(holder); 
 } else { 
   holder = (ViewHolder) convertView.getTag(); 
 }
  Profile profile = profiles.get(position);
    if (profile.checkDefault()) {
        holder.iv.setImageResource(R.drawable.ic_launcher);
    } else {
        holder.iv.setImageResource(R.drawable.android_icon);
    }
         holder.tv.setText(profile.getName());
 return convertView; 
 }
于 2013-10-29T16:19:40.707 回答