package com.bwie.adapters;
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.bwie.beans.News;
import com.bwie.xlistviewdemo1.R;
import java.util.ArrayList;
/**
* Created by Administrator on 2017/9/7.
*/
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private ArrayList<News> news;
public MyBaseAdapter(ArrayList<News> news, Context context) {
this.news = news;
this.context = context;
}
@Override
public int getCount() {
return news.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//多条目的数量
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position%2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 holder1;
ViewHolder2 holder2;
int rs = getItemViewType(position);//获得当前条目使用的是第几种条目
News newsObj = news.get(position);//得到要展示的数据
if(rs == 0){//第一种条目
if (convertView == null){
holder1 = new ViewHolder1();
convertView = View.inflate(context, R.layout.lsitview_item_1,null);
holder1.textView = (TextView) convertView.findViewById(R.id.mytitle);
convertView.setTag(holder1);
}else{
holder1 = (ViewHolder1) convertView.getTag();
}
//赋值
holder1.textView.setText(newsObj.getTitle());
}else if(rs == 1){//第二种条目
if (convertView == null){
holder2 = new ViewHolder2();
convertView = View.inflate(context, R.layout.listview_item_2,null);
holder2.textView = (TextView) convertView.findViewById(R.id.mytitle);
holder2.imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder2);
}else{
holder2 = (ViewHolder2) convertView.getTag();
}
//赋值
holder2.textView.setText(newsObj.getTitle());
//通过imageloader加载图片,显示在次行
// ImageLoader.getInstance().displayImage(newsObj.getPicUrl(),holder2.imageView);
}
return convertView;
}
class ViewHolder1{
TextView textView;
}
class ViewHolder2{
TextView textView;
ImageView imageView;
}
}
//请求网络的工具类
package com.bwie.xlistviewdemo1;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Administrator on 2017/9/7.
*/
public class NetWorkUtils {
public String getJsonByUrlConnection(String jsonUrl){
String str = "";
try {
URL url = new URL(jsonUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int resCode = conn.getResponseCode();
if(resCode == 200){
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len=is.read(b)) != -1){
str += new String(b,0,len);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
}