简体   繁体   English

如何填充 ListView Android 中的所有列

[英]How To populate All Columns In ListView Android

I am getting data from mysql database through php , I fetching both values but the adapter is displaying only one.我通过phpmysql database获取数据,我获取了两个值,但适配器只显示一个。

This is my android getData() function which is getting one column now I want to get the second column which i am not getting这是我的 android getData()函数,它现在得到一列我想得到我没有得到的第二列

public void getdata(){
    try {
        URL url=new URL(retri_url);
        try {
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            IS=new BufferedInputStream(httpURLConnection.getInputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(IS));

        StringBuilder stringBuilder=new StringBuilder();
        while((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line+"\n");    
        }
        IS.close();
       result=stringBuilder.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }
    try{
        JSONArray jsonArray=new JSONArray(result);
        JSONObject jsonObject=null;
        data=new String[jsonArray.length()];

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

            jsonObject=jsonArray.getJSONObject(i);

            int id=Integer.parseInt(jsonObject.get("id").toString());
            String strI = String.valueOf(id);
            data[i] =strI;
            data[i]=jsonObject.getString("Name");    
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }  
}

The issue is the code is over writing data of names over id using a single array data here问题是代码在这里使用单个数组dataid上写入names data

data[i] =strI;
data[i]=jsonObject.getString("Name");

I suggest to move on to customize adapter but as requested a quick fix , here is the solution我建议继续自定义适配器,但根据要求快速修复,这是解决方案

1.) Create three arrays 1.) 创建三个数组

String[] names, data;
//        ^^        to store names        
//              ^^  to store re-presentable data like "pavneet   2122"  
int[] id ;
//   ^^  to store ids

2.) Initialize arrays 2.) 初始化数组

data=new String[jsonArray.length()];
names=new String[jsonArray.length()];
id=new int[jsonArray.length()];

3.) Use optInt or optString , will handle the parsing for you 3.) 使用optIntoptString ,将为您处理解析

JSONArray jsonArray=new JSONArray(result);
JSONObject jsonObject=null;
data=new String[jsonArray.length()];
names=new String[jsonArray.length()];
id=new int[jsonArray.length()];

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

 jsonObject=jsonArray.getJSONObject(i);
    id[i]=jsonObject.optInt("id");
    name[i]=jsonObject.optString("Name");
    data[i]=id[i]+"\t\t"+name[i];

Note : \\t\\t is for tab space so you can use \\n for two line formatting注意: \\t\\t用于制表符空间,因此您可以使用\\n进行两行格式设置

and you can use this data[i]=name[i]+"\\t\\t"+id[i];你可以使用这个data[i]=name[i]+"\\t\\t"+id[i]; to display name and id显示名称和 ID
` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM