麻烦老师看下为什么没法显示ListView
Activity中:
package com.example.felix.homeworkapp; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.widget.ListView; import android.widget.Toast; import com.example.felix.homeworkapp.Adapter.GridAdapter1; import com.example.felix.homeworkapp.Adapter.MenuAdapter1; import com.example.felix.homeworkapp.Info.MenuInfo; import com.example.felix.homeworkapp.Info.MenuResult; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class MenuActivity extends AppCompatActivity { private ListView menuListView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); menuListView = findViewById(R.id.mainFragment_listView); //LayoutInflater layoutInflater= (LayoutInflater) getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE); } public class MenuAsyncTask extends AsyncTask<Void,Integer, String>{ @Override protected void onPreExecute() { Toast.makeText(MenuActivity.this,"开始准备获取数据",Toast.LENGTH_LONG).show(); super.onPreExecute(); } @Override protected String doInBackground(Void... voids) { return request("http://www.imooc.com/api/shopping?type=11"); } private String request(String s) { try { URL url=new URL(s); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(30*1000); connection.setRequestMethod("GET"); connection.connect(); int reponseCode=connection.getResponseCode(); String reponseMessage=connection.getResponseMessage(); if(reponseCode==HttpURLConnection.HTTP_OK){ InputStream inputStream=connection.getInputStream(); Bitmap bitmap= BitmapFactory.decodeStream(inputStream); }else{ Toast.makeText(MenuActivity.this,"ERROR",Toast.LENGTH_LONG).show(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(String s) { MenuResult menuResult=new MenuResult(); try { JSONObject jsonObject=new JSONObject(); JSONArray dataArray=jsonObject.getJSONArray("data"); int status=jsonObject.getInt("status"); menuResult.setmStatus(status); List<MenuInfo> menuInfoList=new ArrayList<>(); for(int i=0;i<dataArray.length();i++){ MenuInfo menuInfo=new MenuInfo(); JSONObject js= (JSONObject) dataArray.get(i); menuInfo.setId(js.getInt("id")); menuInfo.setName(js.getInt("name")); menuInfo.setImg(js.getString("img")); menuInfo.setAction(js.getInt("action")); menuInfo.setCount(js.getInt("count")); menuInfo.setPrice(js.getInt("price")); menuInfoList.add(menuInfo); } menuResult.setmDataInfoList(menuInfoList); } catch (JSONException e) { e.printStackTrace(); } MenuAdapter1 menuAdapter1=new MenuAdapter1(menuResult.getmDataInfoList(),MenuActivity.this); menuListView.setAdapter(menuAdapter1); super.onPostExecute(s); } } }
Adapter中:
package com.example.felix.homeworkapp.Adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.felix.homeworkapp.Info.MenuInfo; import com.example.felix.homeworkapp.R; import java.util.ArrayList; import java.util.List; public class MenuAdapter1 extends BaseAdapter { List<MenuInfo> menuInfoList=new ArrayList<>(); private Context context; public MenuAdapter1(List<MenuInfo> menuInfoList, Context context) { this.menuInfoList = menuInfoList; this.context = context; } @Override public int getCount() { return menuInfoList.size(); } @Override public Object getItem(int i) { return menuInfoList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder=new ViewHolder(); if(view==null){ view=View.inflate(context, R.layout.item_fragment_main,null); viewHolder.img_dish=view.findViewById(R.id.item_fragment_main_imageView); viewHolder.tv_dish_name=view.findViewById(R.id.item_fragment_main_textView); view.setTag(viewHolder); }else { view= (View) view.getTag(); } MenuInfo menuInfo=menuInfoList.get(i); //viewHolder.img_dish.setImageBitmap(menuInfo.getImg()); viewHolder.tv_dish_name.setText(menuInfo.getName()); return view; } public class ViewHolder { ImageView img_dish; TextView tv_dish_name; //TextView tv_dish_name; } }
以及顺便请老师看下
viewHolder.img_dish.setImageBitmap(menuInfo.getImg());
get方法中是String这里应该怎么进行格式转换,谢谢老师
0
收起
正在回答
3回答
数据获取有如下问题:
MenuAsyncTask类中,doInBackground()方法返回的值将被当做onPostExecute()方法的参数传递进去,你的代码中doInBackground()方法返回的request()方法的返回值,而request没有正确返回请求的数据。
1)如果获取的数据,你参考如下使用循环读取服务器数据并返回
String str = null; InputStream in = null; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(3*1000); conn.connect(); if (conn.getResponseCode()==HttpURLConnection.HTTP_OK){ in = conn.getInputStream(); int len = -1; byte[]by = new byte[4*1024]; StringBuffer buffer = new StringBuffer(); while((len=in.read(by))!=-1){ buffer.append(new String(by,0,len)); } str = new String(buffer); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if (in!=null){ in.close(); } } catch (IOException e) { e.printStackTrace(); } } return str;
Android网络操作与数据存储2018版
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星