注意:课程设计只为完成任务,不做细节描述~

效果图

Android课程设计第四天ListView运用-LMLPHP

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="asad"
>
</ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/img_ico"
android:src="@mipmap/x2017612"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/text1"
android:layout_toRightOf="@id/img_ico"
android:text="miemie"
android:textSize="16sp"
android:textColor="#0ccfff"/>
<TextView android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/text2"
android:layout_below="@id/text1"
android:layout_alignLeft="@id/text1"
android:text="xxxxx"
android:textSize="16sp"
android:textColor="#0ccfff"
/>
</RelativeLayout>
 package com.example.myapplication;

 import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; /**
* Created by 樱花落舞 on 2017/6/12.
*/ public class MyAdapter extends BaseAdapter {
private String names[], contents[];
private int images[];
//上下文
private Context context; public MyAdapter(String names[], String contents[], int images[],Context context) {
this.names = names;
this.contents = contents;
this.images = images;
this.context=context;
} //获取列表总数
@Override
public int getCount() {
return names.length;
} //获取单个item
@Override
public Object getItem(int position) {
return names[position];
} //获取itemid
@Override
public long getItemId(int position) {
return position;
} //得到item视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.item,null);
ImageView image = (ImageView) view.findViewById(R.id.img_ico);
TextView name = (TextView) view.findViewById(R.id.text1);
TextView content = (TextView) view.findViewById(R.id.text2); image.setImageResource(images[position]);
name.setText(names[position]);
content.setText(contents[position]); return view;
}
}
 package com.example.myapplication;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/*
listview的使用
在布局中准备控件
在java代码中声明实例化
准备数据源
创建适配器
加载适配器 */
public class MainActivity extends AppCompatActivity {
//声明控件
private ListView listView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listview); String names[]={"xh","xhh","xhhh","xhhhh"};
String contents[]={"Max","max","min","Min"};
int images[]={R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d};
MyAdapter adapter =new MyAdapter(names,contents,images,MainActivity.this);
listView.setAdapter(adapter);
}
}
05-11 17:13