I'm trying to make a pdf book app with expandable list view and custom adapter. I've numbers of different pdf files in assets for different topics. I've made previous and next button layout in pdf view and I want those buttons to open previous and next pdf file. I'm a new learner so please help me with this. Here is how it looks:
Fullview activity-
String fileName;
Intent intent;
PDFView pdfView;
Button nbutton,pbutton;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_view);
pdfView=(PDFView)findViewById(R.id.pdfView);
pbutton=findViewById(R.id.pbutton);
nbutton=findViewById(R.id.nbutton);
intent=getIntent();
fileName=intent.getStringExtra("fileName");
pdfView.fromAsset(fileName+".pdf").load();
}
});
}
Main Activity -
ExpandableListView expandableListView;
CustomAdapter customAdapter;
List<Cover>coverList;
List<Topics>topicsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView=(ExpandableListView)findViewById(R.id.expandableListView);
addData();
}
void addData()
{
coverList=new ArrayList<>();
topicsList=new ArrayList<>();
topicsList.add(new Topics("1.","pdf1"));
topicsList.add(new Topics("2.","pdf2"));
topicsList.add(new Topics("3.","pdf3"));
topicsList.add(new Topics("4.","pdf4"));
coverList.add(new Cover("Cover 1",topicsList));
topicsList=new ArrayList<>();
topicsList.add(new Topics("1.","pdf5"));
topicsList.add(new Topics("2.","pdf6"));
topicsList.add(new Topics("3.","pdf7"));
topicsList.add(new Topics("4.","pdf8"));
topicsList.add(new Topics("5.","pdf9"));
topicsList.add(new Topics("6.","pdf10"));
coverList.add(new Cover("Cover 2",topicsList));
sendData();
}
void sendData()
{
customAdapter=new CustomAdapter(coverList, MainActivity.this);
expandableListView.setAdapter(customAdapter);
}
& Custom Adapter :
public class CustomAdapter extends BaseExpandableListAdapter {
List<Cover>coverList;
Context context;
public CustomAdapter(List<Cover> coverList, Context context) {
this.coverList = coverList;
this.context = context;
}
//parent size count
@Override
public int getGroupCount() {
return coverList.size();
}
@Override
public int getChildrenCount(int i) {
return coverList.get(i).getTopicsList().size();
}
//position set
@Override
public Object getGroup(int i) {
return coverList.get(i);
}
@Override
public Object getChild(int i, int i1) {
return coverList.get(i).getTopicsList().get(i1);
}
//id
@Override
public long getGroupId(int i) {
return (i);
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewgroup) {
view= LayoutInflater.from(context).inflate(R.layout.cover_item,viewgroup, false);
TextView coverName=(TextView)view.findViewById(R.id.coverTitle);
coverName.setText(coverList.get(i).getCoverName());
ImageView imageView=(ImageView)view.findViewById(R.id.arrow);
if (b==true)
{
imageView.setRotation(90);
}
else if (b==false)
{
imageView.setRotation(0);
}
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewgroup) {
view= LayoutInflater.from(context).inflate(R.layout.topic_item,viewgroup, false);
TextView topicName=(TextView)view.findViewById(R.id.topicTitle);
topicName.setText(coverList.get(i).getTopicsList().get(i1).getTopicName());
CardView cardView=(CardView)view.findViewById(R.id.topicClick);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =(new Intent(context, FullView.class));
intent.putExtra( "fileName",coverList.get(i).getTopicsList().get(i1).getFileName());
context.startActivity(intent);
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
Where is your button click listeners? I am answering on the basis of understanding of your question.
You have to change the
addData()or you can use a counter to handle next and previous button. Let sayonNext()increment counter by 1 andonPrevious()decrement counter by 1. This way you can use this counter as the index of the array to get the current pdfs.I hope this is what you were asking. If not then share the button click handling code.