[{"acteurid":"86744570","productions":[{"variete":"Riz","recolte":"10000"}],"nom":"Charles Diouf"},{"acteurid":"6535150","productions":[{"variete":"Riz","recolte":"1000"}],"nom":"Daba Diouf"},{"acteurid":"86817462","productions":[{"variete":"Riz","recolte":"8000"}],"nom":"Diel Ndour"},{"acteurid":"14047190","productions":[{"variete":"Ble","recolte":"10000"},{"variete":"Mais","recolte":"1000"},{"variete":"Mais","recolte":"2000"},{"variete":"Riz","recolte":"5000"},{"variete":"Ble","recolte":"8000"}],"nom":"Hamady Diouf"}]

我如何读取上述json数据?我想以ListView形式显示它,如下所示:
  • nom
  • 品种
  • recolte
  • .variete
  • .recolte



  • nom
  • 品种
  • recolte

  • 我尝试了以下代码,但未显示列表。我有
    FATAL EXCEPTION: main
    java.lang.NullPointerException at
           pcom.jsontest.MyCustomAdapter.getView(MyCustomAdapter.java:32)
    

    代码是
    for(int i=0;i<jarray.length();i++){
                JSONObject json=jarray.getJSONObject(i);
    
                JSONArray prodar=json.getJSONArray("productions");
                for(int j=0;j<prodar.length();j++){
                    JSONObject prod=prodar.getJSONObject(j);
                    Production production = new Production(prod.getString("variete"),prod.getString("recolte"));
                    productions.add(production);
                }
    
                Producteur producteur=new Producteur(json.getString("acteurid"),productions,json.getString("nom"));
                producteurs.add(producteur);
            }
    
    
    MyCustomAdapter adapter = new MyCustomAdapter(this,producteurs);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);
    

    自定义阵列适配器
    public class MyCustomAdapter extends ArrayAdapter<Producteur> {
        public MyCustomAdapter(Context context, ArrayList<Producteur> prods) {
            super(context, 0, prods);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            Producteur producteur = getItem(position);
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
            }
            // Lookup view for data population
            TextView name = (TextView) convertView.findViewById(R.id.name);
            name.setText(producteur.getNom());
    
            ArrayList<Production> productions=producteur.getProductions();
            ListView listprod = (ListView) convertView.findViewById(R.id.list);
            ArrayAdapter<Production> productionsAdapter = new ArrayAdapter<Production>(getContext(), R.layout.list_prod_item, productions);
            listprod.setAdapter(productionsAdapter);
    
           // Return the completed view to render on screen
            return convertView;
        }
    }
    

    list_item.xml
     <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/nom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textColor="#43bd00"
            android:textSize="16sp"
            android:textStyle="bold" />
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        </LinearLayout>
    

    list_prod_item.xml
    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/variete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textColor="#43bd00"
            android:textSize="16sp"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/recolte"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:paddingTop="6dip"
            android:textColor="#43bd00"
            android:textSize="16sp"
            android:textStyle="bold" />
    
    </LinearLayout>
    

    最佳答案

    为了完成dilix的示例,假设您的模型称为Producteur:

    public class Producteur {
        // Private attributes
        private int mId;
        // List of Produit, which is another model or object (call it however you want) created by you.
        private List<Produit> mProduits;
        private String mNom;
    
        // Attributes getters and setters (they are optionnal in this example, but it's always good idea to have them)
        public int get_mId() { return this.mId; }
        public void set_mId(int id) { this.mId = id; }
    
        public List<Produit> get_mProduits() { return this.mProduits; }
        public void set_mProduits(List<Produit> produits) { this.mProduits = produits; }
    
        public String get_mNom() { return this.mNom; }
        public void set_mNom(String nom) { this.mNom= nom; }
    
        // Constructor
        public Producteur(int id, List<Produit> produits, String nom) {
            this.mId = id;
            this.mProduits = produits;
            this.mNom = nom;
        }
    }
    

    然后,您将拥有表示生产者产品之一的模型Produit:
    public class Produit {
        // A product has two attributes: variete and recolte. I'll let you complete
        // this class, you can inspire yourself of the above example.
    }
    

    现在是时候在数据读取中使用这两个对象了:
    // Instead of a hashmap, we'll be using a list of "Producteur"
    List<Producteur> producteurs = new List<Producteur>();
    JSONArray jarray=new JSONArray(resSel);
    //String acteur_id="";
    for(int i=0;i<jarray.length();i++){
        JSONObject json = jarray.getJSONObject(i);
    
        int id = json.getInt("acteurid");
        // Note the name is not part of the "productions" JSONArray
        String nom = json.getString("nom");
    
        // Here we'll declare the productor's productions
        List<Produit> productions = new List<Produit>();
    
        JSONArray prodar=json.getJSONArray("productions");
        for(int j=0;j<prodar.length();j++){
            JSONObject prod=prodar.getJSONObject(j);
            String variete = prod.getString("variete");
            int recolte = Integer.parseInt(prod.getString("recolte"));
    
            // We'll create a new "Produit" at each loop and add it to the productor's product list.
            Produit unProduit = new Produit(variete, recolte);
            productions.add(unProduit);
        }
    
        // We'll create a new productor at each loop and add it to the productors list
        Producteur unProducteur = new Producteur(id, productions, nom);
        producteurs.add(unProducteur);
    }
    

    然后,要将其添加到listView中,可以使用ArrayAdapter<Producteur>:
    ListView listprod = (ListView)findViewById(R.id.list);
    // We will passe the list of "Producteur" we just created above as the objects to represent in the ListView
    ArrayAdapter<Producteur> producteursAdapter = new ArrayAdapter<Producteur>(this, R.layout.list_item, producteurs);
    listprod.setAdapter(producteursAdapter);
    

    请注意,这是在ListView中显示生产者的最基本方法。如果要自定义对象显示,请检查this link

    08-03 14:25
    查看更多