|
1 | 1 | package edu.hzuapps.androidlabs.homeworks.net1414080903231;
|
2 | 2 |
|
3 |
| -import android.content.Intent; |
4 |
| -import android.support.v7.app.AppCompatActivity; |
| 3 | + |
| 4 | +import android.app.Activity; |
| 5 | +import android.database.sqlite.SQLiteOpenHelper; |
5 | 6 | import android.os.Bundle;
|
| 7 | +import android.support.v7.app.AppCompatActivity; |
6 | 8 | import android.view.View;
|
7 |
| -import android.widget.ImageView; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.BaseAdapter; |
| 11 | +import android.widget.LinearLayout; |
8 | 12 | import android.widget.ListView;
|
9 |
| -import android.widget.SimpleAdapter; |
10 |
| -import android.widget.TextView; |
11 |
| -import android.database.Cursor; |
12 | 13 | import android.widget.TextView;
|
| 14 | +import android.widget.Toast; |
| 15 | +import java.io.ByteArrayInputStream; |
| 16 | +import java.util.List; |
13 | 17 |
|
14 |
| -import java.util.ArrayList; |
15 |
| -import java.util.HashMap; |
| 18 | +import com.loopj.android.http.AsyncHttpClient; |
| 19 | +import com.loopj.android.http.AsyncHttpResponseHandler; |
| 20 | +import com.loopj.android.image.*; |
16 | 21 |
|
17 | 22 |
|
18 |
| -public class Net1414080903231Sleep extends AppCompatActivity { |
| 23 | +public class Net1414080903231Sleep extends Activity { |
19 | 24 |
|
| 25 | + private ListView lv_music; |
| 26 | + private LinearLayout loading; |
| 27 | + private List<MusicInfo> musicinfos; |
20 | 28 |
|
21 |
| - @Override |
22 |
| - protected void onCreate(Bundle savedInstanceState) { |
23 |
| - super.onCreate(savedInstanceState); |
24 |
| - setContentView(R.layout.activity_net1414080903231_sleep); |
25 |
| - //绑定XML中的ListView,作为Item的容器 |
26 |
| - ListView list = (ListView) findViewById(R.id.SleepListView); |
| 29 | + private class MusicAdapter extends BaseAdapter { |
| 30 | + public int getCount() { |
| 31 | + return musicinfos.size(); |
| 32 | + } |
| 33 | + |
| 34 | + public View getView(int position, View covertView, ViewGroup parent) { |
| 35 | + View view = View.inflate(Net1414080903231Sleep.this, R.layout.net1414080903231_sleep_listitem, null); |
| 36 | + SmartImageView siv = (SmartImageView) view.findViewById(R.id.siv_icon); |
| 37 | + TextView tv_title = (TextView) view.findViewById(R.id.tv_title); |
| 38 | + TextView tv_des = (TextView) view.findViewById(R.id.tv_des); |
| 39 | + TextView tv_type = (TextView) view.findViewById(R.id.tv_type); |
| 40 | + MusicInfo musicinfo = musicinfos.get(position); |
27 | 41 |
|
28 |
| - //生成动态数组,并且转载数据 |
29 |
| - ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); |
30 |
| - for(int i=0;i<5;i++) |
31 |
| - { |
32 |
| - HashMap<String, String> map = new HashMap<String, String>(); |
33 |
| - map.put("ItemTitle", "This is Music....."); |
34 |
| - map.put("ItemText", "This is text....."); |
35 |
| - mylist.add(map); |
| 42 | + siv.setImageUrl(musicinfo.getIconpath(), R.drawable.clockpic, R.drawable.clockpic); |
| 43 | + tv_title.setText(musicinfo.getMusicname()); |
| 44 | + tv_des.setText(musicinfo.getDes()); |
| 45 | + int type = musicinfo.getType(); |
| 46 | + switch (type) { |
| 47 | + case 1: |
| 48 | + tv_type.setText("安静"); |
| 49 | + break; |
| 50 | + case 2: |
| 51 | + tv_type.setText("治愈"); |
| 52 | + break; |
| 53 | + case 3: |
| 54 | + tv_type.setText("入眠"); |
| 55 | + break; |
| 56 | + } |
| 57 | + return view; |
| 58 | + } |
| 59 | + |
| 60 | + public Object getItem(int position) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + public long getItemId(int position) { |
| 65 | + return 0; |
36 | 66 | }
|
37 |
| - //生成适配器,数组===》ListItem |
38 |
| - SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释 |
39 |
| - mylist,//数据来源 |
40 |
| - R.layout.net1414080903231_sleep_listitem,//ListItem的XML实现 |
41 |
| - |
42 |
| - //动态数组与ListItem对应的子项 |
43 |
| - new String[] {"ItemTitle", "ItemText"}, |
44 |
| - |
45 |
| - //ListItem的XML文件里面的两个TextView ID |
46 |
| - new int[] {R.id.SleepItemTitle,R.id.SleepItemText}); |
47 |
| - //添加并且显示 |
48 |
| - list.setAdapter(mSchedule); |
49 | 67 | }
|
50 | 68 |
|
| 69 | + protected void onCreate(Bundle savedInstanceState){ |
| 70 | + super.onCreate(savedInstanceState); |
| 71 | + setContentView(R.layout.activity_net1414080903231_sleep); |
| 72 | + lv_music=(ListView)findViewById(R.id.lv_music); |
| 73 | + loading=(LinearLayout)findViewById(R.id.loading); |
| 74 | + fillData2(); |
| 75 | + } |
| 76 | + private void fillData2(){ |
| 77 | + AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); |
| 78 | + asyncHttpClient.get(getString(R.string.serverurl), |
| 79 | + new AsyncHttpResponseHandler(){ |
| 80 | + public void onSuccess(String content){ |
| 81 | + //Toast.makeText(Net1414080903231Sleep.this,"!!",Toast.LENGTH_SHORT).show(); |
| 82 | + super.onSuccess(content); |
| 83 | + byte[] bytes=content.getBytes(); |
| 84 | + ByteArrayInputStream bais=new ByteArrayInputStream(bytes); |
| 85 | + musicinfos=MusicinfoService.getMusicinfos(bais); |
| 86 | + if(musicinfos==null){ |
| 87 | + Toast.makeText(Net1414080903231Sleep.this,"解析失败",Toast.LENGTH_SHORT).show(); |
| 88 | + } |
| 89 | + else{ |
| 90 | + loading.setVisibility(View.INVISIBLE); |
| 91 | + lv_music.setAdapter(new MusicAdapter()); |
| 92 | + |
| 93 | + } |
| 94 | + } |
| 95 | + public void onFailure(Throwable error,String content){ |
| 96 | + super.onFailure(error,content); |
| 97 | + Toast.makeText(Net1414080903231Sleep.this,"请求失败",Toast.LENGTH_SHORT).show(); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
51 | 101 |
|
52 | 102 | }
|
0 commit comments