Custom Adapter:

public class MyAdapter extends BaseAdapter { private Context context; private List<Joker> list; private LayoutInflater inflater = null; public MyAdapter(Context context, List<Joker> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if(convertView==null){ holder=new ViewHolder(); convertView=inflater.inflate(R.layout.joker_item,null); holder.tv_title= (TextView) convertView.findViewById(R.id.tv_title); holder.tv_content= (TextView) convertView.findViewById(R.id.tv_content); holder.tv_url= (TextView) convertView.findViewById(R.id.tv_url); holder.tv_content.setMovementMethod(ScrollingMovementMethod.getInstance()); convertView.setTag(holder); }else { holder= (ViewHolder) convertView.getTag(); } Joker joker= (Joker) getItem(position); holder.tv_title.setText(joker.getTitle().toString()); holder.tv_content.setText(joker.getContent().toString()+joker.getPoster()); // CharSequence cs= Html.escapeHtml(joker.getContent().toString()); // holder.tv_content.setText(cs); // holder.tv_content.setMovementMethod(LinkMovementMethod.getInstance()); // When clicked, the hyperlink holder.tvurl.settext (joker.geturl ().tostring ()) is generated; holder.tv_url.setMovementMethod(LinkMovementMethod.getInstance()); return convertView; } class ViewHolder { private TextView tv_title,tv_content,tv_url; }}Copy the code

MainAcvitity:

public class MainActivity extends AppCompatActivity { private ListView lv_joker; private String url="http://api.laifudao.com/open/xiaohua.json"; private MyThread thread=null; private MyHandle handle=null; private List<Joker> list=new ArrayList<>(); private String result=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handle=new MyHandle(); lv_joker= (ListView) findViewById(R.id.lv_joker); If (thread==null){thread=new MyThread(); thread.start(); }} class MyThread extends Thread{Override public void run() {super.run();  Result =loadingGet(); Message message=handle.obtainMessage(1,result); handle.sendMessage(message); }} /** * ③ Private String loadingGet(){try {URL newUrl=new URL(URL); HttpURLConnection conn= (HttpURLConnection) newUrl.openConnection(); conn.setRequestMethod("GET"); int code=conn.getResponseCode(); if(code==200){ InputStream is=conn.getInputStream(); InputStreamReader inreader=new InputStreamReader(is); String line=""; BufferedReader reader=new BufferedReader(inreader); StringBuffer buffer=new StringBuffer(); while ((line=reader.readLine())! =null){ buffer.append(line); } reader.close(); inreader.close(); is.close(); conn.disconnect(); return buffer.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "access failed "; } /** * ⑤ Extends Handler{private JSONArray array=null; @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 1: try { array=new JSONArray(result); for (int i = 0; array! =null&&i <array.length() ; i++) { JSONObject obj=array.getJSONObject(i); Joker joker=new Joker(); joker.setTitle(obj.getString("title")); joker.setContent(obj.getString("content")+obj.getString("poster")); joker.setUrl(obj.getString("url")); list.add(joker); } MyAdapter adapter=new MyAdapter(MainActivity.this,list); lv_joker.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } break; }}}Copy the code