Skip to content

Commit 4a3c83b

Browse files
committed
hzuapps#6 hzuapps#26 第六次作业
1 parent 61b7ac2 commit 4a3c83b

File tree

8 files changed

+288
-61
lines changed

8 files changed

+288
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903231;
2+
3+
4+
public class MusicInfo {
5+
private String iconpath;
6+
private String musicname;
7+
private String des;
8+
private int type;
9+
private long comment;
10+
11+
public String getIconpath() {
12+
return iconpath;
13+
}
14+
15+
public void setIconpath(String iconpath) {
16+
this.iconpath = iconpath;
17+
}
18+
19+
public String getMusicname() {
20+
return musicname;
21+
}
22+
23+
public void setMusicname(String musicname) {
24+
this.musicname = musicname;
25+
}
26+
27+
public String getDes() {
28+
return des;
29+
}
30+
31+
public void setDes(String des) {
32+
this.des = des;
33+
}
34+
35+
public int getType() {
36+
return type;
37+
}
38+
39+
public void setType(int type) {
40+
this.type = type;
41+
}
42+
43+
public long getComment() {
44+
return comment;
45+
}
46+
47+
public void setComment(long comment) {
48+
this.comment = comment;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903231;
2+
3+
4+
import android.util.Log;
5+
import android.util.Xml;
6+
7+
8+
import org.xmlpull.v1.XmlPullParser;
9+
import java.io.InputStream;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class MusicinfoService {
14+
public static List<MusicInfo> getMusicinfos(InputStream is){
15+
XmlPullParser parser= Xml.newPullParser();
16+
try{
17+
parser.setInput(is,"utf-8");
18+
int type=parser.getEventType();
19+
List<MusicInfo> musicInfos=null;
20+
MusicInfo musicInfo=null;
21+
while(type!=XmlPullParser.END_DOCUMENT){
22+
switch(type){
23+
case XmlPullParser.START_TAG:
24+
Log.i("info",parser.getName());
25+
if("music".equals(parser.getName())){
26+
musicInfos=new ArrayList<MusicInfo>();
27+
}else if("musicInfo".equals(parser.getName())){
28+
musicInfo=new MusicInfo();
29+
}else if("icon".equals(parser.getName())){
30+
String icon=parser.nextText();
31+
musicInfo.setIconpath(icon);
32+
}else if("title".equals(parser.getName())){
33+
String title=parser.nextText();
34+
musicInfo.setMusicname(title);
35+
}else if("content".equals(parser.getName())){
36+
String des=parser.nextText();
37+
musicInfo.setDes(des);
38+
}else if("type".equals(parser.getName())){
39+
String musictype=parser.nextText();
40+
musicInfo.setType(Integer.parseInt(musictype));
41+
}else if("comment".equals(parser.getName())){
42+
String comment=parser.nextText();
43+
musicInfo.setComment(Long.parseLong(comment));
44+
}
45+
break;
46+
case XmlPullParser.END_TAG:
47+
if("musicInfo".equals(parser.getName())){
48+
musicInfos.add(musicInfo);
49+
musicInfo=null;
50+
Log.i("info","start2");
51+
}
52+
break;
53+
}
54+
type=parser.next();
55+
}
56+
return musicInfos;
57+
}catch (Exception e){
58+
e.printStackTrace();
59+
return null;
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,102 @@
11
package edu.hzuapps.androidlabs.homeworks.net1414080903231;
22

3-
import android.content.Intent;
4-
import android.support.v7.app.AppCompatActivity;
3+
4+
import android.app.Activity;
5+
import android.database.sqlite.SQLiteOpenHelper;
56
import android.os.Bundle;
7+
import android.support.v7.app.AppCompatActivity;
68
import android.view.View;
7-
import android.widget.ImageView;
9+
import android.view.ViewGroup;
10+
import android.widget.BaseAdapter;
11+
import android.widget.LinearLayout;
812
import android.widget.ListView;
9-
import android.widget.SimpleAdapter;
10-
import android.widget.TextView;
11-
import android.database.Cursor;
1213
import android.widget.TextView;
14+
import android.widget.Toast;
15+
import java.io.ByteArrayInputStream;
16+
import java.util.List;
1317

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.*;
1621

1722

18-
public class Net1414080903231Sleep extends AppCompatActivity {
23+
public class Net1414080903231Sleep extends Activity {
1924

25+
private ListView lv_music;
26+
private LinearLayout loading;
27+
private List<MusicInfo> musicinfos;
2028

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);
2741

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;
3666
}
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);
4967
}
5068

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+
}
51101

52102
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="serverurl">https://raw.githubusercontent.com/ileyinan/android-labs-2017/master/AndroidLabs/app/src/main/res/values/MusicInfo.xml</string>
4+
</resources>
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
5-
android:id="@+id/activity_net1414080903231_sleep"
4+
android:orientation="vertical"
65
android:layout_width="match_parent"
76
android:layout_height="match_parent"
8-
android:paddingBottom="@dimen/activity_vertical_margin"
9-
android:paddingLeft="@dimen/activity_horizontal_margin"
10-
android:paddingRight="@dimen/activity_horizontal_margin"
11-
android:paddingTop="@dimen/activity_vertical_margin"
127
tools:context="edu.hzuapps.androidlabs.homeworks.net1414080903231.Net1414080903231Sleep">
138

14-
15-
<ListView
16-
android:id="@+id/SleepListView"
9+
<FrameLayout
1710
android:layout_width="match_parent"
18-
android:layout_height="match_parent" >
11+
android:layout_height="match_parent">
12+
13+
<LinearLayout
14+
android:layout_width="match_parent"
15+
android:layout_height="match_parent"
16+
android:id="@+id/loading"
17+
android:visibility="invisible"
18+
android:gravity="center"
19+
android:orientation="vertical">
20+
21+
<ProgressBar
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content" />
24+
25+
<TextView
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:text="正在加载信息..."/>
29+
30+
</LinearLayout>
31+
32+
<ListView
33+
android:layout_width="match_parent"
34+
android:layout_height="match_parent"
35+
android:id="@+id/lv_music"></ListView>
36+
37+
</FrameLayout>
1938

20-
</ListView>
21-
</RelativeLayout>
39+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:layout_width="fill_parent"
4-
android:orientation="vertical"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="65dip">
5+
6+
<com.loopj.android.image.SmartImageView
7+
android:id="@+id/siv_icon"
8+
android:layout_width="80dip"
9+
android:layout_height="60dip"
10+
android:layout_alignParentLeft="true"
11+
android:layout_marginLeft="5dip"
12+
android:layout_marginBottom="5dip"
13+
android:layout_marginTop="5dip"
14+
android:scaleType="centerCrop"
15+
android:src="@drawable/clockpic">
16+
17+
</com.loopj.android.image.SmartImageView>
18+
19+
<TextView
520
android:layout_height="wrap_content"
6-
android:id="@+id/SleepListItem"
7-
android:paddingBottom="3dip"
8-
android:paddingLeft="10dip">
21+
android:layout_width="wrap_content"
22+
android:id="@+id/tv_title"
23+
android:layout_marginLeft="5dip"
24+
android:layout_marginTop="10dip"
25+
android:layout_toRightOf="@id/siv_icon"
26+
android:text="我是标题"
27+
android:maxLength="20"
28+
android:singleLine="true"
29+
android:ellipsize="end"
30+
android:textColor="#000000"
31+
android:textSize="18sp">
32+
</TextView>
933

1034
<TextView
11-
android:layout_width="fill_parent"
12-
android:id="@+id/SleepItemTitle"
13-
android:textSize="30dip"
14-
android:layout_height="wrap_content">
35+
android:layout_height="wrap_content"
36+
android:layout_width="wrap_content"
37+
android:id="@+id/tv_des"
38+
android:layout_below="@id/tv_title"
39+
android:layout_marginLeft="5dip"
40+
android:layout_marginTop="5dip"
41+
android:layout_toRightOf="@id/siv_icon"
42+
android:text="我是描述"
43+
android:maxLength="16"
44+
android:singleLine="true"
45+
android:ellipsize="end"
46+
android:textColor="#99000000"
47+
android:textSize="14sp">
1548
</TextView>
1649

1750
<TextView
1851
android:layout_height="wrap_content"
19-
android:layout_width="fill_parent"
20-
android:id="@+id/SleepItemText">
52+
android:layout_width="wrap_content"
53+
android:id="@+id/tv_type"
54+
android:layout_marginBottom="5dip"
55+
android:layout_alignParentBottom="true"
56+
android:layout_alignParentRight="true"
57+
android:text="评论"
58+
android:textColor="#99000000"
59+
android:textSize="12sp">
2160
</TextView>
22-
</LinearLayout>
61+
</RelativeLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="serverurl">https://raw.githubusercontent.com/ileyinan/android-labs-2017/master/AndroidLabs/app/src/main/res/values/MusicInfo.xml</string>
4+
</resources>

0 commit comments

Comments
 (0)