I have a table layout where rows are dynamically added. I want 30 data to be shown in one page.pages should be automatically added with increasing data. This is my code.
int increment = 0;
public int TOTAL_LIST_ITEMS;
public int NUM_ITEMS_PAGE = 50;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.frag_tab1, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tableLayout = (TableLayout) view.findViewById(R.id.tableLay);
buttonNext = (Button) view.findViewById(R.id.next);
listView = (ListView) view.findViewById(R.id.list);
title = (TextView) view.findViewById(R.id.text);
buttonPrev = (Button) view.findViewById(R.id.prev);
buttonPrev.setEnabled(false);
// data = new ArrayList<String>();
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
increment++;
loadList(increment);
Toast.makeText(getActivity(),"the page is"+increment,Toast.LENGTH_LONG).show();
checkEnable();
}
});
buttonPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
increment--;
loadList(increment);
Toast.makeText(getActivity(),"the page is"+increment,Toast.LENGTH_LONG).show();
checkEnable();
}
});
db = getActivity().openOrCreateDatabase("hitechData.db", Context.MODE_PRIVATE, null);
DBHelper dh = new DBHelper(getActivity(), "hitechData.db", null, 1);
tableLayout.setColumnStretchable(0, true);
tableLayout.setColumnStretchable(2, true);
tableLayout.setStretchAllColumns(true);
tableLayout.bringToFront();
db = getActivity().openOrCreateDatabase("hitechData.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
dh.onCreate(db);
db.setLockingEnabled(true);
String query = "SELECT * FROM leadData3 Order by id DESC";
Cursor cur;
cur = db.rawQuery(query, null);
TOTAL_LIST_ITEMS = cur.getCount();
data=TOTAL_LIST_ITEMS;
int val = TOTAL_LIST_ITEMS % NUM_ITEMS_PAGE;
val = val == 0?0:1;
pageCount = TOTAL_LIST_ITEMS / NUM_ITEMS_PAGE;
if (cur != null && cur.getCount() > 0) {
if (cur.moveToFirst()) {
try {
while (cur.isAfterLast() == false ){
TableRow tr = new TableRow(getActivity().getBaseContext());
final TextView compName = new TextView(getActivity().getBaseContext());
compName.setBackgroundResource(R.drawable.borderr);
final TextView firstName = new TextView(getActivity().getBaseContext());
firstName.setBackgroundResource(R.drawable.borderr);
final TextView midName = new TextView(getActivity().getBaseContext());
midName.setBackgroundResource(R.drawable.borderr);
final TextView lastName = new TextView(getActivity().getBaseContext());
lastName.setBackgroundResource(R.drawable.borderr);
compName.append(cur.getString(1));
compName.setTextColor(this.getResources().getColor(R.color.colorPrimary));
compName.setPadding(30, 0, 0, 0);
firstName.append(cur.getString(21));
firstName.setTextColor(this.getResources().getColor(R.color.colorPrimary));
firstName.setPadding(30, 0, 0, 0);
midName.append(cur.getString(22));
midName.setTextColor(this.getResources().getColor(R.color.colorPrimary));
midName.setPadding(30, 0, 0, 0);
lastName.append(cur.getString(23));
lastName.setTextColor(this.getResources().getColor(R.color.colorPrimary));
lastName.setPadding(30, 0, 0, 0);
// String name =firstName.getText().toString()+midName.getText().toString()+lastName.getText().toString();
firstName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nameValue = firstName.getText().toString();
String companyValue = compName.getText().toString();
Intent intent = new Intent(getActivity().getApplicationContext(), ShowDetailData.class);
intent.putExtra("name", nameValue);
intent.putExtra("accNo", companyValue);
Toast.makeText(getActivity().getApplicationContext(), nameValue, Toast.LENGTH_LONG).show();
startActivity(intent);
}
});
tr.addView(firstName);
tr.addView(compName);
tableLayout.addView(tr);
cur.moveToNext();
/* count++;
nextPage++;
Toast.makeText(getActivity().getApplicationContext(), "count is : " + count, Toast.LENGTH_LONG).show();*/
//cur.close();
}
} finally {
cur.close();
db.close();
}
} else {
Toast.makeText(getActivity().getApplicationContext(), "no Data", Toast.LENGTH_LONG).show();
}
}
}
/*
*/
/*
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextPage++;
Toast.makeText(getActivity(), "next page is"+nextPage, Toast.LENGTH_SHORT).show();
}
});*/
private void loadList(int number){
ArrayList<String> sort = new ArrayList<String >();
title.setText("Page "+(number+1+" of " +pageCount));
int start = number * NUM_ITEMS_PAGE;
for (int i=start; i<(start)+NUM_ITEMS_PAGE; i++)
{
if(i<data)
{
sort.add(String.valueOf(data));
}
else {
break;
}
// adapter = new ArrayAdapter<String>(getActivity(), //android.R.layout.simple_list_item_1,sort);
// tablerow.setAdapter(adapter);
//implement table row in adapter
}}
private void checkEnable(){
if(increment+1 == pageCount)
{
buttonNext.setEnabled(false);
}
else if(increment == 0){
buttonPrev.setEnabled(false);
}
else {
buttonPrev.setEnabled(true);
buttonNext.setEnabled(true);
}
}
}
I want to know how can i change the page after no. of rows exceeds 30. I am unable to do pagination. Currently i am using Sqlite and all the filled data is appearing on the first page. I want to divide the data and want it appear in set of 30 after click of previous and next.