首页 > 代码库 > stu
stu
1.Stu
public class Stu {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.StuAdapter
public class StuAdapter extends BaseAdapter {
List stuList;
Activity activity;
public StuAdapter(List stuList,Activity activity){
this.stuList=stuList;
this.activity=activity;
}
@Override
public int getCount() {
return stuList.size();
}
@Override
public Object getItem(int position) {
return stuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=activity.getLayoutInflater().inflate(R.layout.listview_item,null);
TextView textView_name=(TextView)view.findViewById(R.id.name);
TextView textView_age=(TextView)view.findViewById(R.id.age);
textView_name.setText(stuList.get(position).getName());
textView_age.setText(stuList.get(position).getAge()+"");
return view; }
3.MainActivity
public class MainActivity extends AppCompatActivity {
List stuList=null;
ListView listView;
public Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 200:
if (stuList!=null){
StuAdapter stuAdapter=new StuAdapter(stuList,MainActivity.this);
listView.setAdapter(stuAdapter);
}
break;
case -1:
Toast.makeText(MainActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
break;
case -2:
Toast.makeText(MainActivity.this,"获取异常",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(MainActivity.this,"我也不知道为啥",Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listview);
getDateFromSeriver();
}
private void getDateFromSeriver() {
final String path="";
new Thread(){
@Override
public void run() {
super.run();
try {
URL url=new URL(path);
HttpURLConnection coon=(HttpURLConnection)url.openConnection();
coon.setRequestMethod("GET");
coon.setConnectTimeout(5000);
int code=coon.getResponseCode();
if (code==200){
InputStream inputStream=coon.getInputStream();
String abc=inputstream2string(inputStream);
JSONObject jsonObject=new JSONObject(abc);
if (jsonObject.getString("code").equals("200")){
String studata=http://www.mamicode.com/jsonObject.getString("data");
Gson gson=new Gson();
stuList=gson.fromJson("studata",new TypeToken>(){}.getType());
Message msg=new Message();
msg.what=200;
msg.obj=abc;
handler.sendMessage(msg);
}else {}
}else {
Message msg=new Message();
msg.what=-1;
handler.sendMessage(msg);
}
}catch (Exception e){
Message msg=new Message();
msg.what=-2;
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
private String inputstream2string(InputStream inputStream) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int length = 0;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
inputStream.close();
baos.close();
//或者用这种方法
//byte[] result=baos.toByteArray();
//return new String(result);
return baos.toString();
} catch (Exception e) {
e.printStackTrace();
return "获取失败";
}
}
}
4.布局文件
(1)mainactivity
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.believer.gougoude.MainActivity">
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
(2)listview_item.xml
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="姓名"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/age"
android:text="年龄"/>
.Stu
StuAdapter
MianActivity
2.布局文件
stu
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。