Android 使用自定义阵列适配器 [英] Android working with custom array adapter

查看:62
本文介绍了Android 使用自定义阵列适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究自定义阵列适配器.我有一个可扩展的列表视图,我正在向其中添加列表.我还有一个 search 视图,用户应该可以在其中搜索任何项目.下面是我的自定义适配器和片段的代码

I am working on a custom array adapter. I have an expandable list view to which I am adding the list. I also have a search view in which users should be able to search any item. Below is my code for my custom adapter and my fragment

public class ThreeLevelListAdapter extends BaseExpandableListAdapter {

String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;

/**
 * Constructor
 * @param context
 * @param parentHeader
 * @param secondLevel
 * @param data
 */
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
    this.context = context;

    this.parentHeaders = parentHeader;

    this.secondLevel = secondLevel;

    this.data = data;
}

@Override
public int getGroupCount() {
    return parentHeaders.length;
}

@Override
public int getChildrenCount(int groupPosition) {


    // no idea why this code is working

    return 1;

}

@Override
public Object getGroup(int groupPosition) {

    return groupPosition;
}

@Override
public Object getChild(int group, int child) {


    return child;


}



@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return true;
}



@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.row_first, null);
    TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
    text.setText(this.parentHeaders[groupPosition]);

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);

    String[] headers = secondLevel.get(groupPosition);


    List<String[]> childData = new ArrayList<>();
    HashMap<String, String[]> secondLevelData = data.get(groupPosition);

    for (String key : secondLevelData.keySet()) {


        childData.add(secondLevelData.get(key));

    }


    secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));

    secondLevelELV.setGroupIndicator(null);


    secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        int previousGroup = -1;

        @Override
        public void onGroupExpand(int groupPosition) {
            if (groupPosition != previousGroup)
                secondLevelELV.collapseGroup(previousGroup);
            previousGroup = groupPosition;
        }
    });


    return secondLevelELV;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}}

`片段

SearchView mSearchView;
static String TAG = "product search";
ExpandableListView suggestion_list; 
ThreeLevelListAdapter threeLevelListAdapterAdapter;
 String[] parent = new String[]{"A2A 100 MG TABS", "AQUA-VIT SACHET", "BRONCOPHYLINE SYRUP"};
String[] q1 = new String[]{"BAHRIA TOWN MARKET", "MAIN ROAD BAGH MONCHI LADHA"};
String[] q2 = new String[]{"DAROGHEWALA MAIN BAZAR"};
String[] q3 = new String[]{"ASKARI-10 HOUSING SOCITEY","CHUNGI DOGAJE"};
String[] des1 = new String[]{"M/S MEDSERVE PHARMACY"};
String[] des2 = new String[]{"M/S DANISH MEDICAL STORE"};
String[] des3 = new String[]{"FUTURE HEALTH PHARMACY","M/S ASAD PHARMACY","M/S GHAFOOR MEDICAL STORE"};
String[] des4 = new String[]{"M/S NOOR MEDICOSE"};
String[] des5 = new String[]{"M/S ABDUL GHAFAR MEDIACL STORE"};
ArrayList<String> parentlist = new ArrayList<String>(Arrays.asList(parent));
LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
 * Second level array list
 */
List<String[]> secondLevel = new ArrayList<>();
/**
 * Inner level data
 */
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();

 secondLevel.add(q1);
    secondLevel.add(q2);
    secondLevel.add(q3);
    thirdLevelq1.put(q1[0], des1);
    thirdLevelq1.put(q1[1], des2);
    thirdLevelq2.put(q2[0], des3);
    thirdLevelq3.put(q3[0], des4);
    thirdLevelq3.put(q3[1], des5);

    data.add(thirdLevelq1);
    data.add(thirdLevelq2);
    data.add(thirdLevelq3);

threeLevelListAdapterAdapter = new ThreeLevelListAdapter(getActivity(), parent, secondLevel, data);
    suggestion_list.setAdapter(threeLevelListAdapterAdapter);

 mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            if (TextUtils.isEmpty(s)) {
                // here I want to empty the threeLevelListAdapterAdapter

                suggestion_list.clearTextFilter();
            } else {
                // here I want to fill the threeLevelListAdapterAdapter 
            }
            return true;
        }
    });

更新 1

我找到了以下代码,它使用可扩展的列表视图实现搜索.

I have found the below code which implements the search with an expandable list view.

 public void filterData(String query){

 query = query.toLowerCase();
 Log.v("MyListAdapter", String.valueOf(continentList.size()));
 continentList.clear();

 if(query.isEmpty()){
   continentList.addAll(originalList);
 }
 else {

 for(Continent continent: originalList){
 
 ArrayList<Country> countryList = continent.getCountryList();
 ArrayList<Country> newList = new ArrayList<Country>();
 for(Country country: countryList){
 if(country.getCode().toLowerCase().contains(query) ||
   country.getName().toLowerCase().contains(query)){
  newList.add(country);
  }
 }
 if(newList.size() > 0){
  Continent nContinent = new Continent(continent.getName(),newList);
  continentList.add(nContinent);
 }
 }
 }

 Log.v("MyListAdapter", String.valueOf(continentList.size()));
 notifyDataSetChanged();

 }

如何在搜索过程中清除和填充我的自定义适配器?

How I can clear and fill my custom adapter during the search?

任何帮助将不胜感激

推荐答案

你所要做的就是为你的搜索创建一个新的list并传入你的适配器.当用户完成搜索后,将第一个列表传递给您的适配器.

All you have to do is to make a new list for your search and pass into your adapter. When the user is done with search pass the first list to your adapter.

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            String text = s.getText().toString().toLowerCase(Locale.getDefault());
            ArrayList<Country> countryList = continent.getCountryList();
            List<Country> resultData = new ArrayList<>();
            for (Country country: countryList) {
                if (country.getCode().toLowerCase().contains(text) ||
                        country.getName().toLowerCase().contains(text) )) {
                    resultData.add(country);
                }
            }

            threeLevelListAdapterAdapter = new ThreeLevelListAdapter(getActivity(), parent, secondLevel, resultData);
            suggestion_list.setAdapter(threeLevelListAdapterAdapter);
            if (text.length() == 0) {
                threeLevelListAdapterAdapter = new ThreeLevelListAdapter(getActivity(), parent, secondLevel, data);
                suggestion_list.setAdapter(threeLevelListAdapterAdapter);
            }
            return true;
        }
        
});

这篇关于Android 使用自定义阵列适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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