Andriod学习之ListView使用基础_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Andriod学习之ListView使用基础

Andriod学习之ListView使用基础

 2014/12/20 13:26:05  zhouhb  程序员俱乐部  我要评论(0)
  • 摘要:1、ListView的简单用法1.1布局<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/listView1"android
  • 标签:学习 使用 view list

1、ListView的简单用法

1.1 布局

<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 代码

public class MainActivity extends Activity {

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);

//处理点击事件
listView.setOnItemClickListener(new OnItemClickListener() {

@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();
}
});
}

}

2、定制ListView的界面

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;
}

}

上一篇: 如何在一个人输入框中只输入数字 下一篇: 没有下一篇了!
发表评论
用户名: 匿名