有ArrayAdapter
个TextView
。
当它设置为ListView
时它正常工作但是当我对它进行排序时,
它根据名称进行排序,但TextView
保持号码不会相应地改变其位置。
如果我不应用排序并尝试使用数字过滤名称,则只显示初始联系人,而不是显示我想要的联系人。
这是我的自定义适配器: -
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomListAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String[] name;
private final String[] number;
public CustomListAdapter(Activity context, String[] name, String[] number) {
super(context, R.layout.name_num_list, name);
this.context=context;
this.name=name;
this.number=number;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.name_num_list, null, true);
TextView txt_name = (TextView) rowView.findViewById(R.id.txt_name);
txt_name.setText(name[position]);
TextView txt_num= (TextView) rowView.findViewById(R.id.txt_number);
txt_num.setText(number[position]);
return rowView;
}
}
这是我的列表活动......
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.hardware.Camera;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
public class ContactListActivity extends Activity implements OnItemClickListener{
String phoneNumber;
String name;
String[] namePerson;
String[] phoneNumberP;
ListView lv;
EditText searchContact;
CustomListAdapter customAdapter;
ArrayList <String> aa= new ArrayList<String>();
ArrayList <String> aa1= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
searchContact=(EditText) findViewById(R.id.EditText_contact_search);
lv= (ListView) findViewById(R.id.lv);
getNumber(this.getContentResolver());
lv.setOnItemClickListener(this);
searchContact();
}
public void getNumber(ContentResolver cr)
{
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
aa.add(name);
aa1.add(phoneNumber);
}
phones.close();// close cursor
phoneNumberP=aa1.toArray(new String[aa1.size()]);
namePerson=aa.toArray(new String[aa.size()]);
customAdapter= new CustomListAdapter(ContactListActivity.this, namePerson, phoneNumberP);
customAdapter.sort(new Comparator<String>() {
@Override
public int compare(String arg1, String arg0) {
return arg1.compareTo(arg0);
}
});
customAdapter.notifyDataSetChanged();
lv.setAdapter(customAdapter);
}
@Override
public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
}
private void searchContact() {
lv.setTextFilterEnabled(true);
searchContact.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
}
@Override
public void beforeTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
}
@Override
public void afterTextChanged( Editable name)
{
ContactListActivity.this.customAdapter.getFilter().filter(name);
customAdapter.notifyDataSetChanged();
}
});
}
}
我的xmls是......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/txt_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
和列表
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/preview6" >
<LinearLayout
android:id="@+id/main_ll_contactlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bg_black">
<EditText
android:id="@+id/EditText_contact_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:drawableLeft="@android:drawable/ic_menu_search"
android:hint="Search"
android:textColor="@android:color/white"
android:typeface="serif"
android:textSize="20sp"
android:focusableInTouchMode="true"
android:background="@android:color/transparent" >
</EditText>
<ListView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/lv"
android:background="@android:color/transparent"
/>
</LinearLayout>
</FrameLayout>
我将FrameLayout
用于其他作品。
如果有任何其他想法,那么建议我。
答案 0 :(得分:2)
问题在于:
您尝试在自定义适配器中维护两个数组:name[]
和number[]
,但ArrayAdapter
旨在跟踪单个数组/列表。
使用customAdapter.sort
对项目进行排序时
它只会对name
数组进行排序,并且您的number
数组将保持不变,因此无论您如何对名称进行排序,数字都将显示在原始位置。
您需要的是维护name
和number
变量之间的映射。你可以使用HashMap。绑定它们的另一个简单方法是将您的项目设为
class Item {
String name;
String number;
}
使用数组Item[]
初始化您的适配器;并通过提供自定义比较器对此数组进行排序:
customAdapter.sort(new Comparator<Item>() {
@Override
public int compare(Item arg1, Item arg0) {
return arg1.name.compareTo(arg0.name);
}
});