手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >listview只能显示3个JSON解析出来的item数据
listview只能显示3个JSON解析出来的item数据
摘要:调试看了发现Arraylis的size只有3。是不是因为这个原因?我的listview上一次只能显示3条item。而且从其他页面回到到该界面...

调试看了发现Arraylis的size只有3。是不是因为这个原因?我的listview上一次只能显示3条item。而且从其他页面回到到该界面才会刷新listview,但刷新出来的也是重复的3条数据。。。。

package com.chason.igo.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.chason.igo.R;
import com.chason.igo.utils.Constans;
import com.chason.igo.utils.NewsItemInfo;
import com.example.lib_lyn.HttpUtils;
import com.example.lib_lyn.volley.VolleyLisener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by chason on 2017/6/12.
 */

public class MainFragment extends Fragment {
    private View mLayout;
    public ArrayList< String > pic;
    public ArrayList< NewsItemInfo > datalist = new ArrayList< NewsItemInfo >();
    private ListView lv_main;
    private MyListView adapter;
    private NewsItemInfo newsItemInfo;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private TextView tv_title;
    private TextView tv_summary;
    private TextView tv_price;
    private ImageView iv_img;

    public MainFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, null);
        lv_main = (ListView) view.findViewById(R.id.lv_main);
        initoption();
        adapter = new MyListView();
        lv_main.setAdapter(adapter);
        new MyAsyn().execute(Constans.URL.FAVORITE_URL);
        return view;
    }

    class MyAsyn extends AsyncTask< String, Void, Void > implements VolleyLisener {

        @Override
        protected Void doInBackground(String... params) {
            HttpUtils.getVolley(params[0], this);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            adapter.notifyDataSetChanged();
            super.onPostExecute(result);
        }

        @Override
        public void onResponse(String response) {
            parseJson(response);
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), "数据解析失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void parseJson(String response) {
        try {
             JSONObject json = new JSONObject(response);
            JSONArray jsonArray = json.getJSONArray("goodlist");
            for (int i = 0; i <  jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String title = jsonObject.getString("product");
                String str = jsonObject.getString("short_title");
                int price = jsonObject.getInt("price");
               
                JSONArray picArray = jsonObject.getJSONArray("images");
                pic = new ArrayList< String >();
                for (int j = 0; j <  picArray.length(); j++) {
                    JSONObject jsonpic = picArray.getJSONObject(i);
                    String string = jsonpic.getString("image");
                    pic.add(string);
                }

                newsItemInfo = new NewsItemInfo(title, str, price, pic);
                datalist.add(newsItemInfo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /*图片初始化*/
    private void initoption() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageForEmptyUri(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();

    }

    class MyListView extends BaseAdapter {

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

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            newsItemInfo = datalist.get(position);
            View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, null);
            tv_title = (TextView) inflate.findViewById(R.id.tv_title1);
            tv_summary = (TextView) inflate.findViewById(R.id.tv_summary);
            tv_price = (TextView) inflate.findViewById(R.id.tv_price);
            iv_img = (ImageView) inflate.findViewById(R.id.iv_img);
            imageLoader.displayImage(newsItemInfo.pic.get(0), iv_img, options);

            tv_title.setText(""+newsItemInfo.title);
            tv_summary.setText(""+newsItemInfo.str);
            tv_price.setText(""+newsItemInfo.price);
            adapter.notifyDataSetChanged();


            return inflate;
        }
    }
}



listview只能显示3个JSON解析出来的item数据0

附上demo地址http://download.csdn.net/detail/aa931667248/9872696

回复讨论(解决方案)

你json数据的多少是看

for (int i = 0; i <  jsonArray.length(); i++) {

这行代码中的jsonarray的长度

你的设置为读取goodlist标签的数据集合,那你就要去看看你的要表现的数据是否都是在goodlist下的了 你json数据的多少是看

for (int i = 0; i <  jsonArray.length(); i++) {

这行代码中的jsonarray的长度

你的设置为读取goodlist标签的数据集合,那你就要去看看你的要表现的数据是否都是在goodlist下的了

listview只能显示3个JSON解析出来的item数据1

是在goodlist里的。我尝试改变size大小,然后就报错显示数组越界,大小只有3 数组越界的话那就是你的Mylistview 或者adapter有哪里出了问题,只能给到你这个方向了 数组越界的话那就是你的Mylistview 或者adapter有哪里出了问题,只能给到你这个方向了

谢谢,已经发现问题了。是JSON图片数组里的for语句写错了。把i改回j就可以了。

JSONObject jsonPic = picArray.getJSONObject(j);

【listview只能显示3个JSON解析出来的item数据】相关文章:

ListView异步加载图片实现思路

Android 自定义标题栏 显示网页加载进度的方法实例

listview Button始终放在底部示例

android实现listview分页的方法

Android实现原生侧滑菜单的超简单方式

android调试工具DDMS的使用详解

android判断软件是否第一次运行的方法

解决Android arChartengine 导入工程出错办法

Android判断SDK版本及判断是否联网

Android手机在开发调试时logcat不显示输出信息的办法

精品推荐
分类导航