android ListView的使用

本文介绍了一个简单的 Android 应用示例,展示了如何使用 ListView 控件显示一周的日期名称,并通过 ArrayAdapter 设置适配器。同时,文章还讲解了如何为 ListView 添加 OnItemSelectedListener 和 OnItemClickListener 事件,以实现对列表项的选择反馈。

在Acvitity中显示要显示列表,并且显示相应内容:

public class MainActivity extends Activity {
 private static final String[] array = {
  "sunday","monday","tuesday","wednesday",
  "thursday","friday","saturday"
 };
 
 private LinearLayout myLinearLayout;
 private TextView myTextView;
 private ListView myListView;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //创建一个布局器
        myLinearLayout = new LinearLayout(this);
        myLinearLayout.setBackgroundColor(android.graphics.Color.WHITE);
        myLinearLayout.setOrientation(LinearLayout.VERTICAL);
       
        //添加TextView
        myTextView = new TextView(this);
        LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.FILL_PARENT,
          LinearLayout.LayoutParams.WRAP_CONTENT
        );
        myTextView.setText(R.string.title);
        myTextView.setBackgroundColor(getResources().getColor(R.drawable.blue));
        myLinearLayout.addView(myTextView, param1);
       
        //创建ListView
        myListView = new ListView(this);
        LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.FILL_PARENT,
          LinearLayout.LayoutParams.WRAP_CONTENT
        );
        myListView.setBackgroundColor(getResources().getColor(R.drawable.ltgray));
        myLinearLayout.addView(myListView, param2);
       
        //new ArrayAdapter对象,并将数据传入
        ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>
             (this, R.layout.my_simple_list_item, array);
        myListView.setAdapter(arrAdapter);

 

       setContentView(myLinearLayout);
        
      //====================================================================================
       
        //添加鼠标滚轮选中后出发事件OnItemSelectedListener
        myListView.setOnItemSelectedListener(new OnItemSelectedListener() {

   public void onItemSelected(AdapterView<?> arg0, View arg1,
     int arg2, long arg3) {
    //将鼠标滚轮选中的item的字符串内容显示到myTextView上
    myTextView.setText("你选的是" + arg0.getSelectedItem().toString());
    
   }

   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    
   }
         
        });
       
        //添加鼠标单击事件
        myListView.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
    //arg2是ListView的index
    myTextView.setText("你选中的是" + array[arg2]);
    
   }
         
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zen@sz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值