1

Android not parsing JSON data into ListView, I am using this tutorial and just made few changes in ListViewAdapter.java

Like in my new implementation i used ViewHolder, and my code looks like this:

 public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();
    ViewHolder holder;

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

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

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

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

    static class ViewHolder {
        public ViewHolder(View convertView) {
            // TODO Auto-generated constructor stub
        }                
        TextView rank;
        TextView country;
        TextView population;
        ImageView flag;
    }  


    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
         // Avoid unneccessary calls to findViewById() on each row, which is expensive!
        holder = null;

        /*
         * If convertView is not null, we can reuse it directly, no inflation required!
         * We only inflate a new View when the convertView is null.
         */

        if (convertView == null) {
            convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);

            // Create a ViewHolder and store references to the two children views
            holder = new ViewHolder(convertView);
            holder.rank = (TextView) convertView.findViewById(R.id.rank);
            holder.country = (TextView) convertView.findViewById(R.id.country);
            holder.population = (TextView) convertView.findViewById(R.id.population);
            // Locate the ImageView in listview_item.xml
            holder.flag = (ImageView) convertView.findViewById(R.id.flag);

            // The tag can be any Object, this just happens to be the ViewHolder
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Capture position and set results to the TextViews
        holder.rank.setText(resultp.get(MainActivity.RANK));
        holder.country.setText(resultp.get(MainActivity.COUNTRY));
        holder.population.setText(resultp.get(MainActivity.POPULATION));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
        // Capture ListView item click

        return convertView;
    }

}

Edited: Click on ListItem code

@Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();

            listview.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // code to handle click
                 }
            });
        }

But i don't no why i am not getting data into ListView !

1
  • You would want to return something in the getItem() method Commented May 27, 2014 at 11:22

2 Answers 2

2

The issue is that you are not assigning the HashMap to `resultp that has the information you want to display

public View getView(final int position, View convertView, ViewGroup parent) {
    holder = null;

    if (convertView == null) {
        convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.listview_item, null);

        holder = new ViewHolder(convertView);
        holder.rank = (TextView) convertView.findViewById(R.id.rank);
        holder.country = (TextView) convertView.findViewById(R.id.country);
        holder.population = (TextView) convertView.findViewById(R.id.population);
        holder.flag = (ImageView) convertView.findViewById(R.id.flag);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Here's the change

    resultp    =    data.get(position);      

    // Here's the change

    holder.rank.setText(resultp.get(MainActivity.RANK));
    holder.country.setText(resultp.get(MainActivity.COUNTRY));
    holder.population.setText(resultp.get(MainActivity.POPULATION));
    imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), holder.flag);
    return convertView;
}

To attach OnItemClickListener to your ListView, in the Activity that contains the ListView, add the following:

public class MyActivity implements OnItemClickListener{

    ListView  lv;

    @Override
    public void onCreate(Bundle savedInstanceState() {
        ....
        ....
        // lv initialized here
        // adapter of lv set here
        attachListeners();
    }

    private void attachListeners() {
        ....
        ....
        // attach listeners to other views if you like
        lv.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // code to handle click
    }
}

Or, if you don't want your Activity to implement OnItemClickListener, then,

public class MyActivity {

    ListView  lv;

    @Override
    public void onCreate(Bundle savedInstanceState() {
        ....
        ....
        // lv initialized here
        // adapter of lv set here
        attachListeners();
    }

    private void attachListeners() {
        ....
        ....
        // attach listeners to other views if you like
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // code to handle click
            }
        });
    }        
}
Sign up to request clarification or add additional context in comments.

5 Comments

yes.... i forgot to use : resultp = data.get(position); and this worked for me :) thanks i will accept your answer in next 3 mins but ticked as useful
may i know now where & how can i write listViewOnItemClickListener in my code?
thanks, please see my edited code can i write onlistItemClick in onPostExecute(..) ?'
I am not sure. Can you try and tell me
Ok great. Now I know :D
0

First of all try to fix this:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.