简体   繁体   中英

Unable to parse all values of XML to Custom ListView used in fragment, android?

I am parsing an XML file in the Listview. But the listView only gets 5-6 elements from the XML and then repeats itself. The Xml contains 20 elements.

This is my Fragment code

   public class ListFragemnt extends Fragment {

// All static variables
static final String URL = "URL";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_CATEGORY = "category";
private XMLParser parser = new XMLParser();
private Vector<String> title = new Vector<String>();
   private Vector<String> category = new Vector<String>();
private ListView lv;
CustomAdapter cAdapter ;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setRetainInstance(true);
    View view = inflater.inflate(R.layout.list_fragment,container, false);

    lv = (ListView) view.findViewById(R.id.listView1);
    new ShowDialogAsyncTask().execute();

    return view;
   }
} 

This is the asyncTask Code I am using for parsing XML

  private class ShowDialogAsyncTask extends AsyncTask<String[], Void, Void> {

    ProgressDialog dialog = new ProgressDialog(getActivity());

    @Override
    protected void onPreExecute() {
        // update the UI immediately after the task is executed
        super.onPreExecute();
        dialog.setMessage("Please wait...");
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected Void doInBackground(String[]... params) {
        String xmlResponse = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xmlResponse); // getting DOM
                                                            // element
        NodeList nl = doc.getElementsByTagName(KEY_ITEM);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            Element e = (Element) nl.item(i);
            title.add(parser.getValue(e, KEY_TITLE));
            category.add(parser.getValue(e, KEY_CATEGORY));
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        cAdapter = new CustomAdapter(getActivity(), title, category);
        lv.setAdapter(cAdapter);
        dialog.dismiss();
    }
}

Here is the BaseAdapter code

   private class CustomAdapter extends BaseAdapter {

    Context adapterContext;
    LayoutInflater inflater=null;
    Vector<String> title1;
    Vector<String> category1;


    public CustomAdapter(Context context, Vector<String> title2,
            Vector<String> category2) {
        this.adapterContext = context;
         inflater = (LayoutInflater)adapterContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.e("title2  ", ""+title2)
        title1 = title2;
        category1 = category2;


    }

    @Override
    public int getCount() {
        return title1.size();
    }

    @Override
    public Object getItem(int position) {
        return title1.get(position);
    }

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

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

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_element, null);


            TextView t1 = (TextView) convertView
                    .findViewById(R.id.categoryTextView);
            TextView t2 = (TextView) convertView
                    .findViewById(R.id.titleTextView);

            t1.setText(category1.get(position).toString());

            t2.setText(title1.get(position).toString());

        }

        return (convertView);
    }

}

What could be the issue I am unable to resolve. However in the constructor of the BaseAdapter I am getting all the 20 values but no in the listview.

Change getView and use a ViewHolder

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Also check

How ListView's recycling mechanism works

static class ViewHolder
{
   TextView t1,t2;
}

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

        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_element, null);
            holder = new ViewHolder();
            holder.t1 = (TextView) convertView.findViewById(R.id.categoryTextView);
            holder.t2 = (TextView) convertView.findViewById(R.id.titleTextView); 
            convertView.setTag(holder); 
            } else 
            { 
            holder = (ViewHolder) convertView.getTag(); 
            }  
            holder.t1.setText(category1.get(position).toString());
            holder.t2.setText(title1.get(position).toString());

        return convertView;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM