如何在列表视图中获取字母条目? [英] How to get alphabetic entries in the listview ?
问题描述
我尝试按升序修改查询,但输出仍不按字母顺序排列。
以下是 MainActivity.java文件:
包com.example.user.phonebook;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.CursorLoader;
import android.database .cursor;
导入android.net.Uri;
导入android.os.Build;
导入android.os.Bundle;
导入android.provider.ContactsContract;
导入android.util.Log;
导入android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget .TextView;
import java.util.ArrayList;
import java.util.Collections;
公共类MainActivity扩展ListActivity {
ArrayList< contact>联系方式;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState) ;
setContentView(R.layout.activity_main);
contacts = new ArrayList< contact>();
Uri allContacts = ContactsContract.Contacts.CONTENT_URI;
//声明我们的光标
光标c ;
String [] projection = new String []
{ContactsContract.Contacts._ID,
ContactsContract .Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
if(Build.VERSION。 SDK_INT< 11){
// ---如果设备ID在Honey之前在OS上运行梳子
//使用Activity类的managedQuery()来检索托管游标
c = managedQuery(allContacts,projection,null,null,null);
} else {
// --- Honeycomb后来使用游标加载器类来检索托管游标---
CursorLoader cursorLoader = new CursorLoader(
this,
allContacts,
projection,
null,
null,
null);
c = cursorLoader.loadInBackground();
}
PrintContacts(c);
MyClassAdapter适配器;
//再次检测android版本..
if(Build.VERSION.SDK_INT< 11){
// ---如果它在Honeycomb之前---
adapter = new MyClassAdapter(
this,R.layout.row_line,contacts);
} else {
// --- Honeycomb及以后---
adapter = new MyClassAdapter(
this,R.layout.row_line,contacts);
}
this.setListAdapter(适配器);
}
private void PrintContacts(光标c){
ContentResolver cr = getContentResolver();
// ---显示联系人ID和姓名电话号码----
if(c.moveToFirst()){
do {
// ---获取联系人ID和名称
字符串contactID = c.getString(c.getColumnIndex(
ContactsContract.Contacts._ID));
String contactDisplayName =
c.getString(c。 getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
Log.v(Content Providers,contactID +,+
contactDisplayName );
String contactDisplayPhone =;
// ---获取电话号码---
int hasPhone =
c.getInt(c.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(hasPhone == 1 ){
光标phoneCursor =
getContentResolver()。query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null
,null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +ASC);
while(phoneCursor.moveToNext() ){
Log.v(内容提供商,
contactDisplayPhone = phoneCursor.getString(
phoneCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phoneCursor.close();
}
contacts.add(新联系人(contactDisplayName,contactDisplayPhone));
} while(c.moveToNext()) ;
}
}
公共课程联系{
public String contactName =;
public String contactNumber =;
public Contact(String name,String number){< br $> b $ b
contactName = name;
contactNumber = number;
}
}
公共类MyClassAdapter扩展ArrayAdapter< contact> {
私人类ViewHolder {
私有TextView名称;
私有TextView号码;
}
public MyClassAdapter(Context context,int textViewResourceId,ArrayList< contact> items){
super(context,textViewResourceId,项目);
}
公共视图getView(int position,View convertView,ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null){
convertView = LayoutInflater.from(this.getContext())
.inflate( R.layout.row_line,parent,false);
viewHolder = new ViewHolder();
viewHolder.name =(TextView)convertView .findViewById(R.id.contactName);
viewHolder.number =(TextView)convertView.findViewById(R.id.contactNumber);
convertView.setTag(viewHolder);
}其他{
viewHolder =(ViewHolder)convertView.getTag();
}
联系item = getItem(position);
if(item!= null){
viewHolder.name.setText(item.contactName);
viewHolder.number.setText(item.contactNumber);
}
返回convertView;
}
}
}
我尝试了什么:
i尝试使用SimpleCursorAdapter,但条目中有字母但电话号码不可见。你可以参考这个链接:http://stackoverflow.com/questions/38618185/how-should-i-solve-the-issue-of-duplicate-contact-names-and-phone-numbers-not-vi
I tried modifying the query in ascending order but still the output is not in alphabetical order.
The following is the MainActivity.java file:
package com.example.user.phonebook;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class MainActivity extends ListActivity {
ArrayList<contact> contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contacts = new ArrayList<contact>();
Uri allContacts = ContactsContract.Contacts.CONTENT_URI;
//declare our cursor
Cursor c;
String[] projection = new String[]
{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
if (Build.VERSION.SDK_INT < 11) {
//---if the device ids running on OS before Honeycomb
//use the managedQuery() of the Activity class to retrieve a managed cursor
c = managedQuery(allContacts, projection, null, null, null);
} else {
//---Honeycomb and later use the cursor loader class to retrieve managed cursor---
CursorLoader cursorLoader = new CursorLoader(
this,
allContacts,
projection,
null,
null,
null);
c = cursorLoader.loadInBackground();
}
PrintContacts(c);
MyClassAdapter adapter;
//detect the android version again..
if (Build.VERSION.SDK_INT < 11) {
//---if it is before Honeycomb---
adapter = new MyClassAdapter(
this, R.layout.row_line, contacts);
} else {
//---Honeycomb and later---
adapter = new MyClassAdapter(
this, R.layout.row_line, contacts);
}
this.setListAdapter(adapter);
}
private void PrintContacts(Cursor c) {
ContentResolver cr = getContentResolver();
//---display the contact id and name and phone number----
if (c.moveToFirst()) {
do {
//---get the contact id and name
String contactID = c.getString(c.getColumnIndex(
ContactsContract.Contacts._ID));
String contactDisplayName =
c.getString(c.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
Log.v("Content Providers", contactID + ", " +
contactDisplayName);
String contactDisplayPhone = "";
//---get phone number---
int hasPhone =
c.getInt(c.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone == 1) {
Cursor phoneCursor =
getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null
, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
while (phoneCursor.moveToNext()) {
Log.v("Content Providers",
contactDisplayPhone = phoneCursor.getString(
phoneCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phoneCursor.close();
}
contacts.add(new Contact(contactDisplayName, contactDisplayPhone));
} while (c.moveToNext());
}
}
public class Contact {
public String contactName = "";
public String contactNumber = "";
public Contact(String name, String number) {
contactName = name;
contactNumber = number;
}
}
public class MyClassAdapter extends ArrayAdapter<contact> {
private class ViewHolder {
private TextView name;
private TextView number;
}
public MyClassAdapter(Context context, int textViewResourceId, ArrayList<contact> items) {
super(context, textViewResourceId, items);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.row_line, parent, false);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.contactName);
viewHolder.number = (TextView) convertView.findViewById(R.id.contactNumber);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Contact item = getItem(position);
if (item != null) {
viewHolder.name.setText(item.contactName);
viewHolder.number.setText(item.contactNumber);
}
return convertView;
}
}
}
What I have tried:
i tried SimpleCursorAdapter too though in that entries got alphabetical but phone numbers were not visible. you may refer to this link: http://stackoverflow.com/questions/38618185/how-should-i-solve-the-issue-of-duplicate-contact-names-and-phone-numbers-not-vi
推荐答案
请参阅收集| Android开发者 [ ^ ]对于潜在的收集类型。
See Collection | Android Developers[^] for potential collection types.
这篇关于如何在列表视图中获取字母条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!