如何在类扩展baseadapter上处理过滤列表单击监听器?

时间:2014-09-11 06:46:45

标签: android xml baseadapter listadapter

每一个人!我是android中的新bie ...在我的应用程序中单击片段上的按钮后我切换到新的Activity显示列表然后我过滤了列表,现在我想获取所选过滤列表项的详细信息回到调用片段。 ..我在适配器类的getView方法中尝试了onClick Listener,它扩展了基本适配器,但它没有切换到调用fragment ..它只保留在那里

My Adapter classs

public class user_list_Adapter extends BaseAdapter {
    private Context context;
    public static ArrayList<user_list_item> user_arraylist;
    private ArrayList<user_list_item> arraylist;
    public static String selected_code;

    public user_list_Adapter(Context context, ArrayList<user_list_item> user_array) {
        this.context = context;
        this.user_arraylist = user_array;
        this.arraylist = new ArrayList<user_list_item>();
        this.arraylist.addAll(user_array);

    }

    @Override
    public int getCount() {
        return user_arraylist.size();
    }

    @Override
    public Object getItem(int position) {
        return user_arraylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.user_row, null);
        }

        TextView txtname = (TextView) convertView.findViewById(R.id.dname);
        TextView txtcode = (TextView) convertView.findViewById(R.id.dcode);

        txtname.setText(user_arraylist.get(position).getCust_name());
        txtcode.setText(user_arraylist.get(position).getCode());

        // Listen for ListView Item Click
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
              Deposite df = new Deposite();
            MainActivity activity = (MainActivity) context; //this line gives class cast exception
            FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
            Bundle b = new Bundle();
            b.putString("search_name ", search_name);
            b.putString("search_code ", search_code);
            df.setArguments(b);
            transaction.replace(R.id.content_frame, df);
            transaction.addToBackStack(null);
            transaction.commit();
                    }
        });

        return convertView;
    }


    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        user_arraylist.clear();
        if (charText.length() == 0) {
            user_arraylist.addAll(arraylist);
        }
        else
        {
            for (user_list_item item : arraylist)
            {
                if (item.getCust_name().toLowerCase(Locale.getDefault()).contains(charText))
                {
                    user_arraylist.add(item);
                }

                if (item.getCode().toLowerCase(Locale.getDefault()).contains(charText))
                {
                    user_arraylist.add(item);
                }
            }
        }
        notifyDataSetChanged();
    }

}

1 个答案:

答案 0 :(得分:0)

我的Adpater类,内部类过滤器

public class user_list_Adapter extends ArrayAdapter<user_list_item> implements Filterable 
{

    private Filter mfilter;
    private Context context;
    private ArrayList<user_list_item> filter_user_list;
    private ArrayList<user_list_item> alluserlist;
    //public static String selected_code;

    public user_list_Adapter(Context context, ArrayList<user_list_item> user_array) {
        super(context,R.layout.user_row,user_array);
        this.context = context;
        this.filter_user_list = user_array;
        this.alluserlist = new ArrayList<user_list_item>();
        this.alluserlist.addAll(user_array);

    }

    @Override
    public int getCount() {
        return filter_user_list.size();
    }

    @Override
    public user_list_item getItem(int position) {

        return filter_user_list.get(position);
    }

    @Override
    public long getItemId(int position) {

       return filter_user_list.get(position).hashCode();
    }

    public void resetData() {
        filter_user_list = alluserlist;
    }


    /* *********************************
     * We use the holder pattern
     * It makes the view faster and avoid finding the component
     * **********************************/

    private static class UserHolder {
        public TextView txtname;
        public TextView txtcode;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        UserHolder uholder=new UserHolder();

        // First let's verify the convertView is not null
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.user_row, null);
            // Now we can fill the layout with the right values
            TextView tvname = (TextView) convertView.findViewById(R.id.dname);
            TextView tvcode = (TextView) convertView.findViewById(R.id.dcode);


            uholder.txtname = tvname;
            uholder.txtcode = tvcode;

            convertView.setTag(uholder);
        }
        else
            uholder = (UserHolder) convertView.getTag();

        user_list_item p = filter_user_list.get(position);
        uholder.txtname.setText(p.getCust_name());
        uholder.txtcode.setText(p.getCode());

        return convertView;
    }

        /*
     * We create our filter
     */

    @Override
    public Filter getFilter() {
        if (mfilter == null)
            mfilter = new userFilter();

        return mfilter;
    }

    private class userFilter extends Filter {

       @Override
        protected FilterResults performFiltering(CharSequence charText) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            if (charText == null || charText.length() == 0) {
                // No filter implemented we return all the list
                results.values = alluserlist;
                results.count = alluserlist.size();
            }
            else {
                // We perform filtering operation
                List<user_list_item> uList = new ArrayList<user_list_item>();

                for (user_list_item p : filter_user_list) {
                    if (p.getCust_name().toLowerCase(Locale.getDefault()).contains(charText)) {
                    uList.add(p);
                }

                    if (p.getCode().toLowerCase(Locale.getDefault()).contains(charText)) {
                    uList.add(p);
                }
                }

                results.values = uList;
                results.count = uList.size();

            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {

                filter_user_list = (ArrayList<user_list_item>) results.values;
                notifyDataSetChanged();
            }

        }

    }

    }

在我的search_window Activity类ie.List类中调用适配器类 将onItemclick侦听器设置为

 // Binds the Adapter to the ListView

 list.setAdapter(adapter);

       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {

                // We know that each row in the adapter is a Map
                user_list_item row =  adapter.getItem(position);
                //Toast.makeText(getApplicationContext(),"You Clicked On.."+row.getCust_name(),Toast.LENGTH_LONG).show();
                MainActivity.deposite_search_flag = true;     //global static variable 
                MainActivity.search_name = row.getCust_name(); //global 
                MainActivity.search_code =row.getCode();     //global 
                for (int i=0;i<RecordList.size();i++){  //user_list_item only contains name n code
                    if (row.getCode()==Code_Array[i]){
                        MainActivity.search_available = avail_bal_Array[i];
                    }
                }
                finish();  //switch to caller fragment and executes OnResume method of fragment
            }
        });

通过在MainActivity类上设置全局变量,我通过直接完成此活动切换到调用者片段(finish();)

//In caller fragment class
 @Override
    public void onResume() {
        super.onResume();
        if (MainActivity.deposite_search_flag){
            txtname.setText(MainActivity.search_name);
            txtcode.setText(MainActivity.search_code);
            txt_available_bal.setText(MainActivity.search_available);
        }
    }