Search code examples
androidbaseadaptercommonsware-cwac

Working with MergeAdapter and BaseAdapter


I already have used MergeAdapter and works really well with ArrayAdapter, but when I used with LazyAdapter extends BaseAdapter, it doesn't show any error on Logcat and the emulator don't show nothing of the LazyAdapter content.

Question:

So is possible to work with BaseAdapter and MergeAdapter? If not, its my code, what I should do?

ListFragment:

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
    }

LazyAdapter:

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();

        }


    }

Solution

  • Just to put an answer in this question, I have made a mistake, the xml file in my server was misspelled, So Yes MergeAdapter works with BaseAdapter.

    So this could be a sample code for who wants to use.

    Thanks Commonsware for your help.