How to add Child and Parent items programmatically [ExpandableRecyclerView]?

Multi tool use
Multi tool use


How to add Child and Parent items programmatically [ExpandableRecyclerView]?



Acutally i'm trying to implement in my app this Expandable Recycler View.
So i made an project a part for make some test and just after i get how it's work impelent it in my app but i'm having some issues.



I've used this classe's from the ERV github for build by app and i've added 2 EditText in the layout and 2 buttons, so that i would archive it if i write something in "GENRE" edittext and i press button ADD GENRE there will be added in RecyclerView an Item and if i put something in EditText Artist and i'll press ADD ARTIST i want that it will be added to the last added GENRE item in the RecyclerView.


EditText


buttons


edittext


RecyclerView


EditText


RecyclerView



Here is my code from my MainActivity.


public class MainActivity extends AppCompatActivity {

public GenreAdapter adapter;

List<Genre> parent;
EditText artistTXT;
EditText genreTXT;
List<Artist> child;
Button addART;
Button addGENR;

RecyclerView mRecyclerView;


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

artistTXT = findViewById(R.id.ArtistTXT);
genreTXT = findViewById(R.id.GenreTXT);

addART = findViewById(R.id.AddArtist);
addGENR = findViewById(R.id.AddGenre);



child = new ArrayList<>();
parent = new ArrayList<>();

addART.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

adapter.addChild(new Artist(artistTXT.getText().toString()));

}
});


addGENR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

adapter.addGroup(new Genre(genreTXT.getText().toString(),null));

}
});


Button clear = findViewById(R.id.toggle_button);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {



}
});

BuildRecyclerView();

}

public void BuildRecyclerView(){
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
GenreAdapter adapter = new GenreAdapter(parent,this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(adapter);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
adapter.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
adapter.onRestoreInstanceState(savedInstanceState);
}

}



Here is my Adapter code



public class GenreAdapter extends ExpandableRecyclerViewAdapter {


Context context ;
LayoutInflater inflater ;
List<Genre> genres;

public GenreAdapter(List<? extends ExpandableGroup> groups, Context context) {
super(groups);
this.context = context ;
inflater = LayoutInflater.from(context);
}

@Override
public GenreViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item_genre, parent, false);
return new GenreViewHolder(view);
}

@Override
public ArtistViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item_artist, parent, false);
return new ArtistViewHolder(view);
}

@Override
public void onBindChildViewHolder(ArtistViewHolder holder, int flatPosition,
ExpandableGroup group, int childIndex) {

final Artist artist = (Artist) group.getItems().get(childIndex);
holder.setArtistName(artist.getName());
}

@Override
public void onBindGroupViewHolder(GenreViewHolder holder, int flatPosition,
ExpandableGroup group) {

holder.setGenreTitle(group);
}


public void addChild(Artist child){
// I assume the 'groups' list is accessible in your Adapter
Genre group = genres.get(genres.size());
group.getItems().add(child);
notifyDataSetChanged();
}

public void addGroup(Genre group){
genres.add(group);
notifyDataSetChanged();
}



}




1 Answer
1



This answer doesn't work, see edit below



You have a list of Group containing Child.



So if you want to add a Child to the last existing Group just do (in your Adapter):


public void addChild(YourChildType child){
// I assume the 'groups' list is accessible in your Adapter
// Retrieve the last group (it's a basic implementation, you may want to check if the list is not empty to prevent an OutOfBoundsException)
GroupType group = groups.get(groups.size());
group.getItems().add(child);
notifyDataSetChanged();
}



If you want to add a new group:


public void addGroup(GroupType group){
groups.add(group);
notifyDataSetChanged(
}



EDIT: updated answer



It turned out it's not possible to change the dataset (yet) once the adapter has been created (see the response from the author here)



So you need to store your list of Genre in MainActivity. Every time you want to update the list you should do:


Genre


MainActivity


List<Genre> mGenres;
GenreAdapter mAdapter;

public void addGenre(Genre genre){
mGenres.add(genre);
mAdapter = new GenreAdapter(genres, ...);
mRecyclerView.setAdapter(mAdapter);
}

public void removeGenre(Genre genre){
mGenres.remove(genre);
mAdapter = new GenreAdapter(genres, ...);
mRecyclerView.setAdapter(mAdapter);
}





By adding addChild in my Adapter i'm getting an error, i've added the code in the question can you check it?
– JohnKarry
Jul 2 at 9:40





What is the error?
– Eselfar
Jul 2 at 9:41





"There is no default constructor available in 'com.thoughtbot...)" i've added it in my Question
– JohnKarry
Jul 2 at 9:42





it's because it should be public void addChild. I missed the void so addChild is considered as a constructor. i've updated my answer
– Eselfar
Jul 2 at 9:43



public void addChild


void





I've updated my MainActivity with the addChild and addGroup but when i'm trying to add the Group the app Crash with "addGroup, attempt to add method on a null object reference" :l
– JohnKarry
Jul 2 at 9:53






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

up7Gq qcq,oN p,6MbxgDm2m,rm19zhumWCOE1aNwKYE2UriOaG2Ea,iYjG
2m6QtRoZSJtMs6PTHqZ6Lxd

Popular posts from this blog

Rothschild family

Cinema of Italy