ListView的使用

1.第一种,使用数组适配器(0调用静态方法)

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list_activity);
 ListAdapter adapter = ArrayAdapter.createFromResourc(this,R.array.myarrays,android.R.layout.simple_list_item_1);
 this.setListAdapter(adapter);

2.详细讲解下第二种

首先我们new一个数组

 String[] strs = {"aaa","sss","ddd","fff"};

然后我们创建一个新的适配器对象,

 ListApadter adapter =new ListAdapter <> (this,android.R.layout.simple_list_item_2,strs);
this.setListAdapter(adapter);

this指的是当前Listview,android.R.layout.simple_list_item_2每一行的样式(系统的样式),想要自定义需要自己创建布局,选择textview

171620_0yt0_2502566.png

我们在其中定义下Textview样式

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#ff9900">
   
</TextView>

我们可以设置文件过滤器,我们要获取listview

 this.getListview().setTextFilterEnabled(ture);

当我们在应用的时候学要实现监听,获取用户点击事件,

this.getListView().setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View View, int position, long id) {
   // TODO Auto-generated method stub
   Toast.makeText(MainActivity.this, View.getClass().getName()+""+strs[position], 0).show();
  }
 
 });

position指的是单行的行号,就是View的位置

parent指的是ListView,view指的是每一单行的View(就是TextView)

现在运行一下

我们发现现在显示的只有listview,但是我们想加其他控件呢?

我们重新布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/biaoge"
        android:layout_weight="0"
        />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1">
</ListView>
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="delete"
        android:layout_weight="0"/>
</LinearLayout>

接下来看重点在这

不需要创建新的listview,我们只需要使用android提供的list,把他放进来就可以了

我们在其运行,发现,按钮超出屏幕在,现在我想要有Button必须存在,在离我们引入权重

在TextView和Buttomn中写入android:layout_weight="0"

我们使用最小的权重就Ok,然后我们在ListView中加入android:layout_weight="1"

其实Adapter不仅适用数据,一课时使用资源文件,例如图片列表

全部代码

 package com.example.listactivtity;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
 String[]  strs = {"aa","ss","dd","ff"};
 private boolean ture;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list_activity);
//  ListAdapter adapter = ArrayAdapter.createFromResource(this,R.array.myarrays,android.R.layout.simple_list_item_1);
//  this.setListAdapter(adapter);
// ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2,strs);
  ListAdapter adapter = new ArrayAdapter<Object>(this, R.layout.list_array,strs);
 this.setListAdapter(adapter);
 this.getListView().setTextFilterEnabled(ture);
 this.getListView().setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View View, int position, long id) {
   // TODO Auto-generated method stub
   Toast.makeText(MainActivity.this, View.getClass().getName()+""+strs[position], 0).show();
  }
 
 });
 }

}

转载于:https://my.oschina.net/u/2502566/blog/529047

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值