我有listview必须填写JSON文件,但直到现在运行应用程序后我什么也看不见,我不知道java代码或JSON文件的问题? 另外我注意到我无法在@Path ,,
中添加文件扩展名MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
final Retrofit retrofit = new Retrofit.Builder().baseUrl("http://10.0.2.2").addConverterFactory(GsonConverterFactory.create()).build();
TelService service = retrofit.create(TelService.class);
final retrofit.Call<List<tel_list>> calls = service.getphones("telephones");
calls.enqueue(new Callback<List<tel_list>>() {
@Override
public void onResponse(Response<List<tel_list>> response, Retrofit retrofit) {
if (!response.isSuccess()) {
//TODO
return;
}
List<tel_list> telephones = response.body();
TelAdapter adapter = new TelAdapter (MainActivity.this, telephones);
listView.setAdapter(adapter);
}
@Override
public void onFailure(Throwable t) {
}
});
}
}
TelService.java
public interface TelService {
@GET("/alruthea/{getphones_1}.php")
retrofit.Call<List<tel_list>> getphones(@Path("getphones_1")String telephones);
}
TelAdapter.java
public class TelAdapter extends ArrayAdapter<tel_list> {
private Context context;
private List<tel_list> telephones;
public TelAdapter(Context context, List<tel_list> data) {
super(context, R.layout.list_item, data);
telephones = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View item = inflater.inflate(R.layout.list_item, null);
TextView name = (TextView) item.findViewById(R.id.names);
name.setText(telephones.get(position).getName());
TextView number = (TextView) item.findViewById(R.id.numbers);
number.setText(telephones.get(position).getNumber());
return item;
}
}
tel_list.java
public class tel_list {
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}