檢查下面的代碼:
package com.example.mapsdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
public class MainActivity extends Activity {
ArrayList<String> a = new ArrayList<String>();
private ListView lView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillarray();
lView = (ListView) findViewById(R.id.listView1);
lView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, a));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lView.setOnItemClickListener(new OnItemClickListener() {
private String my_sel_items;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// List list = new ArrayList();
my_sel_items = new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ (String) lView.getAdapter().getItem(i);
}
}
Log.v("values", my_sel_items);
}
});
}
private void fillarray() {
// TODO Auto-generated method stub
a.clear();
a.add("a");
a.add("b");
a.add("c");
a.add("d");
a.add("e");
}
}
您造成日誌
03-26 12:25:06.106: V/values(3301): Selected Items,a
03-26 12:25:06.810: V/values(3301): Selected Items,a,b
03-26 12:25:07.466: V/values(3301): Selected Items,a,b,c
03-26 12:25:08.136: V/values(3301): Selected Items,a,b,c,d
編輯:
檢查這個link您可以使用此代碼的任何字體顏色& ListView的背景色。
機器人:文字顏色=「@色/白」 – sprixes 2013-03-26 06:29:19
答案是創建具有屬性顏色/白一個TextView,並把在ListView看到http://stackoverflow.com/questions/4533440/ android-listview-text-color – sprixes 2013-03-26 06:33:41
好的,那麼如何使用多選listview。 – user2210356 2013-03-26 06:36:12