使用Spinner和AsyncTask 下载数据绑定到Spinner
2016-01-16 00:00
387 查看
功能:一个Spinner 从网络上下载数据 然后绑定到Spinner
//记得配置联网权限
1、在布局界面布局 activity_main.xml
代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/bt_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载数据"
android:onClick="downLoad"
/>
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
/>
</LinearLayout>
----------------------------------------
2、MainActivity 类
代码
public class MainActivity extends Activity {
private Spinner sp;
private ArrayAdapter<String> adapter;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.sp = (Spinner) this.findViewById(R.id.spinner);
this.pb = (ProgressBar) this.findViewById(R.id.pb);
}
//按钮 事件监听
public void downLoad(View view){
String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";
new MyAsyncTask().execute(url);
}
class MyAsyncTask extends AsyncTask<String, Integer, byte[]>{
// private ProgressDialog dialog;
// public MyAsyncTask(Context context) {
// dialog = new ProgressDialog(context);
// dialog.setTitle("温馨提示");
// dialog.setMessage("数据下载中,请稍等...");
// dialog.setIcon(R.drawable.ic_launcher);
// }
@Override
protected void onPreExecute() {
super.onPreExecute();
//dialog.show();
//当按下按钮后 就启动工具类 然后显示进度条
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//更新进度条
pb.setProgress(values[0]);
}
@Override
protected byte[] doInBackground(String... params) {
String url = params[0];
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
long totalLength = response.getEntity().getContentLength();
byte[] b = new byte[1024];
int count = 0;
int len = 0;
if(response.getStatusLine().getStatusCode() == 200){
BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
count+=len;
int l = (int)((count/(double)totalLength)*100);
publishProgress(l);
}
bis.close();
return bos.toByteArray();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] result) {
if(result != null){
//下载完数据 就使用json解析 下载的数据
List<String> list = jsonMethod(new String(result,0,result.length));
//把解析好的数据丢到ArrayAdapter适配器里
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list);
//把适配器绑定到Spinner里
sp.setAdapter(adapter);
}else{
Toast.makeText(MainActivity.this, "数据下载失败!", Toast.LENGTH_SHORT).show();
}
//dialog.dismiss();
pb.setVisibility(View.GONE);
}
}
public List<String> jsonMethod(String string) {
List<String> list = new ArrayList<String>();
try {
JSONObject object = new JSONObject(string);
JSONObject object_paramz = object.getJSONObject("paramz");
JSONArray object_feeds = object_paramz.getJSONArray("feeds");
for(int i = 0;i<object_feeds.length();i++){
JSONObject object_data = object_feeds.getJSONObject(i).getJSONObject("data");
String data = object_data.getString("subject");
list.add(data);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
//记得配置联网权限
1、在布局界面布局 activity_main.xml
代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/bt_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载数据"
android:onClick="downLoad"
/>
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
/>
</LinearLayout>
----------------------------------------
2、MainActivity 类
代码
public class MainActivity extends Activity {
private Spinner sp;
private ArrayAdapter<String> adapter;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.sp = (Spinner) this.findViewById(R.id.spinner);
this.pb = (ProgressBar) this.findViewById(R.id.pb);
}
//按钮 事件监听
public void downLoad(View view){
String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";
new MyAsyncTask().execute(url);
}
class MyAsyncTask extends AsyncTask<String, Integer, byte[]>{
// private ProgressDialog dialog;
// public MyAsyncTask(Context context) {
// dialog = new ProgressDialog(context);
// dialog.setTitle("温馨提示");
// dialog.setMessage("数据下载中,请稍等...");
// dialog.setIcon(R.drawable.ic_launcher);
// }
@Override
protected void onPreExecute() {
super.onPreExecute();
//dialog.show();
//当按下按钮后 就启动工具类 然后显示进度条
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//更新进度条
pb.setProgress(values[0]);
}
@Override
protected byte[] doInBackground(String... params) {
String url = params[0];
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(get);
long totalLength = response.getEntity().getContentLength();
byte[] b = new byte[1024];
int count = 0;
int len = 0;
if(response.getStatusLine().getStatusCode() == 200){
BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
count+=len;
int l = (int)((count/(double)totalLength)*100);
publishProgress(l);
}
bis.close();
return bos.toByteArray();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] result) {
if(result != null){
//下载完数据 就使用json解析 下载的数据
List<String> list = jsonMethod(new String(result,0,result.length));
//把解析好的数据丢到ArrayAdapter适配器里
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list);
//把适配器绑定到Spinner里
sp.setAdapter(adapter);
}else{
Toast.makeText(MainActivity.this, "数据下载失败!", Toast.LENGTH_SHORT).show();
}
//dialog.dismiss();
pb.setVisibility(View.GONE);
}
}
public List<String> jsonMethod(String string) {
List<String> list = new ArrayList<String>();
try {
JSONObject object = new JSONObject(string);
JSONObject object_paramz = object.getJSONObject("paramz");
JSONArray object_feeds = object_paramz.getJSONArray("feeds");
for(int i = 0;i<object_feeds.length();i++){
JSONObject object_data = object_feeds.getJSONObject(i).getJSONObject("data");
String data = object_data.getString("subject");
list.add(data);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
相关文章推荐
- 使用AutoCompleteTextView和AsyncTask 检索城市
- ListView 的简单使用day9
- ListView和SimPleteAdapter 把新闻数据绑定到ListView
- BaseAdapter 简单使用day9.1
- ListView和BaseAdapter 把新闻数据添加到ListView
- ListView 加载下一页的简单功能
- 从网上绑定新闻数据到ListView
- ListView和CheckBox 的简单使用
- mysql排序
- 0001 -- 搭建bootstrap环境。
- 0002 -- bootstrap 图标的使用。
- 0003 -- bootstrap 按钮的使用。
- Linux学习笔记
- STL编程题1(C++程序设计第1周)
- 【HTML打卡】0115 margin重叠、内联元素、css控制段落
- [caffe]深度学习之图像分类模型VGG解读
- 构建灵活的界面
- [乡土民间故事_徐苟三传奇]第廿五回_请课酒计耍刘财主
- Python Numpy Tutorial
- [Swift开发实战]-类的使用01