从适配器获取数据到活动

时间:2018-02-26 20:17:59

标签: android adapter

我有一个检查列表(自定义可扩展列表视图),我通过适配器填充。我希望在单击活动中的文本视图时从适配器检索数组到活动。

活动如下,我想在 onOptionsItemSelected 函数中检索数据:

public class MyPreferencesActivity extends BaseActivity implements PreferencesContract.View, AdapterView.OnItemClickListener, ExpandableListView.OnGroupClickListener {
private PreferencesContract.Presenter mPresenterPreferences;

private Context context;
private ExpandableListView elvListView;
private ArrayList<CategoryModel> categories;
private ExpandableListAdapter ela;
private List<CategoryModel> mCategories;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_my_preferences);
    this.context = this;
    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setupActionbar(toolbar, getString(R.string.preferneces), true, R.drawable.ab_home);

    new PreferencesPresenter(this,
            Injection.provideUseCaseHandler(),
            Injection.provideGetCategories(getApplicationContext())
            );
    mPresenterPreferences.getCategories();


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_save_my_preferences, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    //listener for home

    switch (menuItem.getItemId()) {

        case R.id.menu_create_event:
            break;
    }
    return super.onOptionsItemSelected(menuItem);
}

private void populateList() {
    categories = new ArrayList<>();
    for (int i = 0; i < mCategories.size(); i ++){
        categories.add(new CategoryModel(mCategories.get(i).getCategoryName(), mCategories.get(i).getCategoryId(), mCategories.get(i).getSubCategories()));
    }
}

@Override
public void onItemClick(AdapterView<?> adapterView, android.view.View view, int i, long l) {

}

@Override
public boolean onGroupClick(ExpandableListView expandableListView, android.view.View view, int i, long l) {
    return false;
}

@Override
public void setLoading(boolean isActive) {}

public void initViews(){
    ela = new ExpandableListAdapter(this, categories);

    elvListView = (ExpandableListView) findViewById(R.id.lv_preferences);
    elvListView.setAdapter(ela);
    elvListView.setOnItemClickListener(this);
    elvListView.setOnGroupClickListener(this);
}
@Override
public void showGetPreferencesSuccess(List<CategoryModel> categories) {
    setLoading(false);
    mCategories = categories;
    populateList();
    initViews();
}

@Override
public void showGetPreferencesFail(int errorCode) {

}

@Override
public void setPresenter(PreferencesContract.Presenter presenter) {
    mPresenterPreferences = Preconditions.checkNotNull(presenter);
}
}

适配器如下:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> item;
private ArrayList<Boolean> itemsChecked;
private ArrayList<ArrayList<Boolean>> childChecked;
private ArrayList<ArrayList<String>> childList;

public ExpandableListAdapter(Context c, ArrayList<CategoryModel> d) {
    this.context = c;
    this.item = new ArrayList<>();

    for (int i = 0; i< d.size(); i++){
        this.item.add(d.get(i).getCategoryName());
        ArrayList<String> subCat = new ArrayList<>();
        for (int j = 0 ; j < d.get(j).getSubCategories().size() ; j++){
            subCat.add(d.get(j).getSubCategories().get(j).getSubCategoryName());
        }
        childList.add(subCat);
    }

    itemsChecked = new ArrayList<>();
    childChecked = new ArrayList<ArrayList<Boolean>>();
    for (int i = 0; i < this.item.size() ; i++) {
        itemsChecked.add(false);
        ArrayList <Boolean>temp = new ArrayList<>();
        for (int j = 0 ; j < childList.get(i).size(); j ++){
            temp.add(false);
        }
        childChecked.add(temp);
    }
}

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

    LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = infalInflater.inflate(R.layout.my_preferences_list_group, null);
    TextView tvName = (TextView) convertView.findViewById(R.id.tvGroupName);
    final CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
    if((getChildrenCount(groupPosition) == 0 && itemsChecked.get(groupPosition))|| (getChildrenCount(groupPosition) > 0 && !(childChecked.get(groupPosition).contains(false)))) {
        checkBox.setChecked(true);
    }
    else {
        checkBox.setChecked(false);
    }

    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CheckBox checkBox = (CheckBox)view;
            RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
            TextView child = (TextView)vwParentRow.getChildAt(0);
            final int position = item.indexOf(child.getText());

            if (checkBox.isChecked()){
                itemsChecked.set(position, true);
                for (int i = 0 ; i < getChildrenCount(groupPosition); i++){
                    childChecked.get(groupPosition).set(i, true);
                }
            }
            else {
                itemsChecked.set(position, false);
                for (int i = 0 ; i < getChildrenCount(groupPosition); i++){
                    childChecked.get(groupPosition).set(i, false);
                }
            }
            ExpandableListAdapter.this.notifyDataSetChanged();
        }
    });
    TextView tvCount = (TextView) convertView.findViewById(R.id.tv_item_count);

    tvName.setText(item.get(groupPosition));
    int childCount = getChildrenCount(groupPosition);
    if (childCount > 0) {
        tvCount.setText(getChildrenCount(groupPosition) + "items");
        tvCount.setVisibility(View.VISIBLE);
    }
    if (isExpanded) {
    }
    else {
    }
    return convertView;
}

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = infalInflater.inflate(R.layout.my_preferences_list_item, null);

    TextView tvName = (TextView) convertView.findViewById(R.id.tv_item);
    CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
    if (childChecked.get(groupPosition).get(childPosition))
        checkBox.setChecked(true);

    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            CheckBox checkBox = (CheckBox)view;

            RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
            TextView child = (TextView)vwParentRow.getChildAt(0);
            int position = -1;
            for (int i = 0 ; i < childList.size(); i++){
                if (childList.get(i).contains(child.getText())){
                    if(checkBox.isChecked()) {
                        childChecked.get(i).set(childList.get(i).indexOf(child.getText()) , true);
                        ExpandableListAdapter.this.notifyDataSetChanged();
                    }
                    else {
                        childChecked.get(i).set(childList.get(i).indexOf(child.getText()) , false);
                        ExpandableListAdapter.this.notifyDataSetChanged();
                    }
                }
            }
            if (checkBox.isChecked()){
                if (position >0 )
                itemsChecked.set(position, true);
            }
            else if(position > 0) {
                itemsChecked.set(position, false);
            }
        }
    });
    String s = childList.get(groupPosition).get(childPosition);
    tvName.setText(s);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return childList.get(groupPosition).size();
}   

@Override
public Object getChild(int groupPosition, int childPosition) {
    return childList.get(groupPosition).get(childPosition);
}   

@Override
public int getGroupCount() {
    return item.size();
}

@Override
public Object getGroup(int groupPosition) {
    return item.get(groupPosition);
}

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

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

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

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

我知道将数据从Activity发送到适配器 以其他方式做任何帮助?

1 个答案:

答案 0 :(得分:0)

我认为这就像在适配器中创建一个getter一样简单

public List<String> getItems() {
    return this.item;
}

活动内的onOptionsItemSelected

ela.getItems();