ListView看上去不难,用起来也不简单啊.老是出错,今天试了下,把问题解决差不多了.Adapter有三个,至于游标,现在没有使用.在后面文章介绍.
出现的问题:在LinearLayout 嵌一个ListView,然后在前面放一个TextView结果.只显示TextView的内容.列表没有了-->
因为LinearLayout是单独显示行或列的.设置android:orientation="vertical"垂直才可以.
使用ArrayAdapter数据的ListView:
这样一个自定义列表就完成了.
SimplaAdapter数据的列表也差不多:只不过这里的列要对应得上,如下面的from和to要对应.掌握这个就容易了,
ListView的简单应用就是这些了.而通常用ListView是不会这样处理的,而更关注它的Adapter重写,在后面的文章都有使用到.
出现的问题:在LinearLayout 嵌一个ListView,然后在前面放一个TextView结果.只显示TextView的内容.列表没有了-->
因为LinearLayout是单独显示行或列的.设置android:orientation="vertical"垂直才可以.
使用ArrayAdapter数据的ListView:
- private ListView listView;
- private TextView textView;
- ArrayAdapter<String> arrayAdapter;
- String[] value={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "};
- private static String TAG="ListViewTest1";
- @Override
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.lv_layout1);
- init();
- }
- private void init(){
- listView=(ListView)findViewById(R.id.listView1);
- textView=(TextView)findViewById(R.id.lvtv1);
- arrayAdapter=new ArrayAdapter<String>(this,R.layout.lv_layout1_row,value);
- //这里的布局网络上查或你到Android.jar里找,就会发现都使用android.R.里面的布局,打开源码看到里面只是一个TextView,只显示一列 我把它拿出来单独放在lv_layout1_row,
- listView.setAdapter(arrayAdapter);
- listView.setOnItemClickListener(new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3){
- setTitle("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());
- textView.setText("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());
- }
- });
- //添加长时间按下的菜单
- listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
- @Override
- public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
- menu.setHeaderTitle("长按菜单-ContextMenu");
- menu.add(0,0,0,"弹出长按菜单0");
- menu.add(0,1,0,"弹出长按菜单1");
- }
- }); //这里的事件参考别人的.而且这只是控件的上下文菜单,并不能对List选中作出什么东西.如果想实现长按List里面的某一项的话就覆盖OnItemLongClick.和OnItemClick基本相似,返回值不同,返回True表示消耗了事件,就不会传播了,如果返回False.还会到OnItemClick事件处理.
- }
- 布局:lv_layout1:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/lv_layout1_ll"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/lvtv1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/app_name"
- android:paddingLeft="12dip"
- android:paddingRight="12dip"
- android:background="#00FF00"/>
- <ListView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/listView1"/>
- </LinearLayout>
- lv_layout1_row:
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/rowtv1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip"
- android:paddingRight="12dip">
- </TextView>
这样一个自定义列表就完成了.
SimplaAdapter数据的列表也差不多:只不过这里的列要对应得上,如下面的from和to要对应.掌握这个就容易了,
- SimpleAdapter simpleAdapter, simpleAdapter2;
- public final static String COLUMN_NAME="name";
- public final static String COLUMN_PHONE="phone";
- List<Map<String,String>> value, value2;
- private static String TAG="ListViewTest2";
- @Override
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.lv_layout2);
- init();
- }
- private void init(){
- listView=(ListView)findViewById(R.id.listView2);
- textView=(TextView)findViewById(R.id.lvtv2);
- button=(Button)findViewById(R.id.listBtn);
- button.setOnClickListener(new View.OnClickListener(){
- public void onClick(View arg0){
- if(listView.getAdapter()==simpleAdapter){
- listView.setAdapter(simpleAdapter2);
- }else{
- listView.setAdapter(simpleAdapter);
- }
- }
- });
- String[] from={COLUMN_NAME,COLUMN_PHONE};
- int[] to={R.id.name,R.id.phone};
- addValue();
- addValue2();
- simpleAdapter=new SimpleAdapter(this,value,R.layout.lv_layout2_row,from,to);
- simpleAdapter2=new SimpleAdapter(this,value2,R.layout.lv_layout2_row,from,to);
- listView.setAdapter(simpleAdapter);
- listView.setOnItemClickListener(new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3){
- setTitle("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());
- textView.setText("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());
- //如果是触摸屏事件,这里的arg0.getSelectedItem()为null,按enter则会显示from中的项.
- }
- });
- //添加长时间按下的菜单
- listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
- @Override
- public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
- menu.setHeaderTitle("长按菜单-ContextMenu");
- menu.add(0,0,0,"弹出长按菜单0");
- menu.add(0,1,0,"弹出长按菜单1");
- }
- });
- }
- //长按菜单响应函数
- @Override
- public boolean onContextItemSelected(MenuItem item){
- setTitle("点击了长按菜单第"+item.getItemId()+"个项目");
- return super.onContextItemSelected(item);
- }
- public List<Map<String,String>> addValue(){
- value=new ArrayList<Map<String,String>>();
- Map<String,String> item1=new HashMap<String,String>();
- item1.put(COLUMN_NAME,"griffin");
- item1.put(COLUMN_PHONE,"132123");
- value.add(item1);
- Map<String,String> item2=new HashMap<String,String>();
- item2.put(COLUMN_NAME,"eoe.android");
- item2.put(COLUMN_PHONE,"132");
- value.add(item2);
- Map<String,String> item3=new HashMap<String,String>();
- item3.put(COLUMN_NAME,"gryphone");
- item3.put(COLUMN_PHONE,"132342");
- value.add(item3);
- return value;
- }
- public List<Map<String,String>> addValue2(){
- value2=new ArrayList<Map<String,String>>();
- Map<String,String> item1=new HashMap<String,String>();
- item1.put(COLUMN_NAME,"google");
- item1.put(COLUMN_PHONE,"123456");
- value2.add(item1);
- Map<String,String> item2=new HashMap<String,String>();
- item2.put(COLUMN_NAME,"android");
- item2.put(COLUMN_PHONE,"456789");
- value2.add(item2);
- Map<String,String> item3=new HashMap<String,String>();
- item3.put(COLUMN_NAME,"netbeans");
- item3.put(COLUMN_PHONE,"147258");
- value2.add(item3);
- return value2;
- }
- 布局:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/lv_layout2_ll"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:id="@+id/listBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/list_btn_label"/>
- <TextView android:id="@+id/lvtv2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/app_name"
- android:paddingLeft="12dip"
- android:paddingRight="12dip"
- android:background="#FF0000"/>
- <ListView android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/listView2"/>
- </LinearLayout>
- ............
- <RelativeLayout
- android:id="@+id/lv_layout2_rl"
- android:layout_width="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingLeft="12dip"
- android:paddingRight="12dip">
- <TextView android:id="@+id/name"
- android:paddingTop="12dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/app_name"/>
- <TextView android:id="@+id/phone"
- android:paddingTop="32dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/app_name"/>
- </RelativeLayout>
ListView的简单应用就是这些了.而通常用ListView是不会这样处理的,而更关注它的Adapter重写,在后面的文章都有使用到.
浙公网安备 33010602011771号