Aksebkit Aksebkit - 4 months ago 84
Android Question

OkHTTP AndroidBaseAdapter to ListView from data

I've set up okhttp to download data from my server and display it in normal text view.
But I would love to get it to ListView.

I know I need to have base adapter, some item layout and I need help with how to get it working with OKHTTP.

This is code that I use to download it, if you need anything else, please comment.

public class TaskAktualnosci extends AsyncTask<String, Void, String> {

protected String doInBackground(String... params) {
try {
//*OKHTTP ponoć parametry dodaje do buildera*
// RequestBody parametry = new FormBody.Builder()
// .add("offset", "0")
// .add("limit", "50")
// .build();

OkHttpClient klient = new OkHttpClient();
final Gson gson = new Gson();


Request request = new Request.Builder()
.url("http://www.apirest.poligon.webimpuls.pl/v1/restapi/aktualnosci?offset=0?limit=50")
// .post(parametry)
.build();



Response response = klient.newCall(request).execute();
return response.body().string();



}catch (Exception e){



return null;
}



}


@Override
protected void onPostExecute(String s){
super.onPostExecute(s);

TextView textView = (TextView) findViewById(R.id.ciastko);
textView.setText(s);
}


}

}


I'm kinda new to android as whole and using Android Studio.

Can anyone explain it to me in normal way or show code snippet?

Answer

You need to make your own adapter. I prefer to extend from BaseAdapter and create a own list item clas: class ListItem { long mId; String mContent;

    ListItem(long id, String content){
        mId = id;
        mContent = content;
    }

    public String getContent(){
        return mContent;
    }

}

and yout adapter can look like:

   class MyListAdapter extends BaseAdapter{
       private List<ListItem> mData = new ArrayList<>();

       public void setData(List<ListItem> data){
        mData = data;
        notifyDatasetChanged();
       }

       ListItem getItem(int pos){
           mData.get(pos);
       }

       int getCount(){
        return mData.size();
       }


       View getItem (LayoutInflater inflater, int pos, View v){
          if (v == null){
              v = inflater.inflate(android.R.layout.simple_list_item_1);
              ViewHolder h = new ViewHolder();
              h.textView1 = v.findViewById(android.R.id.text1);
              v.setTag(h);
          }

          ViewHolder holder = (ViewHolder)v.getTag();         
          ListItem item = getItem(pos);
          holder.textView1.setText(item.getContent());

       }

    private class ViewHolder{
              TextView textView1;
    }
   }

then all u need is something to set your adapter to your ListView.

Your AsyncTask would look like:

public class Test1 extends AsyncTask<String, Void, String> {

protected String doInBackground(String... params) {
    try {
        //*OKHTTP ponoć parametry dodaje do buildera*
   //     RequestBody parametry = new FormBody.Builder()
    //            .add("offset", "0")
     //           .add("limit", "50")
      //          .build();

        OkHttpClient klient = new OkHttpClient();
         final Gson gson = new Gson();


       Request request = new Request.Builder()
                .url("http://www.apirest.poligon.webimpuls.pl/v1/restapi/aktualnosci?offset=0?limit=50")
               // .post(parametry)
               .build();



        Response response = klient.newCall(request).execute();
        return response.body().string();



    }catch (Exception e){



        return null;
    }



}


@Override
protected  void onPostExecute(String s){
    super.onPostExecute(s);

    List<ListItem> contentList = new ArrayList<>();
    /*
     her u need to read data from s to the list; 
     */

    MyListAdapter adapter = find();//here u need to get your Adapter, maybe its member of your fragment class
    adapter.setData(contentList);
}


}
Comments