就最后一个图片一直在切换不同的图片
相关代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.zuoye12.util.Msg;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends BaseAdapter {
List<Msg> mLessonInfos;
private Context mContext;
Bitmap bitmapc=null;
ViewHolder viewHolder;
public MyAdapter(Context context,List<Msg> infos) {
mLessonInfos=infos;
mContext=context;
}
@Override
public int getCount() {
return mLessonInfos.size();
}
@Override
public Object getItem(int position) {
return mLessonInfos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// viewHolder=new ViewHolder();
if(convertView==null){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
viewHolder.name = convertView.findViewById(R.id.name);
viewHolder.img=convertView.findViewById(R.id.img);
viewHolder.count = convertView.findViewById(R.id.count);
viewHolder.price = convertView.findViewById(R.id.price);
viewHolder.action = convertView.findViewById(R.id.description);
viewHolder.action = convertView.findViewById(R.id.action);
convertView.setTag(viewHolder);
}else{
viewHolder= (ViewHolder) convertView.getTag();
}
String url=mLessonInfos.get(position).getImageView();
RequestDataAsyncTaskSecond taskSecond;
new RequestDataAsyncTaskSecond().execute(url);
viewHolder.name.setText(mLessonInfos.get(position).getName());
// viewHolder.context.setText(mLessonInfos.get(position).getDescription());
viewHolder.price.setText(mLessonInfos.get(position).getPrice());
viewHolder.price.setText(mLessonInfos.get(position).getAction());
viewHolder.action.setText(mLessonInfos.get(position).getCount());
return convertView;
}
public class ViewHolder{
public ImageView img;
public TextView name;
public TextView price;
public TextView context;
public TextView count;
public TextView action;
}
public class RequestDataAsyncTaskSecond extends AsyncTask<String,Void,Bitmap> {
String str=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//
}
protected Bitmap doInBackground(String... path) {
str=path[0];
try {
URL url = new URL(str);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream inputStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Bitmap bitmap) {
viewHolder.img.setImageBitmap(bitmap);
notifyDataSetChanged();
}
}
}
相关截图:
问题描述:
就最后一个图片一直在切换不同的图片
8
收起
正在回答 回答被采纳积分+1
2.Android 网络操作与流行框架
- 参与学习 人
- 提交作业 220 份
- 解答问题 1800 个
本阶段是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理;从文件存储、到轻量级数据库SQLite的使用;最后利用最流程的第三方框架助你快速开发企业级APP。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星