Skip to content Skip to sidebar Skip to footer

How Can I Access Ui Elements Of A Fragment Inside Of A Custom Adapter From An Activity In Android

Good day all, I am working on an application where I have multiple Activities (Activities A, B, B) that inflate this fragment (TheListFragment). This is a visual representation of

Solution 1:

So here is the complete answer i think.

First let each class do its job. So Activity have Fragments and Fragments have adapters and adapters have Holders.

Create a abatract Fragment with the methods you need and then create concrete version of that fragments by making them extend the abstract one.

one of the methods in the abstract fragment should be i.e. public void doSomething();.

in the concrete version of your fragments (A B o C) tha implements the methods you do the actual "something" on the doSomething() method. in these framgents use some kind of colletion to hold the desired information (i use an arraylist). this is the actual data on the list, and holder will pupulate as needed to show on the screen. in you case lest assume that will be the text to put in the elements on the screen. This actually should be a class that represents the elemtens on the screen i.e. class GameItem that has all the elemtns that need to be shown, even the checkbox state. that represent a game.

Then in you adapter (custom adapter) you have to receive two paramenter the context and the arraylist (containg the array of text or to be 100% correct should receive the array of Games) from the holding activity. Also in this adapter create a public method called public void changeGameInfo(); is in method is where you change the content of the array you receive. The holder will acceess the element when you populate the list and grab the appropiate values to show on the screen.

In your class you create the instance of the fragment. at the correct time you call the method you create within the fragment, i.e.fregment.doSomething(); in the doSomething is where you call the adapter.changeGameInfo(); Since is the fragment that holds an instance of the Adapter. and since the adapter has a reference of the arraylist of games you passed as paramters i will know the elemetn that need to be show and that's it,

So as a resume Class XXXX calls fragment.doSomething(). In fragment.doSoemthing you call adapter.changeGameInfo() in the adapter you alter the desired game representation of the arraylist paseed as paramter. the viewholder just shows the elements, After you change the elemnts on the array list remember to call notifyDataSetChanged(); in the adpter so the actual values on the screen get updated automatically

look at examples of the viewholder patter

publicclassInteractiveArrayAdapterextendsArrayAdapter<Model> {

privatefinal List<Model> list;
privatefinal Activity context;

publicInteractiveArrayAdapter(Activity context, List<Model> list) {
  super(context, R.layout.rowbuttonlayout, list);
  this.context = context;
  this.list = list;
}

staticclassViewHolder {
  protected TextView text;
  protected CheckBox checkbox;
}

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
  Viewview=null;
  if (convertView == null) {
    LayoutInflaterinflator= context.getLayoutInflater();
    view = inflator.inflate(R.layout.rowbuttonlayout, null);
    finalViewHolderviewHolder=newViewHolder();
    viewHolder.text = (TextView) view.findViewById(R.id.label);
    viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
    viewHolder.checkbox
      .setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

        @OverridepublicvoidonCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
          Modelelement= (Model) viewHolder.checkbox
              .getTag();
          element.setSelected(buttonView.isChecked());

        }
      });
  view.setTag(viewHolder);
  viewHolder.checkbox.setTag(list.get(position));
} else {
  view = convertView;
  ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolderholder= (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
} 

Post a Comment for "How Can I Access Ui Elements Of A Fragment Inside Of A Custom Adapter From An Activity In Android"