Populating a List view from an async call with okhttp

问题: With android studio, I am trying to create a fragment on which I have a list of item displayed, item that I am getting from an Http call using okhttp. The problem is tha...

问题:

With android studio, I am trying to create a fragment on which I have a list of item displayed, item that I am getting from an Http call using okhttp.

The problem is that my adapter doesnt seems to work. I do not have any error or whatever, but my list is not displayed. By itself, the Http call is working, I get a list of car brand that are mapped to a list of type Make with gson.

Here is what I have so far:

public class MakeFragment extends Fragment {

private List<Make> allMake = new ArrayList<>();
ListView listView;
MakeAdapter adapter;

private OnFragmentInteractionListener mListener;

public MakeFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View root = inflater.inflate(R.layout.fragment_make, container, false);

    listView = (ListView) root.findViewById(R.id.make_list);
    adapter = new MakeAdapter(allMake,getContext());
    listView.setAdapter(adapter);

    new MyTask().execute();

    return root;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}


public class MyTask extends AsyncTask<Void,Void,List<Make>> {

    @Override
    protected List<Make> doInBackground(Void... voids) {
        ImaApi api = new ImaApiImpl();
        return api.getAllMake();
    }

    @Override
    protected void onPostExecute(List<Make> result) {
        super.onPostExecute(result);
        allMake.addAll(result);
        adapter.notifyDataSetChanged();
    }

}}

My adapter:

public class MakeAdapter extends BaseAdapter{

    List<Make> list = new ArrayList<>();
    Context context;

    public MakeAdapter(List<Make> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void setList(List<Make> list ){
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        ViewHolder viewHolder;

        if (convertView == null){

            View view = LayoutInflater.from(context).inflate(R.layout.make_list_item,parent,false);

            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Make make = list.get(position);

        viewHolder.textView.setText(make.name);

        return viewHolder.itemView;
    }

    public static class ViewHolder{

        TextView textView;
        View itemView;

        public ViewHolder(View itemView) {
            this.itemView = itemView;
            textView = itemView.findViewById(R.id.text_view);
        }

    }
}

My make_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginTop="10dp">
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:textSize="20dp"
        android:maxLength="5"
        android:text=""/>

</android.support.v7.widget.CardView>

And finally my MakeFragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MakeFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dasfaddsa" />

    <ListView
        android:id="@+id/make_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible" />
</FrameLayout>

I've been trying to reproduce this tutorial. When I use the debugger, the adapter getCount is showing me that I have 43 objects, as I should and the method getView is never called.

EDIT: Im adding the getAllMake() method to help, but it is working as it should.

@Override
public List<Make> getAllMake() {
    Request request = new Request.Builder()
            .url(baseUrl + "/api/v1/brand/")
            .build();
    Response response = null;
    String responseBody = "";
    try {
        response = client.newCall(request).execute();
        responseBody = response.body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }

    MakeWrapper makeWrapper = gson.fromJson(responseBody, MakeWrapper.class);
    return makeWrapper.content;
}

I hope someone can give me a hand with this. Thank you.


回答1:

Try this,

public class MakeFragment extends Fragment {

private List<Make> allMake = new ArrayList<>();
ListView listView;
MakeAdapter adapter;

private OnFragmentInteractionListener mListener;

public MakeFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View root = inflater.inflate(R.layout.fragment_make, container, false);

    listView = (ListView) root.findViewById(R.id.make_list);
    adapter = new MakeAdapter(allMake,getContext());
    listView.setAdapter(adapter);

    new MyTask().execute();

    return root;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}


public class MyTask extends AsyncTask<Void,Void,List<Make>> {

    @Override
    protected List<Make> doInBackground(Void... voids) {
        ImaApi api = new ImaApiImpl();
        return api.getAllMake();
    }

    @Override
    protected void onPostExecute(List<Make> result) {
        super.onPostExecute(result);
        allMake.addAll(result);
        adapter.notifyDataSetChanged();
        }

    }}
  • 发表于 2019-03-10 10:17
  • 阅读 ( 198 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除