Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用MergeAdapter和BaseAdapter_Android_Baseadapter_Commonsware Cwac - Fatal编程技术网

Android 使用MergeAdapter和BaseAdapter

Android 使用MergeAdapter和BaseAdapter,android,baseadapter,commonsware-cwac,Android,Baseadapter,Commonsware Cwac,我已经使用过MergeAdapter,并且在ArrayAdapter中运行得非常好,但是当我使用LazyAdapter extends BaseAdapter时,它在Logcat上没有显示任何错误,模拟器也没有显示任何LazyAdapter内容 问题: public class HomeFragment extends ListFragment{ private static final String TAG = "URL"; private MergeAdapter ad

我已经使用过MergeAdapter,并且在ArrayAdapter中运行得非常好,但是当我使用
LazyAdapter extends BaseAdapter
时,它在Logcat上没有显示任何错误,模拟器也没有显示任何LazyAdapter内容

问题:

public class HomeFragment extends ListFragment{



    private static final String TAG = "URL";
    private MergeAdapter adapter = null;
    private ImageView [] imageView;
    private ViewFlipper flippy;
    private ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
    private NoticiaAdapter nAdapter = null;
    // XML node keys
        static final String KEY_SONG = "news"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_TITLE = "title";
        static final String KEY_TEXT = "text";
        static final String KEY_HOUR = "hour";
        static final String KEY_THUMB_URL = "thumb_url";

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);

        setRetainInstance(true);

        String[] Url = create();
        new MyAsyncTask(Url).execute();
        nAdapter = new NoticiaAdapter(getActivity(), newsList);
        adapter = new MergeAdapter();
        adapter.addView(buildlabel1());
        adapter.addAdapter(nAdapter);

        setListAdapter(adapter);
        //mergeadapter
    }
public class NoticiaAdapter extends BaseAdapter {   

    private FragmentActivity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    ViewHolder holder = null;

    public final class ViewHolder {
        public TextView title;
        public TextView text;
        public TextView hour;
        public ImageView thumb_image;
        // public Button save_btn;

    }

     public NoticiaAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
            super();
            this.activity = fragmentActivity;
            this.data = d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader=new ImageLoader(activity.getApplicationContext());
        }

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


        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return data.get(position);
        }


     public long getItemId(int position) {       
      return position;   
     }   

     @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

     public View getView(int position, View convertView, ViewGroup parent) {  
         View vi=convertView;
            if(convertView==null) 

                vi = inflater.inflate(R.layout.news_list_row, null);
            holder = new ViewHolder();
            holder.title = (TextView)vi.findViewById(R.id.txtTitle); // title
            holder.text = (TextView)vi.findViewById(R.id.txtDescription); // artist name
            holder.hour = (TextView)vi.findViewById(R.id.txtHour); // duration
            holder.thumb_image=(ImageView)vi.findViewById(R.id.img_news); // thumb image

            HashMap<String, String> news = new HashMap<String, String>();
            news = data.get(position);

            holder.title.setText(news.get(HomeFragment.KEY_TITLE));
            holder.text.setText(news.get(HomeFragment.KEY_TEXT));
            holder.hour.setText(news.get(HomeFragment.KEY_HOUR));
            imageLoader.DisplayImage(news.get(HomeFragment.KEY_THUMB_URL), holder.thumb_image);

      return vi;   
     }

     public void add(HashMap<String, String> map) {
            // TODO Auto-generated method stub
            data.add(map);
            notifyDataSetChanged();

        }


    }
那么可以使用BaseAdapter和MergeAdapter吗? 如果不是,这是我的代码,我应该怎么做

列表片段:

public class HomeFragment extends ListFragment{



    private static final String TAG = "URL";
    private MergeAdapter adapter = null;
    private ImageView [] imageView;
    private ViewFlipper flippy;
    private ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
    private NoticiaAdapter nAdapter = null;
    // XML node keys
        static final String KEY_SONG = "news"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_TITLE = "title";
        static final String KEY_TEXT = "text";
        static final String KEY_HOUR = "hour";
        static final String KEY_THUMB_URL = "thumb_url";

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);

        setRetainInstance(true);

        String[] Url = create();
        new MyAsyncTask(Url).execute();
        nAdapter = new NoticiaAdapter(getActivity(), newsList);
        adapter = new MergeAdapter();
        adapter.addView(buildlabel1());
        adapter.addAdapter(nAdapter);

        setListAdapter(adapter);
        //mergeadapter
    }
public class NoticiaAdapter extends BaseAdapter {   

    private FragmentActivity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    ViewHolder holder = null;

    public final class ViewHolder {
        public TextView title;
        public TextView text;
        public TextView hour;
        public ImageView thumb_image;
        // public Button save_btn;

    }

     public NoticiaAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
            super();
            this.activity = fragmentActivity;
            this.data = d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader=new ImageLoader(activity.getApplicationContext());
        }

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


        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return data.get(position);
        }


     public long getItemId(int position) {       
      return position;   
     }   

     @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

     public View getView(int position, View convertView, ViewGroup parent) {  
         View vi=convertView;
            if(convertView==null) 

                vi = inflater.inflate(R.layout.news_list_row, null);
            holder = new ViewHolder();
            holder.title = (TextView)vi.findViewById(R.id.txtTitle); // title
            holder.text = (TextView)vi.findViewById(R.id.txtDescription); // artist name
            holder.hour = (TextView)vi.findViewById(R.id.txtHour); // duration
            holder.thumb_image=(ImageView)vi.findViewById(R.id.img_news); // thumb image

            HashMap<String, String> news = new HashMap<String, String>();
            news = data.get(position);

            holder.title.setText(news.get(HomeFragment.KEY_TITLE));
            holder.text.setText(news.get(HomeFragment.KEY_TEXT));
            holder.hour.setText(news.get(HomeFragment.KEY_HOUR));
            imageLoader.DisplayImage(news.get(HomeFragment.KEY_THUMB_URL), holder.thumb_image);

      return vi;   
     }

     public void add(HashMap<String, String> map) {
            // TODO Auto-generated method stub
            data.add(map);
            notifyDataSetChanged();

        }


    }
公共类HomeFragment扩展了ListFragment{
私有静态最终字符串TAG=“URL”;
私有MergeAdapter=null;
私有ImageView[]ImageView;
私家侦探Flipper flippy;
private ArrayList newsList=new ArrayList();
私有通知适配器nAdapter=null;
//XML节点密钥
静态最终字符串键\u SONG=“news”//父节点
静态最终字符串键\u ID=“ID”;
静态最终字符串键\u TITLE=“TITLE”;
静态最终字符串键\u TEXT=“TEXT”;
静态最终字符串键\u HOUR=“HOUR”;
静态最终字符串键\u THUMB\u URL=“THUMB\u URL”;
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
//TODO自动生成的方法存根
super.onViewCreated(视图,savedInstanceState);
setRetainInstance(真);
字符串[]Url=create();
新建MyAsyncTask(Url).execute();
nAdapter=newnotifiaadapter(getActivity(),newsList);
adapter=新的MergeAdapter();
addView(buildlabel1());
adapter.addAdapter(nAdapter);
setListAdapter(适配器);
//合并适配器
}
懒散适配器:

public class HomeFragment extends ListFragment{



    private static final String TAG = "URL";
    private MergeAdapter adapter = null;
    private ImageView [] imageView;
    private ViewFlipper flippy;
    private ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
    private NoticiaAdapter nAdapter = null;
    // XML node keys
        static final String KEY_SONG = "news"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_TITLE = "title";
        static final String KEY_TEXT = "text";
        static final String KEY_HOUR = "hour";
        static final String KEY_THUMB_URL = "thumb_url";

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);

        setRetainInstance(true);

        String[] Url = create();
        new MyAsyncTask(Url).execute();
        nAdapter = new NoticiaAdapter(getActivity(), newsList);
        adapter = new MergeAdapter();
        adapter.addView(buildlabel1());
        adapter.addAdapter(nAdapter);

        setListAdapter(adapter);
        //mergeadapter
    }
public class NoticiaAdapter extends BaseAdapter {   

    private FragmentActivity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    ViewHolder holder = null;

    public final class ViewHolder {
        public TextView title;
        public TextView text;
        public TextView hour;
        public ImageView thumb_image;
        // public Button save_btn;

    }

     public NoticiaAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
            super();
            this.activity = fragmentActivity;
            this.data = d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader=new ImageLoader(activity.getApplicationContext());
        }

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


        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return data.get(position);
        }


     public long getItemId(int position) {       
      return position;   
     }   

     @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

     public View getView(int position, View convertView, ViewGroup parent) {  
         View vi=convertView;
            if(convertView==null) 

                vi = inflater.inflate(R.layout.news_list_row, null);
            holder = new ViewHolder();
            holder.title = (TextView)vi.findViewById(R.id.txtTitle); // title
            holder.text = (TextView)vi.findViewById(R.id.txtDescription); // artist name
            holder.hour = (TextView)vi.findViewById(R.id.txtHour); // duration
            holder.thumb_image=(ImageView)vi.findViewById(R.id.img_news); // thumb image

            HashMap<String, String> news = new HashMap<String, String>();
            news = data.get(position);

            holder.title.setText(news.get(HomeFragment.KEY_TITLE));
            holder.text.setText(news.get(HomeFragment.KEY_TEXT));
            holder.hour.setText(news.get(HomeFragment.KEY_HOUR));
            imageLoader.DisplayImage(news.get(HomeFragment.KEY_THUMB_URL), holder.thumb_image);

      return vi;   
     }

     public void add(HashMap<String, String> map) {
            // TODO Auto-generated method stub
            data.add(map);
            notifyDataSetChanged();

        }


    }
公共类NotifiaAdapter扩展了BaseAdapter{
私人碎片活动;
私有数组列表数据;
专用静态充气机=空;
公共图像加载器;
ViewHolder=null;
公共最终类视图持有者{
公共文本视图标题;
公共文本查看文本;
公共文本浏览时间;
公共图像查看大拇指图像;
//公共按钮保存;
}
公共通知适配器(FragmentActivity FragmentActivity,ArrayList d){
超级();
this.activity=fragmentActivity;
这个数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
imageLoader=新的imageLoader(activity.getApplicationContext());
}
public int getCount(){
返回data.size();
}   
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回数据。获取(位置);
}
公共长getItemId(int位置){
返回位置;
}   
@凌驾
public void notifyDataSetChanged(){
super.notifyDataSetChanged();
}
公共视图getView(int位置、视图转换视图、视图组父级){
视图vi=转换视图;
if(convertView==null)
vi=充气机。充气(R.layout.news\u list\u行,空);
holder=新的ViewHolder();
holder.title=(TextView)vi.findViewById(R.id.txtTitle);//title
holder.text=(TextView)vi.findViewById(R.id.txtDescription);//艺术家姓名
holder.hour=(TextView)vi.findViewById(R.id.txtHour);//持续时间
holder.thumb_image=(ImageView)vi.findviewbyd(R.id.img_news);//thumb image
HashMap news=新的HashMap();
新闻=数据。获取(位置);
holder.title.setText(news.get(HomeFragment.KEY_title));
holder.text.setText(news.get(HomeFragment.KEY_text));
hour.setText(news.get(HomeFragment.KEY_hour));
imageLoader.DisplayImage(news.get(HomeFragment.KEY\u THUMB\u URL)、holder.THUMB\u image);
返回vi;
}
公共void添加(HashMap映射){
//TODO自动生成的方法存根
数据。添加(地图);
notifyDataSetChanged();
}
}

为了回答这个问题,我犯了一个错误,我的服务器中的xml文件拼写错误,所以是的,MergeAdapter与BaseAdapter一起工作

因此,这可能是希望使用的用户的示例代码


感谢Commonware的帮助。

暂时删除
合并适配器
,只需将
通知适配器
放入
列表片段
,看看它是否有效。如果无效,问题在于
通知适配器
。谢谢,@commonware我服务器中的xml文件拼写错误。有办法重新加载内容吗只在按下按钮后在“NotifiaAdapter”内?谢谢你解决这个问题请。。。