简体   繁体   中英

Android: How to add button below the listview

I bind the data from array-list to Listview as below:

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        ListView lstView = getListView();       
        lstView.setChoiceMode(2); 

        ArrayList<String> listTODO = PrepareList();
        lv_arr = (String[]) listTODO.toArray(new String[0]);        
        setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked, lv_arr));
        lstView.setTextFilterEnabled(true);
     }

    private ArrayList<String> PrepareList() {
    ArrayList<String> todoItems = new ArrayList<String>();
    todoItems.add("AAA");
    todoItems.add("BBB");
    todoItems.add("CCC");
    todoItems.add("DDD");
    return todoItems;
    }

And the ListView have set multi-selected. I want to get the text have been selected on that Listview after button hv been clicked. Anybody can help me to put the button below that ListView. I can't figure it out how to put the button coz it's didn't have the xml layout file. .

super.onCreate listView.setContentView(R.layout.your_custom_layout_for_listview)之后添加一起来看看更多信息

You can use a xml layout and out a button under the listview element. The only thing to make sure is to have a listview element with it

android:id="@android:id/list"

the line android:layout_weight="0.5" in ListView is very important.

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:soundEffectsEnabled="true" >
</ListView>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal" >

    <Button            
        android:id="@+id/myBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:textSize="18dip" >
    </Button>

</LinearLayout>

if you do not have xml layout, add these controls to a LinearLayout and set layout_weight in code like the following lines:

LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.MATCH_PARENT,
                             LayoutParams.MATCH_PARENT, 0.5f);
myBtn.setLayoutParams(param);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM