ArrayAdapter
ArrayAdapter 仅仅用于文本的显示 lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.tv_info, objects )); 第一个参数:上下文Context 第二个参数:资源文件xml 第三个参数:要显示的文本id 第四个参数:要显示的文本的数组 |
1、list_item.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="30dip"
android:layout_height="30dip"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="文本"
android:textSize="20sp" />
</LinearLayout>
2、MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) this.findViewById(R.id.lv);
String[] objects = new String[100];
for (int i = 0; i < 100; i++) {
objects[i] = "hacket--"+i;
}
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.tv_info, objects ));
} }
|