如何获取数组适配器的 json 值

how to get a json value to array adapter

我需要帮助,我从 php 得到了 name,如何将它发送到阵列适配器?

protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);
        String name;
        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String name = c.getString(TAG_NAME);


            ImageListAdapter itemsAdapter = 
                    new ImageListAdapter(MainActivity.this, name);

我在这个 ImageListAdapter 构造函数中遇到问题。

  public ImageListAdapter(Context context, String imageUrls) {
        super(context imageUrls);

        this.context = context;
        this.imageUrls = imageUrls;

        inflater = LayoutInflater.from(context);
    }

如何从 json 获取名称到数组适配器

package com.example.jsn;

import com.squareup.picasso.Picasso;

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

public class ImageListAdapter extends ArrayAdapter {
    private Context context;
    private LayoutInflater inflater;

    private String[] imageUrls;

    public ImageListAdapter(Context context, String imageUrls) {
        super(context imageUrls);

        this.context = context;
        this.imageUrls = imageUrls;

        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }

        Picasso
            .with(context)
            .load(imageUrls[position])
            .placeholder(R.drawable.ic_launcher) // can also be a drawable
            .fit() // will explain later
            .noFade()
            .into((ImageView) convertView);

        return convertView;
    }
}

适配器将其最后一个值作为数组。将字符串存储在数组或 Arraylist 中,然后将其放入适配器中。 阅读有关适配器的更多信息; http://developer.android.com/reference/android/widget/ArrayAdapter.html

您需要将 ImageListAdapter 构造函数更改为以下内容:

 public ImageListAdapter (Context context,  String[] urls) {
    super(context, -1, urls);

    this.context = context;
    this.imageUrls = urls;

    inflater = LayoutInflater.from(context);
}

这样您就可以将一个字符串数组传递给您的适配器。另外,您必须稍微修改一下 showList() 方法。试试这个:

protected void showList(){
    try{
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);
        String[] names = new String[peoples.length()];
        for ( int i = 0; i < peoples.length(); i++ ) {
            JSONObject c = peoples.getJSONObject(i);
            names[i] = c.getString(TAG_NAME);
        }

        ImageListAdapter itemsAdapter = new ImageListAdapter(MainActivity.this, names);



}

希望对您有所帮助。