问题描述
- 进入页面虚拟机崩溃,找不到错误,求大神帮改下源码,谢谢啦
- import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;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.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;public class StartCanteenActivity extends Activity
{
private static final String urlPath = ""http://www.imooc.com/api/teacher?type=4&num=30"";
//private static final String urlPath = ""http://14.117.17.161/json.php"";
private RecyclerView mRvDemoMain;
private MyRecyclerAdapter mAdapter;
private List mList;@Overrideprotected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.canteen); mRvDemoMain = (RecyclerView) findViewById(R.id.canteen_recyclerview); new NewAsyncTask().execute(urlPath); initView();}/** * 初始化本页面的控件 */private void initView() { mRvDemoMain.setAdapter(mAdapter); // 设置RecyclerView的布局管理器 mRvDemoMain.setLayoutManager(new StaggeredGridLayoutManager(3 StaggeredGridLayoutManager.VERTICAL)); mRvDemoMain.setItemAnimator(new DefaultItemAnimator()); // 调用Adapter中的设置监听器的方法 mAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() { @Override public void onItemClick(View view int position) { Toast.makeText(StartCanteenActivity.thisclick : "" + position Toast.LENGTH_SHORT).show(); } });}/** * 实现网络的异步访问 */class NewAsyncTask extends AsyncTask<String Void List<NewsBeam>>{ @Override protected void onPostExecute(List<NewsBeam> newsBeams) { super.onPostExecute(newsBeams); MyRecyclerAdapter adapter = new MyRecyclerAdapter(StartCanteenActivity.this newsBeams mRvDemoMain); mRvDemoMain.setAdapter(adapter); } @Override protected List<NewsBeam> doInBackground(String... params) { return getJsonData(params[0]); } /** * 从 URL 中获取数据 * * @param url * @return */ private List<NewsBeam> getJsonData(String url) { List<NewsBeam> newsBeamsList = new ArrayList<NewsBeam>(); try { String jsonString = readStream(new URL(urlPath).openStream()); JSONObject jsonObject; NewsBeam newsBeam; jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray(""data""); for(int i = 0; i < jsonArray.length(); i++) { jsonObject = jsonArray.getJSONObject(i); newsBeam = new NewsBeam(); newsBeam.newsIconUrl = jsonObject.getString(""picSmall""); newsBeam.newsTitle = jsonObject.getString(""name""); newsBeamsList.add(newsBeam); } } catch(IOException e) { e.printStackTrace(); } catch(JSONException e) { e.printStackTrace(); } return newsBeamsList; } /** * 从 inpustStream 获取的信息 * * @param is * @return */ private String readStream(InputStream is) { InputStreamReader isr; String result = """"; try { isr = new InputStreamReader(isutf-8""); BufferedReader br = new BufferedReader(isr); String line = """"; while((line = br.readLine()) != null) { result += line; } } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return result; }}
}
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class MyRecyclerAdapter extends RecyclerView.Adapter {
protected LayoutInflater mLayoutInflater;
private Context mContext;
protected List mList;
private List mHeights; // 随机高度的集合
private ImgLoader mImgLoader;
private int mStart;
private int mEnd;
public static String[] URLS;
private boolean mFirstIn;
private MyRecyclerAdapter(List mList) {
this.mList = mList;
}/** * 自定义一个OnItemClickListener的回调接口 */public interface OnItemClickListener{ void onItemClick(View view int position);}/** * 创建接口的成员变量 */private OnItemClickListener mOnItemClickListener;/** * 设置监听器的方法 * @param listener listener */public void setOnItemClickListener(OnItemClickListener listener){ this.mOnItemClickListener = listener;}/** * 构造方法 * @param context context * @param data data */public MyRecyclerAdapter(Context context List<NewsBeam> dataRecyclerView recyclerView) { this.mContext = context; this.mList = data; mLayoutInflater = LayoutInflater.from(mContext); mImgLoader = new ImgLoader(recyclerView); mLayoutInflater = LayoutInflater.from(mContext); mHeights = new ArrayList<Integer>(); // 向随机高度集合中添加随机数(100 ~ 400) for (int i = 0; i < mList.size(); i++) { mHeights.add((int) (100 + Math.random() * 300)); } URLS = new String[data.size()]; for(int i = 0; i < data.size(); i++) { URLS[i] = data.get(i).newsIconUrl; } mFirstIn = true;}/** * RecyclerView中条目的数量 * * @return count */@Overridepublic int getItemCount() { return mList.size();}/** * 创建ViewHolder * * @param parent parent * @param viewType viewType * @return holder */@Overridepublic MyRecyclerHolder onCreateViewHolder(ViewGroup parent int viewType) { // zz 测试 System.out.println(""====onCreateViewHolder : "" + viewType + "" == "" + parent); View view = mLayoutInflater.inflate(R.layout.recycler_item parent false); MyRecyclerHolder mHolder = new MyRecyclerHolder(view); return mHolder;}/** * 绑定ViewHolder * * @param holder holder * @param position position */@Overridepublic void onBindViewHolder(final MyRecyclerHolder holder final int position) { // 为ViewHolder的父控件(也即一条Item)设置高度 ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); lp.height = mHeights.get(position); holder.itemView.setLayoutParams(lp); holder.mIvItem.setImageBitmap(null); holder.mIvItem.setImageResource(R.mipmap.ic_launcher); String url = mList.get(position).newsIconUrl; holder.mIvItem.setTag(url); mImgLoader.showImgByAysncTask(holder.mIvItem url); holder.mTvItem.setText(mList.get(position).newsTitle); // 调用条目的点击事件设置方法 setUpItemEvent(holder); // zz 测试 System.out.println(""====onBindViewHolder : "" + position + "" == "" + holder);}/** * 设置条目的点击事件(点按) * @param holder holder */protected void setUpItemEvent(final MyRecyclerHolder holder) { if (mOnItemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int layoutPosition = holder.getLayoutPosition(); mOnItemClickListener.onItemClick(holder.itemView layoutPosition); } }); }}/** * RecyclerView的ViewHolder类 */public class MyRecyclerHolder extends RecyclerView.ViewHolder { ImageView mIvItem; TextView mTvItem; public MyRecyclerHolder(View itemView) { super(itemView); mIvItem = (ImageView) itemView.findViewById(R.id.id_image); mTvItem = (TextView) itemView.findViewById(R.id.id_name); }}
}
解决方案
代码太多,建议自己调试