ListView包含来自JSON的图像和文本,使用ArrayAdapter

时间:2016-06-29 04:37:47

标签: java android json

我知道从ListItem获取:

string[] = {"data1","data","data3"};

但是如何从JSON响应中获取它?我把数据移到了:

ArrayList<HashMap<String, String>>

我的代码如下,我必须使用JSON而不是String[]因为我的JSON响应包含id,标题和图像链接。

public class MainActivity extends AppCompatActivity {

    String[] titles,header;
    ListView list1;
    int[] img;
    String[] title;
    int[] img = {R.drawable.img1, R.drawable.img2,R.drawable.img3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list1 = (ListView) findViewById(R.id.list);


        title ={"data1","data2","data3"}

        myAdapter adapter = new myAdapter(getApplicationContext(), titles, img);


        list1.setAdapter(adapter);

    }

    class myAdapter extends ArrayAdapter<String> {
        Context context;
        int[] imgs;
        String[] titles;


        private ArrayList<String> mData = new ArrayList<>();
        private ArrayList<Integer> mImage = new ArrayList<>();
        private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
        private LayoutInflater mInflater;

        myAdapter(Context context, String[] titles, int imgs[]) {
            super(context, R.layout.list_item, R.id.text, titles);
            this.context = context;
            this.imgs = imgs;
            this.titles = titles;
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }


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

            if (convertView == null) {
                holder = new ViewHolder();

                        convertView = mInflater.inflate(R.layout.list_item, parent, false);

                        holder.myImage = (ImageView) convertView.findViewById(R.id.image);

                        holder.myText = (TextView) convertView.findViewById(R.id.texttitle);


                        Picasso.with(context)
                                .load(imgs[position])
                                .fit()
                                .into(holder.myImage);

                        holder.myText.setText(titles[position]);
                        convertView.setTag(holder.myImage);



            return convertView;
        }

        public class ViewHolder {
            public TextView myText;
            public ImageView myImage;
        }

    }


}

3 个答案:

答案 0 :(得分:1)

我建议您切换到Volley库,以便从Json加载图像。

这是一个完美教程的链接,它逐步显示了如何实现您的目标:

http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

如果您遇到任何困难,请不要在下面发表评论!

答案 1 :(得分:0)

首先按以下方式更改上面的代码,以便正确查看图像和字符串列表:

public class MainActivity extends AppCompatActivity {

ListView list1;
String[] title = {"data1", "data2", "data3"};
;
int[] img = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list1 = (ListView) findViewById(R.id.list);


    myAdapter adapter = new myAdapter(getApplicationContext(), title, img);


    list1.setAdapter(adapter);

  }
}

class myAdapter extends ArrayAdapter<String> {
Context context;
int[] imgs;
String[] titles;


private ArrayList<String> mData = new ArrayList<>();
private ArrayList<Integer> mImage = new ArrayList<>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;

myAdapter(Context context, String[] titles, int imgs[]) {
    super(context, R.layout.list_item, R.id.text, titles);
    this.context = context;
    this.imgs = imgs;
    this.titles = titles;
    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


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

    if (convertView == null) {
        holder = new ViewHolder();

        convertView = mInflater.inflate(R.layout.list_item, parent, false);

        holder.myImage = (ImageView) convertView.findViewById(R.id.image);

        holder.myText = (TextView) convertView.findViewById(R.id.texttitle);

        holder.myImage.setImageResource(imgs[position]);


   //            Picasso.with(context)
   //                    .load(imgs[position])
   //                    .fit()
   //                    .into(holder.myImage);

        holder.myText.setText(titles[position]);
        convertView.setTag(holder.myImage);


    }
    return convertView;

}

  public class ViewHolder {
    public TextView myText;
    public ImageView myImage;
  }
}

答案 2 :(得分:0)

您需要创建自己的适配器类,扩展ArrayAdapter。

下面是一些可以实现它的链接。

http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/

https://www.youtube.com/watch?v=0Lr37suTPpg

如果您在实施过程中遇到任何问题,请发表评论。

@Rathiga: - 根据您的要求,我已根据您的问题发表评论来创建此答案。