【问题标题】:show result in listview in android from retrofit通过改造在 android 的 listview 中显示结果
【发布时间】:2017-08-13 16:45:10
【问题描述】:

我正在尝试通过使用改造库在 listview 中显示 json 的结果,代码是正确的,没有任何错误,但它不显示结果....如果有人可以帮助我。

job_vec.php

<?php 

	if($_SERVER['REQUEST_METHOD']=='GET'){
		
 
       // $id  = $_GET['id'];		
		require_once('db.php');
		
		$sql = "SELECT * FROM  job_entity ";
		
		$r = mysqli_query($connection,$sql);		
		$result = array();
		
		while($row = mysqli_fetch_array($r)){
               array_push($result,
               array('Id'=>$row['id'],
			   'Title'=>$row['Title'],
			   'City'=>$row['City'],
			   'Address'=>$row['Address'],
			   'Specialization'=>$row['Specialization'],
			   
	));
}
		echo json_encode(array("result"=>$result));
		
		mysqli_close($connection);
		
	}
	?>

jobAPI.java

public interface jobAPI {
    @GET("job_vec.php")
    public void getJobs(Callback<List<job_entity>> response);
}

以及在列表视图中显示结果的方法

package com.example.hamdaalissaei.flah.service;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.hamdaalissaei.flah.InterfaceAPI.jobAPI;
import com.example.hamdaalissaei.flah.R;
import com.example.hamdaalissaei.flah.model.job_entity;

import java.util.List;

import retrofit.RestAdapter;
import retrofit.RetrofitError;

public class Job_vacancy extends AppCompatActivity  implements ListView.OnItemClickListener {
    public static final String ROOT_URL = "http://192.168.0.120/flah";
    public static final String KEY_Title = "Title";
    public static final String KEY_City = "City";
    public static final String KEY_Address = "Address";
    public static final String KEY_Specialization= "Specialization";
    private ListView listView;
    private List<job_entity> jobs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_vacancy);
        listView = (ListView) findViewById(R.id.list);
        getJobs();
        //Setting onItemClickListener to listview
        listView.setOnItemClickListener(this);
    }
    private void getJobs() {
        //While the app fetched data we are displaying a progress dialog
        final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);

        //Creating a rest adapter
        RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint(ROOT_URL)
                .build();

        //Creating an object of our api interface
        jobAPI api = adapter.create(jobAPI.class);

        //Defining the method
        api.getJobs(new retrofit.Callback<List<job_entity>>() {
            @Override
            public void success(List<job_entity> job_entities, retrofit.client.Response response) {
                //Dismissing the loading progressbar
                loading.dismiss();
                //Storing the data in our list
                jobs = job_entities;


                //Calling a method to show the list
                showList();
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    //Our method to show list
    private void showList(){
        //String array to store all the book names
        String[] items = new String[jobs.size()];

        //Traversing through the whole list to get all the names
        for(int i=0; i<jobs.size(); i++){
            //Storing names to string array
            items[i] = jobs.get(i).getTitle();
        }

        //Creating an array adapter for list view
        ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.simple_list,items);

        //Setting adapter to listview
        listView.setAdapter(adapter);
    }


    //This method will execute on listitem click
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Creating an intent
        Intent intent = new Intent(this, jobs.class);

        //Getting the requested book from the list
        job_entity job = jobs.get(position);

        //Adding book details to intent
        intent.putExtra(KEY_Title,job.getTitle());
        intent.putExtra(KEY_Address,job.getAddress());
        intent.putExtra(KEY_City,job.getCity());
        intent.putExtra(KEY_Specialization,job.getSpecialization());

        //Starting another activity to show book details
        startActivity(intent);
    }
}

【问题讨论】:

    标签: android arrays json listview retrofit


    【解决方案1】:

    我认为你应该使用notifyDataSetChanged 在 setAdapter() 之后的 showList() 方法中。

    【讨论】:

    • 我尝试不使用 notifyDataSetChanged 但仍然是同样的问题
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多