OkHTTP AndroidBaseAdapter 从数据到 ListView
OkHTTP AndroidBaseAdapter to ListView from data
我已经设置 okhttp 从我的服务器下载数据并以普通文本视图显示。
但我很想把它放到 ListView 中。
我知道我需要基本适配器和一些项目布局,我需要有关如何让它与 OKHTTP 一起工作的帮助。
这是我用来下载它的代码,如果您还需要什么,请评论。
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);
}
}
}
我对 android 有点陌生,使用的是 Android Studio。
任何人都可以用正常的方式向我解释或显示代码片段吗?
您需要制作自己的适配器。我更喜欢从 BaseAdapter 扩展并创建自己的列表项 class:
class ListItem {
private long mId;
private String mContent;
public ListItem(final long mId, final String mContent) {
this.mId = mId;
this.mContent = mContent;
}
public long getId() {
return mId;
}
public String getContent() {
return mContent;
}
}
public class adapter_akt extends BaseAdapter {
private List<ListItem> mData = new ArrayList<>();
public void setData(List<ListItem> data){
mData = data;
notifyDataSetChanged();
}
public ListItem getItem(int position){
return mData.get(position);
}
@Override
public long getItemId(final int position) {
return getItem(position).getId();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent);
ViewHolder h = new ViewHolder();
h.textView1 = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(h);
}
ViewHolder holder = (ViewHolder)convertView.getTag();
ListItem item = getItem(position);
holder.textView1.setText(item.getContent());
return convertView;
}
public int getCount(){
return mData.size();
}
private class ViewHolder{
TextView textView1;
}
}
那么您只需要将适配器设置为您的 ListView。
您的 AsyncTask 看起来像:
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);
}
}
我已经设置 okhttp 从我的服务器下载数据并以普通文本视图显示。 但我很想把它放到 ListView 中。
我知道我需要基本适配器和一些项目布局,我需要有关如何让它与 OKHTTP 一起工作的帮助。
这是我用来下载它的代码,如果您还需要什么,请评论。
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);
}
}
}
我对 android 有点陌生,使用的是 Android Studio。
任何人都可以用正常的方式向我解释或显示代码片段吗?
您需要制作自己的适配器。我更喜欢从 BaseAdapter 扩展并创建自己的列表项 class:
class ListItem {
private long mId;
private String mContent;
public ListItem(final long mId, final String mContent) {
this.mId = mId;
this.mContent = mContent;
}
public long getId() {
return mId;
}
public String getContent() {
return mContent;
}
}
public class adapter_akt extends BaseAdapter {
private List<ListItem> mData = new ArrayList<>();
public void setData(List<ListItem> data){
mData = data;
notifyDataSetChanged();
}
public ListItem getItem(int position){
return mData.get(position);
}
@Override
public long getItemId(final int position) {
return getItem(position).getId();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent);
ViewHolder h = new ViewHolder();
h.textView1 = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(h);
}
ViewHolder holder = (ViewHolder)convertView.getTag();
ListItem item = getItem(position);
holder.textView1.setText(item.getContent());
return convertView;
}
public int getCount(){
return mData.size();
}
private class ViewHolder{
TextView textView1;
}
}
那么您只需要将适配器设置为您的 ListView。
您的 AsyncTask 看起来像:
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);
}
}