abood250 abood250 - 5 months ago 54
Android Question

ArrayAdapter and retrofit 2.0

How can I make this Adapter working with retrofit 2.0?
I mean in MainActivity

MainActivity.java

public class MainActivity extends AppCompatActivity {

private ListView listView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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





Retrofit retrofit = new Retrofit.Builder().baseUrl("http://10.0.2.2/alruthea").build();

final TelService service = retrofit.create(TelService.class);
service.getphones(new Callback<List<tel_list>>() {
@Override
public void onResponse(Response<List<tel_list>> response, Retrofit retrofit) {

ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, Telephones);
listView.setAdapter(adapter);




}

@Override
public void onFailure(Throwable t) {

}
}


);
}


}


TelService.java

public interface TelService {
@GET("/getphones.php")

retrofit.Call<List<tel_list>> getphones();
}


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;
}
}


TelAdapter.java

public class TelAdapter extends ArrayAdapter<tel_list>{

Context context;


public TelAdapter(Context context, int resource, ArrayList<tel_list> users){
super(context, resource, users);

this.context = context;
}



@Override
public View getView(int position, View cell, ViewGroup parent){

Holder holder;

if (cell == null) {
holder = new Holder();

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

cell = inflater.inflate(R.layout.list_item, null);

holder.textview = (TextView) cell.findViewById(R.id.names);
holder.textview1 = (TextView) cell.findViewById(R.id.numbers);
cell.setTag(holder);
}else {
holder = (Holder)cell.getTag();

}
tel_list user = getItem(position);

holder.textview.setText(user.getName());
holder.textview1.setText(user.getNumber());

return cell;
}

private class Holder{

TextView textview;
TextView textview1;
}


}


and about TelAdapter.class do I to write private class Holder or static class Holder?

Answer

Did you try it:

@Override
  public void onResponse(Response<List<tel_list>> response, Retrofit retrofit) {
    if (!response.isSuccess()) {
        //TODO 
        return;
    }
     List<tel_list> date = response.body();


      ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, date);
          listView.setAdapter(adapter);

}
Comments