-1

I have two views In a single activity StackView and Gridview and 2 corresponding adapters .when I clicked on StackView item it should flip the grid item in Gridview and vice versa?

Trinadh Koya
  • 1,089
  • 15
  • 19

2 Answers2

-1

// activity classs

  public class SampleActivity extends Activity implements    
  OnGridChangeListener {

  public void onCreate(bundle){
 // replace this with your adapter class. 
 Adapter adapter = new adapter(this); 
 }

 @override
 public void OnGridChange(){
   //here you go.
  // write code to do what you want.
}

    // interface to communicate with activity
  public interface OnGridChangeListener {
       public void OnGridChange()
   }


   // adaptor class
  public class Adaptor extends "you apapter class to extend"{

  OnGridChangeListener onGridChangeListener ;

  public Adapter(OnGridChangeListener  listener){
     onGridChangeListener =listener
   }

 public getView(){

   public void onclick(){
      onGridChangeListener.OnGridChange("pass you data");
}
}

 }
Keyur Thumar
  • 608
  • 7
  • 19
-2

As per your question this is what i get -

You have a Activity with two independent views which has their own adapters. So when there is changes in one of the adapter you want it to be reflected into another adapter.

The simple solution for your query would be -

  1. When there is a change in first adapter you reflect the change to the activity. after that call the function in the second adapter to reflect the change you want in second adapter.
  2. For this you have to define an interface in first adapter and implement this is activity.
  3. When the first adapter changes call the interface method and this will reflect in activity.
  4. Then call method in second adapter to do the changes you want.

Code example -

MainActivity.Java

public class MainActivity extends AppCompatActivity implements FirstAdapter.callBackMethods {

FirstAdapter firstAdapter;
SecondAdapter secondAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    firstAdapter = new FirstAdapter(MainAtivity.this.getApplicationContext());
    //do all the declaration and operation of first adapter. Pass context along with your required params.

    secondAdapter = new SecondAdapter();
    //do all the declaration and operation of second adapter.
  }

//callback method of first adapter
@override
public void callback(){
    //changes have been done in FirstAdapter and this methos is fired.
    //now do do the changes in SecondAdapter as per req.
    if(secondAdapter != null){
       secondAdapter.reflectChanges();
    }
  }
}

FirstAdapter.class

I am taking example of recylerview adapter.

public class FirstAdapter extends RecyclerView.Adapter<FirstAdapter.ViewHolder>{

public FirstAdapter(Context context){
  this.context=context;
  }

/*
  all the boilerplate codes and business logic
 */

//when you want to reflect the changes
callBackMethods callBackMethods = (callBackMethods) context;
callBackMethods.callback();
//this will fireup the implementation in the MainActivity.

public interface callBackMethods{
  public void callback();
  }
}

SecondAdapter.class

This is where the changes will be reflected.

public class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.ViewHolder>{

/*
  all the boilerplate codes and business logic
 */

public void reflectChanges(){
   /*
    This method will be called from the MainActivity. pass whatever params you want to pass from Activity and do the changes you want to do in the SecondAdapter.
    */
  }
}

I hope this solves your problem. Happy coding...

Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • Sorry @Sagar Nayak.It won't help me any way – Trinadh Koya Apr 09 '17 at 06:36
  • Sure !! I will try !! I can't give you more than that .What i can ask is how to get the second adopter position in first adopter Call back without applying callback !! – Trinadh Koya Apr 09 '17 at 07:01
  • your question is too confusing buddy. just read the oops basics .. you will get the answer on communication between classes. – Sagar Nayak Apr 09 '17 at 08:18
  • if you don't understand my question,please Ask as many no of times.Don't try to underestimate others it does not make sense.May be you might have a great knowledge on what you are talking,so try to express .I asked you to give me a clue how to do the stuff .You have written your own code apart from my Question. – Trinadh Koya Apr 09 '17 at 08:24
  • In short,if you click on first position in First Adapter,it should effect(do some action) on the same position on second Adapter.So i have taken as Stackview and GridView. – Trinadh Koya Apr 09 '17 at 08:26
  • yes , for this you have to make communication between two adapters. and as the adapters can not make any direct communication you take help of mainactivity. and my answer says how to get that. you pass position from adapter to interface, then this position comes to activity. then pass that position to method in second activity, then make the changes in the position you got. – Sagar Nayak Apr 09 '17 at 08:58
  • Will try to do that !! Sagar !! Can you share me if possible – Trinadh Koya Apr 09 '17 at 09:00
  • sure . go ahead. – Sagar Nayak Apr 09 '17 at 09:28