【问题标题】:Android JSON Data not parsing into ListViewAndroid JSON数据未解析为ListView
【发布时间】:2014-05-27 11:19:24
【问题描述】:

Android 没有将 JSON 数据解析为 ListView,我正在使用这个 tutorial,只是在 ListViewAdapter.java

中做了一些更改

就像在我的新实现中我使用了 ViewHolder,我的代码如下所示:

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

}

已编辑:点击ListItem代码

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

但我不知道为什么我没有将数据输入 ListView !

【问题讨论】:

  • 你想在getItem()方法中返回something

标签: java android json android-listview


【解决方案1】:

问题是您没有将HashMap 分配给具有您要显示的信息的`resultp

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

要将OnItemClickListener 附加到您的ListView,请在包含ListView 的Activity 中添加以下内容:

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

或者,如果您不希望您的 Activity 实现 OnItemClickListener,那么,

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

【讨论】:

  • 是的....我忘了使用:resultp = data.get(position);这对我有用 :) 谢谢我会在接下来的 3 分钟内接受你的回答,但打勾是有用的
  • 没问题@Sophie :)
  • 我现在可以知道在哪里以及如何在我的代码中编写 listViewOnItemClickListener 吗?
  • 谢谢,请看我编辑的代码,我可以在 onPostExecute(..) 中写 onlistItemClick 吗?'
  • 我不确定。你能告诉我吗
【解决方案2】:

首先尝试解决这个问题:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多