• 在Activity里显示ListView列表


    EX04_19.java 
    package irdc.ex04_19;
    import android.app.Activity;
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter; 
    import android.widget.LinearLayout; 
    import android.widget.ListView; 
    import android.widget.TextView; 
    public class EX04_19 extends Activity 
    { 
      private static final String[] array = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" }; 
      LinearLayout myLinearLayout; 
      TextView myTextView; 
      ListView myListView;
      /** Called when the activity is first created. */
      @Override 
      public void onCreate(Bundle savedInstanceState) 
      { 
        super.onCreate(savedInstanceState);
        /* 新增LinearLayout */ 
        myLinearLayout = new LinearLayout(this); 
        myLinearLayout.setOrientation(LinearLayout.VERTICAL);
        myLinearLayout.setBackgroundColor(android.graphics.Color.WHITE);
        /* 新增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.setTextColor(getResources().getColor(R.drawable.blue)); 
        /* 将TextView加到myLinearLayout */ 
        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));
        /* 将ListView加到myLinearLayout */
        myLinearLayout.addView(myListView, param2); 
        /* 将LinearLayout加到ContentView */
        setContentView(myLinearLayout); 
        /* new ArrayAdapter对象并将array字符串数组传入 */ 
        ArrayAdapter adapter =
          new ArrayAdapter(this, R.layout.my_simple_list_item, array); 
        /* 将ArrayAdapter加入ListView对象中 */
        myListView.setAdapter(adapter); 
        /* myListView加入OnItemSelectedListener */ 
        myListView .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        { 
          @Override 
          public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) 
          { 
            /* 使用getSelectedItem()将选取的值带入myTextView中 */ 
            myTextView.setText("你选的是" + arg0.getSelectedItem().toString()); 
            }
          @Override 
          public void onNothingSelected(AdapterView arg0) 
          { 
            // TODO Auto-generated method stub 
            } 
          }); 
        /* myListView加入OnItemClickListener */ 
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        { 
          @Override 
          public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) 
          { 
            /* 使用String[index],arg2为点选到ListView的index,并将值带入myTextView中 */ 
            myTextView.setText("你选的是" + array[arg2]); 
            }
          }); 
        } 
      }
          
     main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
      <TextView  
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"
      />
    </LinearLayout>
    

     my_simple_list_item.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <CheckedTextView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/myCheckedTextView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:textColor="@drawable/black" 
    /> 
    
    

     color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <drawable name="black">#000000</drawable>
      <drawable name="white">#FFFFFFFF</drawable>
      <drawable name="blue">#0000FF</drawable>
      <drawable name="ltgray">#CCCCCC</drawable>
    </resources>
    

     string.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <string name="title">使用ListView显示明细列表</string>
      <string name="hello">Hello World, empty</string>
      <string name="app_name">EX04_19</string>
    </resources>
    

    在添加ArrayAdapter中,使用ArrayAdapter(Context context,int textViewResourceId,T[] objcets)

    例外范例中都是动态的构造LinearLayout,TextView,ListView。

    如上还有一点可值得一提,可以用ListView里的setChoiceMode,传入的值有三种

    ListView.CHOICE_MODE_NONE        0    没有指定选择方式

    ListView.CHOICE_MODE_SINGLE      1     最多一个

    ListView.CHOICE_MODE_MULTIPLE   2      多选

    当ArrayAdapter的构造器textViewResourceId传入Android.R.layout.simple_list_item_multiple_choice,代表ListView是可多选的,此时就需要ListView的setChoiceMode设置了。

  • 相关阅读:
    【域控】获取域控用户
    【MongoDB】开启认证权限
    【MongoDB】 安装为windows services
    【 Quartz】使用 JobListener (任务监听器可实现) 我想在一个任务执行后在执行第二个任务怎么办呢
    【多路复用】I/O多路复用
    静态类和静态类成员
    C#
    response.redirect和server.Transfer的差别详解
    DataReader
    受管制的代码和强类型系统
  • 原文地址:https://www.cnblogs.com/shanshan520/p/2371668.html
Copyright © 2020-2023  润新知