排序和筛选具有两个TextView的自定义数组适配器的ListView? [英] sorting and filtering ListView with custom array adapter with two TextView?

查看:49
本文介绍了排序和筛选具有两个TextView的自定义数组适配器的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个带有两个TextViewArrayAdapter. 将其设置为ListView时,它可以正常工作,但是当我对其进行排序时, 它根据名称进行排序,但是保存数字的TextView不会相应地更改其位置. 如果我不应用排序并尝试用数字过滤姓名,则仅显示初始联系人,而不显示我想要的联系人.

have an ArrayAdapter with two TextView. when set it to ListView it is working correctly but when I sort it, it sorts according to name but TextView which holds number doesn't change their position accordingly. and if I don't apply sort and try to filter name with number then show only initial contacts rather show the contact which I want.

这是我的自定义适配器:-

This is my custom adapter:-

   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;
}

    }

这是我的清单活动....

this is my list activity....

    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();
        }
    });

  }
      }

我的xml是...

      <?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用作我的其他作品.

I take FrameLayout for my other works.

如果有其他任何想法,那么建议我.

if any other idea then suggest me.

推荐答案

这是问题所在: 您尝试在自定义适配器中维护两个阵列:name[]number[],但是ArrayAdapter旨在跟踪单个阵列/列表.

Here is the problem : you try to maintain two arrays in your custom adapter : name[] and number[], however ArrayAdapter is designed to keep track of single array / list.

使用customAdapter.sort对项目进行排序时 它只会对name数组进行排序,而number数组将保持不变,因此无论您如何对名称进行排序,数字都将显示在原始位置.

When you sort items with customAdapter.sort it will sort name array only and your number array will remain intact, thus numbers will be displayed at original positions, no matter how you sort names.

您需要保持在namenumber变量之间的映射.您可以为此使用HashMap.绑定它们的另一个简单解决方案是将您的商品设置为

What you need is to maintain mapping between your name and number variables. You could use a HashMap for that. Another simple solution to bind them is to have your item as

class Item {
    String name;
    String number;
}

使用数组Item[]初始化您的适配器;并通过提供自定义比较器对该数组进行排序:

initialize your adapter with array Item[]; and sort this array by supplying custom comparator :

      customAdapter.sort(new Comparator<Item>() {
          @Override 
          public int compare(Item arg1, Item arg0) {
              return arg1.name.compareTo(arg0.name);
          }
      });

这篇关于排序和筛选具有两个TextView的自定义数组适配器的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆