Android ListView - 在PHP中循环json_encode,但只在ListView中显示一行

时间:2016-05-27 18:54:40

标签: android json listview

我想把数据json形式的PHP文件循环并放入我的android活动中的数组列表(ListView),但它只显示Listview中的一行,这是json的第一行。

在我的活动中我的表格: my table

这是我的PHP文件:

<?php

$con = mysqli_connect("xxxx","xxxx","xxxx","xxxx");

    $nama_klp = $_POST["nama_klp"];

    $sql2 = "select id_klp from kelompok where nama_klp ='$nama_klp' ";
    $result2 = mysqli_query($con, $sql2);
    $followingdata2 = $result2->fetch_assoc();            
    $id_klp = $followingdata2['id_klp'];

    $sql2 = "select id_mhs from praktikan where id_klp ='$id_klp' ";
    $result2 = mysqli_query($con, $sql2) or die("Error in Selecting " . mysqli_error($con));


    while($row2 =mysqli_fetch_assoc($result2))
    {
        $id_mhs = $row2['id_mhs'] ;


        $sql = "select nama_mhs from mahasiswa where id_mhs ='$id_mhs' ";
        $result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));

        $response = array();
        while($row =mysqli_fetch_assoc($result))
        {
            $response[] = $row ;

        }
        echo json_encode($response);


    }

?>

它以这种格式返回一个json字符串:

[{"nama_mhs":"iyan2"}][{"nama_mhs":"illiyan"}][{"nama_mhs":"12"}][{"nama_mhs":"12"}]

这是我的活动代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;
import java.util.List;

public class DaftarNilaiUtama2 extends AppCompatActivity {

    ListView LvMhs;

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

        TextView tvMatkul = (TextView) findViewById(R.id.tvNamaMatkul);
        TextView tvKlp = (TextView) findViewById(R.id.tvNamaKlp);

        Intent intent = getIntent();
        String matkul = intent.getStringExtra("matkul");
        String klp = intent.getStringExtra("klp");

        tvMatkul.setText(matkul);
        tvKlp.setText(klp);

        LvMhs = (ListView) findViewById(R.id.lvMhs);
        final TextView text = (TextView) findViewById(R.id.textView4);


        String nama_klp = (String) tvKlp.getText();


        Response.Listener responseListener= new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                try {
                    List<String> item = new ArrayList<String>();
                    JSONArray jsonMainNode = new JSONArray(response);
                    JSONObject jsonResponse = new JSONObject();

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

                        jsonResponse = jsonMainNode.getJSONObject(i);

                        String nama_mhs = jsonResponse.optString("nama_mhs");
                        item.add(nama_mhs);

                    }

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(DaftarNilaiUtama2.this, android.R.layout.simple_list_item_1, android.R.id.text1, item);


                    LvMhs.setAdapter(adapter);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            };

            RequestLvIdMhs lvReq = new RequestLvIdMhs(nama_klp, responseListener);
            RequestQueue queue = Volley.newRequestQueue(DaftarNilaiUtama2.this);
            queue.add(lvReq);



    }
}

我的服务器请求代码:

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by callbee on 27/05/2016.
 */
public class RequestLvIdMhs extends StringRequest {

    private static final String LvMhs_URL = "http://192.168.1.10/KartuKontrolApp/LvIdMhs-DNilaiU2.php";
    private Map<String, String> params;

    public RequestLvIdMhs(String nama_klp, Response.Listener<String> listener) {
        super(Method.POST, LvMhs_URL, listener, null);
        params = new HashMap<>();

        params.put("nama_klp", nama_klp);


    }

    public Map<String, String> getParams() {
        return params;
    }


}

1 个答案:

答案 0 :(得分:0)

你的php json字符串错误,它应该像跟随字符串一样。

{"data1":[{"nama_mhs":"iyan2"}],"data2":[{"nama_mhs":"illiyan"}],"data3[{"nama_mhs":"12"}],"data4":[{"nama_mhs":"12"}]}

现在您可以使用相应的密钥来解析所有数组,因为您还必须更改您的Java代码。