import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView mylist;
private RelativeLayout activity_main;
List<DataBean.ResultBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置初始化;
ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(MainActivity.this);
ImageLoader.getInstance().init(configuration);
initView();
String url = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0";
MyTask task = new MyTask();
task.execute(url);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(MainActivity.this,DetailActivity.class);
DataBean.ResultBean bean=list.get(position);//(DataBean.ResultBean)parent.getAdapter().getItem(position);
intent.putExtra("img",bean.IMAGEURL);
intent.putExtra("title",bean.TITLE);
intent.putExtra("subtitle",bean.SUBTITLE);
intent.putExtra("time",bean.SHOWTIME);
intent.putExtra("name",bean.FROMNAME);
startActivity(intent);
}
});
}
class MyTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
//从网络获取数据
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
if (code == 200) {
InputStream inputStream = connection.getInputStream();
String str;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
while ((str = reader.readLine()) != null) {
builder.append(str);
}
return builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//获取请求的数据
if (s != null) {
Gson gson = new Gson();
DataBean bean = gson.fromJson(s, DataBean.class);
if (bean != null) {
list = bean.data;
if(list!=null){
MyAdapter adapter=new MyAdapter(MainActivity.this,list);
mylist.setAdapter(adapter);}
}
}
}
}
private void initView() {
mylist = (ListView) findViewById(R.id.mylist);
activity_main = (RelativeLayout) findViewById(R.id.activity_main);
}
}
MyAdapter
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.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private List<DataBean.ResultBean> list = new ArrayList<DataBean.ResultBean>();
private Context context;
private ImageLoader loader;
public MyAdapter(Context context, List<DataBean.ResultBean> list) {
this.context = context;
this.list = list;
loader=ImageLoader.getInstance();
}
@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) {
convertView = View.inflate(context, R.layout.item, null);
holder=new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder=(ViewHolder)convertView.getTag();
}
DataBean.ResultBean resultBean=list.get(position);
//设置图片
loader.displayImage(resultBean.IMAGEURL,holder.item_img);
//设置文本
holder.item_name.setText(resultBean.FROMNAME);
holder.item_title.setText(resultBean.TITLE);
holder.item_time.setText(resultBean.SHOWTIME);
return convertView;
}
public static class ViewHolder {
public View rootView;
public ImageView item_img;
public TextView item_title;
public TextView item_name;
public TextView item_time;
public ViewHolder(View rootView) {
this.rootView = rootView;
this.item_img = (ImageView) rootView.findViewById(R.id.item_img);
this.item_title = (TextView) rootView.findViewById(R.id.item_title);
this.item_name = (TextView) rootView.findViewById(R.id.item_name);
this.item_time = (TextView) rootView.findViewById(R.id.item_time);
}
}
}
传值过来的界面
package a16.erin.week_2demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
/**
* Created by mamiaomiao on 17/8/10.
*/
public class DetailActivity extends Activity {
private ImageView detail_img;
private TextView detail_title;
private TextView detail_subtitle;
private TextView detail_name;
private TextView detail_time;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.detail_layout);
initView();
Intent intent=getIntent();
String url=intent.getStringExtra("img");
String title=intent.getStringExtra("title");
String subtitle=intent.getStringExtra("subtitle");
String time=intent.getStringExtra("time");
String name=intent.getStringExtra("name");
ImageLoader.getInstance().displayImage(url,detail_img);
detail_name.setText(name);
detail_title.setText(title);
detail_subtitle.setText(subtitle);
detail_time.setText(time);
}
private void initView() {
detail_img = (ImageView) findViewById(R.id.detail_img);
detail_title = (TextView) findViewById(R.id.detail_title);
detail_subtitle = (TextView) findViewById(R.id.detail_subtitle);
detail_name = (TextView) findViewById(R.id.detail_name);
detail_time = (TextView) findViewById(R.id.detail_time);
}
}
布局文件
主方法
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#999"
android:dividerHeight="2dp"/>
MyAdapter Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_img"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
<TextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
<TextView
android:id="@+id/item_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
</LinearLayout>
</LinearLayout>
传值过去的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/detail_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:scaleType="fitXY"
/>
<TextView
android:id="@+id/detail_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
<TextView
android:id="@+id/detail_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
<TextView
android:id="@+id/detail_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
<TextView
android:id="@+id/detail_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
/>
</LinearLayout>