我想通过json在android的listview上显示人名

Posted

技术标签:

【中文标题】我想通过json在android的listview上显示人名【英文标题】:I want to show names of people on the listview in android through json 【发布时间】:2018-01-28 11:57:28 【问题描述】:

我正在使用 asynctask,我想通过它获取名称的 json 数组并希望在 listview 中显示。 这是我的php代码:

    $con=mysqli_connect("localhost","root","","raheel");

    $id=intval($_POST['id']);

   $mysqli_qry="select* from name where Name_id='$id'; ";


   $result=mysqli_query($con,$mysqli_qry)or die("Error Occur in Connection" . mysqli_error($con));

  $rowcount=mysqli_num_rows($result);

    $arr=Array();

    if($rowcount>0)


   while($row=mysqli_fetch_assoc($result))
   


        $arr=$row;

    




header('Content-Type: application/json');
echo json_encode($arr); 
mysqli_close($con

?>

我面临的问题是我将如何在异步任务模式下获取 json 数组并将数据(名称)分配给列表视图 这是我的 Home.java 代码: 包 com.example.raheel.bank;

public class Home extends AppCompatActivity implements HomeCallBack 

ListView listView;
private ListAdapter mAdapter;

 p public ArrayList<String > info;

@Override
 protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);




    listView=(ListView)findViewById(R.id.Listview);


    int id=0;











BackgroundHome home=new BackgroundHome(Home.this, (Callback)Home. this);
@Override
public void processData(JSONArray data)


            if(data!=null)
            
                info=new ArrayList<>();
                try
                


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

                        JSONObject js=data.getJSONObject(i);
                    




                 catch (JSONException e) 
                    e.printStackTrace();
                

            
    mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,info);
    listView.setAdapter(mAdapter);




 class BackgroundHome extends AsyncTask<Integer,Integer,JSONArray>



Context c;
Callback callback;

public BackgroundHome(Context c, Callback callback) 
    this.c = c;
    this.callback = callback;


@Override
protected JSONArray doInBackground(Integer...params) 

    JSONArray js=null;

    String url=c.getString(R.string.url) +"Home.php";

    try 
        HttpRequest httpRequest = new HttpRequest(url);
        HashMap<String, String> values = new HashMap<>();
        values.put("id", Integer.toString(params[0]));
        js = httpRequest.prepare(HttpRequest.Method.POST).withData(values).sendAndReadJSONArray();
     catch (IOException e) 
        e.printStackTrace();
     catch (JSONException e) 
        e.printStackTrace();
    
    return js;


protected void onPostExecute(JSONArray jsonArray)

    HomeCallBack.processData(jsonArray);











interface HomeCallBack

public void processData(JSONArray data);

 

这是 Home.java 文件,但我不知道如何从 php 文件中获取名称的 json 数组。

【问题讨论】:

上面的代码能用吗?听起来您的问题不在于 PHP 部分,而在于 Android 列表视图部分? 是的,我有一个用于测试的 html 文件,当我将 Name_id 放入其中时,它会显示关于该 id 的唯一名称 那么您应该更新您的问题并显示您实际遇到问题的代码。没有理由向我们展示有效的代码。问题应该是:“如何使用 json 数组中的数据填充列表视图”。它来自 PHP 的事实与问题无关。 这里是java代码@MagnusEriksson 【参考方案1】:

您必须像这样将数据添加到数组中。将 $arr = $row 修改为 $arr[] = .. 将为您提供带有键/值数据的结果数组。 json_encode 稍后在您的代码中将转换为应用程序所需的 json 数组。

while($row=mysqli_fetch_assoc($result))
       
            $arr[] = array('name' => $row["Name"], 'id' => $row["Name_id"]);

       

【讨论】:

不,没有错误,因为我从我的 html 表单中测试它会给出结果,因为我把 id 给它给出结果 "Name_id":"10","Names":"Raheel" 喜欢这个@Dian Nedelchev【参考方案2】:

你在最后一行代码有错误,你没有关闭括号

mysqli_close($con); // in your code => mysqli_close($con

并更新您的 while 循环,如下所示:

$arr[] = $row;

【讨论】:

@RaheelSajjad 你没有关闭括号 php 没有错误。我的问题是如何在 java 中获取 json 数组? php 给了我正确的结果,例如 "Name_id":"10","Names":"Raheel" 这个@aidinMC 在你的 php 代码中它只返回最后一行@RaheelSajjad

以上是关于我想通过json在android的listview上显示人名的主要内容,如果未能解决你的问题,请参考以下文章

我想使用 json 将 Listview 数据发送到 Android 中的另一个 Activity

如何使用json解析android在listview中获取图像

从 Android Studio 中的 JSON 文件在 ListView 中显示图像

从服务器 json 响应延迟加载 listview 项

Android获取服务器Json字符串并显示在ListView上面

Android网络框架-Volley实践 使用Volley打造自己定义ListView