Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:03:57+00:00 2026-06-07T00:03:57+00:00

I have this application where I implement the ActionBar Fragment interface. Underlying the interface,

  • 0

I have this application where I implement the ActionBar Fragment interface. Underlying the interface, there is one ListFragment where the entries in the list keep duplicated every time I switch to a different tab. Can somebody show me the correct way to implement the listfragment with custom adapter?

The following is my code:
FeaturedFragment.java

public class FeaturedFragment extends ListFragment {

static final String URL = "http://www.sundancepost.com/ivue/Featured.xml";

static final String KEY_PROJECT = "project"; // parent node
static final String KEY_BANNER = "banner";

ListView featuredList;
FeaturedListAdapter featuredAdapter;
ArrayList<HashMap<String, String>> projectsList = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.featured_list, container, false);
        featuredList = (ListView)view.findViewById(android.R.id.list);

        return view;
 }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

        ConnectivityManager cMgr = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
         if (cMgr.getActiveNetworkInfo() != null && cMgr.getActiveNetworkInfo().isConnectedOrConnecting()) {

            new MyAsyncTask().execute();

          } else {
             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
             builder.setMessage("Please check your internet connection");
             builder.setTitle("Failed to download resources");
             builder.setCancelable(false);
             builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           return;
                       }
                   });
                AlertDialog alert = builder.create();
                alert.show();
        }

    featuredAdapter = new FeaturedListAdapter(getActivity(), projectsList);

}

public class MyAsyncTask extends AsyncTask<Void,Void,Void>{

    private final ProgressDialog recents_dialog = new ProgressDialog(getActivity());

    @Override
    protected Void doInBackground(Void... params) {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL);
        Document doc = parser.getDomElement(xml);

        NodeList nl = doc.getElementsByTagName(KEY_PROJECT);

        for (int i = 0; i < nl.getLength(); i++) {

            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            map.put(KEY_BANNER, parser.getValue(e, KEY_BANNER));

            projectsList.add(map);
        }
        return null;
    }

    @Override
    protected void onPreExecute()
    {
        recents_dialog.setMessage("Loading ...");
        recents_dialog.show();
        recents_dialog.setCancelable(false);
    }

    @Override
    protected void onPostExecute(Void result)
    {
        if(recents_dialog.isShowing() == true)
        {
            recents_dialog.dismiss();
        }
         // Getting adapter by passing xml data ArrayList

        featuredList.setAdapter(featuredAdapter);

    }
  }

}

FeaturedListAdapter:

public class FeaturedListAdapter extends BaseAdapter {

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

public FeaturedListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data= (ArrayList<HashMap<String, String>>) 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) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;

        if(vi==null){
            vi=new View(activity);
            vi = inflater.inflate(R.layout.featured_listrow, null);
        }

        ImageView banner =(ImageView)vi.findViewById(R.id.banner); // thumb image
        banner.setAdjustViewBounds(true);
        imageLoader.DisplayImage(data.get(position).get(FeaturedFragment.KEY_BANNER), banner);

        return vi;
  }
}

[Edit]
The following code is modified based on @10s’s suggestion. But, the result is still the same..

public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.featured_listrow, null);
            holder = new ViewHolder();
            holder.banner = (ImageView)convertView.findViewById(R.id.banner);
            holder.banner.setAdjustViewBounds(true);
            imageLoader.DisplayImage(data.get(position).get(FeaturedFragment.KEY_BANNER), holder.banner);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        return convertView;
}

public static class ViewHolder {
    public ImageView banner;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:03:59+00:00Added an answer on June 7, 2026 at 12:03 am

    The code that you are using in getView() of the Custom adapter is recreating the view every time it is called. And the adapter does re-render itself every time your activity, in your case fragment takes focus.

    Try using a holder for your adapter. It is the android best practice and a very easy and useful one. A link to get you started: http://android.amberfog.com/?p=296

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one desktop application which was implemented in C#. This app uploads file
I am new to implement the ListView with section. I have use this application
I want to implement chat feature in my android application,in this feature one user
I'm trying to implement this application. At the moment I have designed tabs on
I need to implement jquery blockUI for my application.. I have this code.. $.blockUI({
I have this task: realize Grails application with ExtJS. And one part of this
I have an application that saves its context to XML. In this application, there
I have an application in C#, i want to implement perfcounters in this application
I want to implement Campaign Tracking in my application using EasyTracker I have this
I have this application written in JSF 2.0 facelets and it has the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.