简体   繁体   中英

Android JSON Data not parsing into ListView

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 !

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

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;
}

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