ListView에서 다중 선택 항목을 설정하는 데 문제가 있습니다. 내 ListView에는 Android 전화에서 가져온 연락처의 이름이 있습니다.ListView에서 다중 선택 항목을 설정할 수 없습니다.
객관식을 설정하는 예를 따라 왔지만 제대로 작동하지 않았습니다.
나는
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this.setListAdapter(adapter);
를 입력 해봤지만리스트 뷰에서 체크 박스를 볼 수 없습니다.
그리고 R.layout.contacts_list
을 android.R.layout.simple_list_item_multiple_choice
으로 바 꾸었습니다. 안드로이드 폰의 연락처에는 체크 박스가 있지만 이름은 표시되지 않았습니다. 여기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/light_goldenrod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stackFromBottom="false"
android:transcriptMode="normal"
android:choiceMode="multipleChoice" >
</ListView>
<TextView
android:textColor="@color/dark_purple"
android:id="@+id/contactName"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
ContactsList.java
public class ContactsList extends ListActivity
{
final Context context = this;
Cursor cursor;
String[] buddiesList =
{"Kanak Priya",
"Joanne Liew",
"Michelle Lam",
"Teo Kin Hua",
"Melissa Haiting",
"David Yeo",
"Natasha Akhbar",
"Gillian Gan",
"Sonia",
"Ms Long",
"Joan Tang",
"Stephanie",
"Nur Ashiqin"
};
BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
ListView list = getListView();
setListAdapter(new ArrayAdapter<String> (this, R.layout.contacts_list, R.id.contactName, buddiesList));
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
int[] views = new int[] {R.id.contactName};
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
this.setListAdapter(adapter);
/*if(cursor.getCount()<0)
{
Intent displayIntent = new Intent(Intent.ACTION_VIEW);
displayIntent.setData(Uri.parse("content://contacts/people"));
startActivity(displayIntent);
displayIntent = new Intent(Intent.ACTION_VIEW);
//displayIntent.setData(Uri.parse("content://contacts/people/"+id));
startActivity(displayIntent);
}
else
{
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("ALERT!");
alertDialog.setMessage("NO Name is Found! D:");
alertDialog.setIcon(R.drawable.warning);
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
Intent backIntent = new Intent(context, ContactsList.class);
startActivity(backIntent);
}
});
alertDialog.show();
}*/
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
buddyDB.open();
long name_id;
super.onListItemClick(l, v, position, id);
Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
TextView contactName = (TextView) v.findViewById(R.id.contactName);
String NameValue = contactName.getText().toString();
name_id = buddyDB.insertNames(NameValue);
Toast.makeText(getBaseContext(),
"Selected: " + buddiesList[position], Toast.LENGTH_SHORT).show();
buddyDB.close();
아,
String[] buddiesList =
{"Kanak Priya",
"Joanne Liew",
"Michelle Lam",
"Teo Kin Hua",
"Melissa Haiting",
"David Yeo",
"Natasha Akhbar",
"Gillian Gan",
"Sonia",
"Ms Long",
"Joan Tang",
"Stephanie",
"Nur Ashiqin"
};
내 토스트 메시지입니다.
정말 도움이 필요합니다. 어떤 도움을 주시면 감사하겠습니다. 예제도 크게 감사하겠습니다.
감사
안녕하세요, 코드를 시험해 보겠습니다. 문제가 생기면 알려 드리겠습니다. 당신의 대답에 감사드립니다. – Preeyah
Android 리소스 - res 폴더에'simple_list_item_multiple_choice'을 (를) 만들어야합니까? – Preeyah
no. 그러나 기본 안드로이드 시스템의 listview 행과 함께 올 것이다.독자적인리스트 뷰의 행을 구현하고 싶다면, res/layout 폴더에 contacts_list.xml을 다시 정의해야한다. – PhatHV