JSONArray到Android ListView

时间:2017-10-14 16:01:15

标签: android json listview

我是编程新手,我当前的应用程序只从我的服务器获取数据,只输入一个数据并将其输入textview。但现在我已经成功创建了一个PHP脚本,用多个值编码JSONArray,但我不知道如何通过循环获取android代码的值,我也不知道如何填充我的listview。

示例JSON

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <h2>Button group</h2>
  <div class="btn-group">
    <button id="button1" type="button" class="btn btn-primary">Div 1</button>
    <button id="button2" type="button" class="btn btn-primary">Div 2</button>
    <button id="button3" type="button" class="btn btn-primary">Div 3</button>
    <button id="button4" type="button" class="btn btn-primary">Div 4</button>
  </div>
  <div class="div-group">
    <div id="div1" style="background-color: red;">div 1</div>
    <div id="div2" style="background-color: blue;">div 2</div>
    <div id="div3" style="background-color: red;">div 3</div>
    <div id="div4" style="background-color: blue;">div 4</div>
  </div>
</div>

PHP脚本

[{"lnumber":"2","violation":"2"},{"lnumber":"2","violation":"No Helmet"}]

我目前如何从服务器获取数据并仅使用textview显示它。

Android代码

$result = array();
$sql = "SELECT lnumber,violation FROM violators WHERE 
lnumber='".$lnumber."'";
$stmt = $con->prepare($sql);
$stmt->execute();
$stmt->bind_result($lnumber, $violation);
while($stmt->fetch())
{
$temp = [
'lnumber'=>$lnumber,
'violation'=>$violation
];
array_push($result, $temp);
}
echo json_encode($result);

PS: 我的Listview id是listView。如果有人可以帮助我如何从服务器向列表视图显示多个数据将是一个非常好的帮助!

2 个答案:

答案 0 :(得分:2)

  

1.您的JSON根标记为[]。您必须使用JSONArray。它不是JSONObject

     

2.您的datetime中的示例未包含JSONObject个键。

试试这个

<强>公司Databean

public class DataBean {

/**
 * lnumber : 2
 * violation : 2
 */

private String lnumber;
private String violation;

public String getLnumber() {
    return lnumber;
}

public void setLnumber(String lnumber) {
    this.lnumber = lnumber;
}

public String getViolation() {
    return violation;
}

public void setViolation(String violation) {
    this.violation = violation;
}
}

<强>适配器

public class MyAdapter extends BaseAdapter {

    private List<DataBean> beanList;
    private LayoutInflater inflater;
    private Context context;

    public MyAdapter() {
    }

    public MyAdapter(List<DataBean> beanList, Context context) {
        this.context = context;
        this.beanList = beanList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return beanList == null ? 0 : beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.your_layout, null);
        DataBean dataBean = beanList.get(position);
        text.setText(dataBean.getLnumber() + dataBean.getViolation());
        return view;
    }
}

<强>活动

private List<DataBean> beanList = new ArrayList<>();
private ListView listView;

private void showJSON(String response) {

    listView = (ListView) findViewById(R.id.listview);
    try {
        String lnumber;
        String violation;
        String datetime;

        DataBean dataBean = new DataBean();
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            lnumber = jsonObject.getString("lnumber");
            violation = jsonObject.getString("violation");
            dataBean.setLnumber(lnumber);
            dataBean.setViolation(violation);
            beanList.add(dataBean);
        }
        MyAdapter myAdapter = new MyAdapter(beanList,MainActivity.this);
        listView.setAdapter(myAdapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

所以你想从你拥有的php脚本中获取json 使用AsyncDataClass从你的php脚本中获取json,因为你是初学者,首先阅读有关AsyncDataClass的内容,然后尝试使用Log.d打印你从脚本中获取的json(&#34;&#34;,&#34; &#34;)然后继续下一步。