Android學習之ListView使用基礎

一、ListView的簡單用法android

1.1 佈局ide

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>佈局

1.2 代碼this

public class MainActivity extends Activity {spa

private String[] data={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
ListView listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(adapter);xml

//處理點擊事件
listView.setOnItemClickListener(new OnItemClickListener() {three

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String string=data[arg2];
Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
}
});
}事件

}ip

二、定製ListView的界面utf-8

2.1 新建MyData類,做爲ListView適配器的適配類型

public class MyData{
private String dataString;
private int imageId;
public MyData(String dataString,int imageId) {
// TODO Auto-generated constructor stub
this.dataString=dataString;
this.imageId=imageId;
}
public String getDataString() {
return dataString;
}
public int getImageId() {
return imageId;
}
}

2.2 爲ListView子項指定自定義佈局mydata_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:id="@+id/myDataImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>

<TextView
android:id="@+id/myDataString"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:gravity="center" />

</LinearLayout>

2.3 自定義適配器類

public class MyDataAdapter extends ArrayAdapter<MyData>{

private int resourceId;
public MyDataAdapter(Context context,int textViewResourceId,List<MyData> objects){
super(context, textViewResourceId,objects);
resourceId=textViewResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MyData data=getItem(position);
View view;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId, null);
}
else {
view=convertView;
}
ImageView dataImageView=(ImageView)view.findViewById(R.id.myDataImage);
TextView dataStringTextView=(TextView)view.findViewById(R.id.myDataString);
dataImageView.setImageResource(data.getImageId());
dataStringTextView.setText(data.getDataString());
return view;
}

}

2.4 修改MainActivity代碼

public class MainActivity extends Activity {

private List<MyData> dataList=new ArrayList<MyData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
MyDataAdapter adapter=new MyDataAdapter(MainActivity.this, R.layout.mydata_item, dataList);
ListView listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
MyData data=dataList.get(arg2);
Toast.makeText(MainActivity.this, data.getDataString(), Toast.LENGTH_SHORT).show();
}
});
}

private void initData() {
// TODO Auto-generated method stub
MyData one=new MyData("one", R.drawable.one);
MyData two=new MyData("two", R.drawable.two);
MyData three=new MyData("three", R.drawable.three);
MyData four=new MyData("four", R.drawable.four);
MyData five=new MyData("five", R.drawable.five);
MyData six=new MyData("six", R.drawable.six);
MyData seven=new MyData("seven", R.drawable.seven);
MyData eight=new MyData("eight", R.drawable.eight);
MyData nine=new MyData("nine", R.drawable.nine);
dataList.add(one);
dataList.add(two);
dataList.add(three);
dataList.add(four);
dataList.add(five);
dataList.add(six);
dataList.add(seven);
dataList.add(eight);
dataList.add(nine);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

相關文章
相關標籤/搜索