ArrayAdapter和改进2.0

时间:2015-11-28 22:02:09

标签: java android class adapter retrofit

如何使此适配器与改装2.0一起使用? 我的意思是在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;
}


}

关于TelAdapter.class我要编写私有类Holder还是静态类Holder?

2 个答案:

答案 0 :(得分:0)

你有没有尝试过:

@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);

}

答案 1 :(得分:0)

Patrick提供的解决方案是正确的。只是你需要为ArrayAdapter增加1个资源参数。这应该有用。

@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, android.R.layout.simple_list_item_1, date);
          listView.setAdapter(adapter);

}