如何使用Listview中的Retrofit获取数据

时间:2017-06-24 17:26:31

标签: android listview retrofit2

我的适配器类中出现了nullPointException。

我希望从包含姓名和电子邮件的服务器获取联系方式。 我的联系人课程是

public class Contact {

@SerializedName("name")
private String Name;

@SerializedName("email")
private String Email;


public String getName() {
    return Name;
}

public String getEmail() {
    return Email;
}

}

ApiClient CLass

public class ApiClient {

private static final String BASE_URL = "http://192.168.43.88/ContactApp/";
public static Retrofit retrofit = null;

public static Retrofit getApiClient (){

    if (retrofit == null){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    return retrofit;
}

}

这是一个适配器类,我在infalter中获得空指针异常

public class RetrofitItemAdapter扩展了BaseAdapter {

Context mContext;
private List<Contact> mContact;
RecyclerView.ViewHolder vh = null;

//Constructor


public RetrofitItemAdapter(List<Contact> mContact) {

    this.mContact = mContact;
}


public  class ViewHolder {

    public TextView tvName;
    public TextView tvEmail;


}


@Override
public int getCount() {
    return mContact.size();
}

@Override
public Object getItem(int position) {
    return mContact.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {

    View vi = convertView;
    ViewHolder holder;

    if (convertView == null) {

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

        vi = inflater.inflate(R.layout.listview_retrofit_items, null);

        holder = new ViewHolder();
        holder.tvName  = (TextView) vi.findViewById(R.id.tv_ret_name);
        holder.tvEmail  = (TextView) vi.findViewById(R.id.tv_ret_email);


        vi.setTag(holder);

    } else
        holder = (ViewHolder) vi.getTag();

    // now set your text view here like

   // holder.tvName.setText("Bla Bla Bla");

    holder.tvName.setText(mContact.get(position).getName());
    holder.tvEmail.setText(mContact.get(position).getEmail());


    // return your view
    return vi;

最后我的主要课程是

public class RetrofitDemo extends AppCompatActivity {

private ListView listView;
private List<Contact> contacts;
private RetrofitItemAdapter adapter;
private ApiInterface apiInterface;


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

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

    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    Call<List<Contact>> call = apiInterface.getContacts();

    call.enqueue(new Callback<List<Contact>>() {
        @Override
        public void onResponse(Call<List<Contact>> call, Response<List<Contact>> response) {

            contacts = response.body();

            adapter = new RetrofitItemAdapter(contacts);
            listView.setAdapter(adapter);

        }

        @Override
        public void onFailure(Call<List<Contact>> call, Throwable t) {

        }
    });

}

}

Logcat错误

E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.example.admin.myapplication, PID: 2528
                                             java.lang.NullPointerException
                                                 at com.example.admin.myapplication.RetrofitItemAdapter.getView(RetrofitItemAdapter.java:66)
                                                 at android.widget.AbsListView.obtainView(AbsListView.java)
                                                 at android.widget.ListView.makeAndAddView(ListView.java:1790)
                                                 at android.widget.ListView.fillDown(ListView.java:691)
                                                 at android.widget.ListView.fillFromTop(ListView.java:752)
                                                 at android.widget.ListView.layoutChildren(ListView.java:1630)
                                                 at android.widget.AbsListView.onLayout(AbsListView.java)
                                                 at android.view.View.layout(View.java:14834)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
                                                 at android.view.View.layout(View.java:14834)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                 at android.view.View.layout(View.java:14834)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                 at android.view.View.layout(View.java:14834)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                 at android.view.View.layout(View.java:14834)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                 at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                 at android.view.View.layout(View.java:14834)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                 at android.view.View.layout(View.java:14834)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1988)
                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1745)
                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1001)
                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5623)
                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                 at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                 at android.os.Handler.handleCallback(Handler.java:733)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:136)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5241)
                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:818)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
                                                 at dalvik.system.NativeStart.main(Native Method)

我的错误在哪里请帮助我 感谢

1 个答案:

答案 0 :(得分:1)

您没有设置适配器中创建的上下文对象。

您需要通过将活动上下文与传递给适配器的联系人一起传递来进行设置

public RetrofitItemAdapter(List<Contact> mContact, Context context) {
   this.mContact = mContact;
   this.mContext = context;
}

活动

  adapter = new RetrofitItemAdapter(contacts, RetrofitDemo.this);
  listView.setAdapter(adapter);