适配器和DialogView - 不确定如何构建适配器

时间:2013-02-28 06:53:12

标签: android android-adapter android-adapterview

所以我尝试使用API​​从文件夹中填充一个Dialog列表,一旦用户点击文件夹,我再次使用其子文件夹填充Dialog等。但我不太确定所有内容是如何组合在一起的。到目前为止,显示了对话框,现在我需要添加实际内容。

 @Override
 protected View onCreateDialogView() {

    LayoutInflater inflater = ((SettingsActivity) ctx).getLayoutInflater();
    View vw = inflater.inflate(R.layout.channel_content_view, null);
    ListView lv = (ListView) vw.findViewById(android.R.id.list);
    File[] files = ChannelHandler.getChannels();
    HiddenChannelsListAdapter adapter = new HiddenChannelsListAdapter(ctx, files);
    lv.setAdapter(adapter);
    return vw;
 }

我不确定Class HiddenChannelsListAdapter 必须如何看待。

这就是我所熟悉的:

package com.example.tvrplayer;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class HiddenChannelsListAdapter extends BaseAdapter {
    public HiddenChannelsListAdapter(Context ctx, File[] files) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        View view = null;
        Log.i("ADAPTER", "Hello");
        return view;
    }

}

当我尝试现在打开对话框时,它会得到一个NullPointerException,我认为是因为适配器没有做任何事情,只是返回null。

适配器返回什么?它在哪里归还?我现在很困惑

1 个答案:

答案 0 :(得分:2)

试试这个

public class ContactListCursorAdapter extends BaseAdapter {

/** Remember our context so we can use it when constructing views. */
private Context mContext;

/**
* Hold onto a copy of the entire Contact List.
*/
private List<ContactEntry> mItems = new ArrayList<ContactEntry>();

public ContactListCursorAdapter(Context context, ArrayList<ContactEntry> items) {
     mContext = context;
     mItems = items;
}

public int getCount() {
     return mItems .size();
}

public Object getItem(int position) {
     return mItems .get(position);
}

 /** Use the array index as a unique id. */
public long getItemId(int position) {
     return position;
}

/**
* @param convertView
*            The old view to overwrite, if one is passed
* @returns a ContactEntryView that holds wraps around an ContactEntry
*/
public View getView(int position, View convertView, ViewGroup parent) {
    ContactEntryView btv;
    if (convertView == null) {
         btv = new ContactEntryView(mContext, mShow.get(position));
    } else {
         btv = (ContactEntryView) convertView;
         String name = mShow.get(position).getName();
         btv.setNameText(name);
         String number = mShow.get(position).getNumber();
         if (number != null) {
              btv.setNumberText("Mobile: " + mShow.get(position).getNumber());
         }
    }
        return btv;
}
}