ListView列表显示

ListView列表显示

每行有多个控件时

  1. 布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
</ListView>
<ImageView
android:id="@+id/imageview1"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="@+id/textview1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="94dp" />
</RelativeLayout>
  1. MainActivity.java文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.example.listviewdemo;
import android.R.drawable;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/***
* 列表显示,每一栏只有一个TextView时用ArrayAdapter即可 用activity_main.xml
* 当每一栏有多个控件,如图配文字,则需要使用BaseAdapter实现 用two_activity.xml
* @author hdcking.cn
*/
public class MainActivity extends Activity {
ListView listView = null;
String s[] = {"苹果","哈密瓜","桃子","芒果","草莓","菠萝","葡萄","草莓","菠萝","葡萄"};
int data[] = {R.drawable.b0,R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,R.drawable.b5,R.drawable.b6,R.drawable.b2,R.drawable.b3,R.drawable.b4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_activity);
listView = (ListView)findViewById(R.id.listView2);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,s);
myAdapter adapter = new myAdapter();
listView.setAdapter(adapter);
}
public class myAdapter extends BaseAdapter{
@Override
public int getCount() {
return s.length;
}
@Override
public Object getItem(int position) {
return s[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View converView, ViewGroup arg2) {
//布局填充器绑定converView得到每行的view
converView = getLayoutInflater().inflate(R.layout.two_activity, null);
//获取每行的数据
ImageView im = (ImageView) converView.findViewById(R.id.imageview1);
TextView tv = (TextView) converView.findViewById(R.id.textview1);
//回填数据
tv.setText(s[position]);
im.setImageResource(data[position]);
return converView;
}
}
}

每行只有一个TextView时

  1. 布局文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <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"
    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=".MainActivity" >
    <ListView
    android:id="@+id/listview1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    </ListView>
    </RelativeLayout>
  2. MainActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.listviewdemo;
import android.R.drawable;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/***
* 列表显示,每一栏只有一个TextView时用ArrayAdapter即可 用activity_main.xml
* @author hdcking.cn
*/
public class MainActivity extends Activity {
ListView listView = null;
String s[] = {"苹果","哈密瓜","桃子","芒果","草莓","菠萝","葡萄","草莓","菠萝","葡萄"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_activity);
listView = (ListView)findViewById(R.id.listView2);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,s);
listView.setAdapter(adapter);
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!