2 qq 24223073 qq_24223073 于 2016.09.25 18:03 提问

Android之ListView加载数据都一样问题

package com.example.kira.httpurldemo;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private ListView listView;
private String URl="http://www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new MyTask().execute(URl);
}

private void initView() {
    listView= (ListView) findViewById(R.id.lv_main);
}
class MyTask extends AsyncTask<String,Void,List<NewsBean>>
{

    @Override
    protected List<NewsBean> doInBackground(String... strings) {

        return getData(strings[0]);
    }

    @Override
    protected void onPostExecute(List<NewsBean> newsBeen) {
        super.onPostExecute(newsBeen);
        NewsAdapter newsAdapter=new NewsAdapter(MainActivity.this,newsBeen);
        listView.setAdapter(newsAdapter);
    }
}

private List<NewsBean> getData(String url) {
    List<NewsBean> list=new ArrayList<>();
    try {
        JSONObject jsonObject=null;
        NewsBean newsBean=new NewsBean();
        String result="";
        URL u=new URL(url);
        InputStream is=null;
        HttpURLConnection conn= (HttpURLConnection) u.openConnection();

        conn.setRequestMethod("GET");
        conn.setReadTimeout(5*1000);
        int code=conn.getResponseCode();

        if (code==200)
        {

            is=conn.getInputStream(); //获取输入流
            InputStreamReader isr=new InputStreamReader(is,"utf-8"); //将字节流转换为字符流
            BufferedReader bfr=new BufferedReader(isr);
            String line="";
            while((line=bfr.readLine())!=null)
            {
                result+=line;
            }
            Log.i("----",result);
            jsonObject=new JSONObject(result);
            JSONArray jsonArray=jsonObject.getJSONArray("data");
            for (int i=0;i<jsonArray.length();i++)
            {
                jsonObject=jsonArray.getJSONObject(i);
                newsBean.iconUrl=jsonObject.getString("picSmall");
                newsBean.iconTitle=jsonObject.getString("name");
                newsBean.iconContent=jsonObject.getString("description");
                list.add(newsBean);
            }

            return list;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }


    return  null;
}

}

NewsAdapter类:
package com.example.kira.httpurldemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class NewsAdapter extends BaseAdapter {
private LayoutInflater mLayoutInflater;
private List data;
NewsAdapter(Context context,List data)
{
mLayoutInflater=LayoutInflater.from(context);
this.data=data;
}
@Override
public int getCount() {
return data.size();
}

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

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    ViewHolder viewHolder;
    if (view==null)
    {

        viewHolder=new ViewHolder();
        view=mLayoutInflater.inflate(R.layout.item,null);
        viewHolder.img= (ImageView) view.findViewById(R.id.iv_icon);
        viewHolder.title=(TextView)view.findViewById(R.id.tv_title);
        viewHolder.content=(TextView)view.findViewById(R.id.tv_content);
        view.setTag(viewHolder);
    }else
    {
        viewHolder= (ViewHolder) view.getTag();
    }
        viewHolder.img.setImageResource(R.mipmap.ic_launcher);
        viewHolder.title.setText(data.get(i).iconTitle);
        viewHolder.content.setText(data.get(i).iconContent);
    return view;
}
class ViewHolder{
    public  ImageView img;
     public TextView title,content;
}

}

NewsBean类:

package com.example.kira.httpurldemo;

public class NewsBean {
public String iconUrl;//图片的网址即picSmall
public String iconTitle;//图片的标题即json中的name属性
public String iconContent;//图片的内容即json中的description

}

为什么ListView显示都一样的?图片说明

2个回答

sonken_0824
sonken_0824   2016.09.28 14:31
                            newsBean.iconUrl=jsonObject.getString("picSmall");
            newsBean.iconTitle=jsonObject.getString("name");
            newsBean.iconContent=jsonObject.getString("description");
            list.add(newsBean);

这里的 newsBean 对象是同一个,只不过在for循环里加载了N次;

在for循环里new 新的 bean !

leerfun
leerfun 高手!一眼看出要害!
9 个月之前 回复
oSanYeCao1234567
oSanYeCao1234567   Ds   Rxr 2016.09.25 20:56

先检查下数据源,看数据是否是重复的

Csdn user default icon
上传中...
上传图片
插入图片