错误:%android.database.cursorindexoutofboundsexception:index 10要求,大小为10%
这是完整的代码。因此,问题是,如果表中有9个记录,并且单击ButtonNext,该应用程序不会崩溃并显示消息“这是最后一页”,并且是否有11个记录通过单击ButtonNext,该应用程序也不会崩溃并转到第二页,但是如果有10个记录,并且单击ButtonNext,该应用程序不会显示消息“这是最后一页”并崩溃。因此,谢谢您的帮助,我非常算在内。
public class TableDemos extends AppCompatActivity implements View.OnClickListener {
final Context context = this;
SQLiteHelper sqLiteHelper = new SQLiteHelper(this);
int i;
TableLayout tabledata;
private TableRow tablerow;
View view;
Button buttonAdd;
Button buttonNext;
Button buttonPrev;
TextView tv_PageCount;
Spinner select_table;
TextView tv;
ScrollView mainscrollview;
TableRow.LayoutParams tableRowParams;
static class indexInfo {
public static int index = 10;
public static int numberofpages = 0;
public static int currentpage = 0;
public static String table_name="";
public static Cursor maincursor;
public static int cursorpostion=0;
public static ArrayList<String> value_string;
public static ArrayList<String> tableheadernames;
public static ArrayList<String> emptytablecolumnnames;
public static boolean isEmpty;
public static boolean isCustomQuery;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.elevage_activity);
tabledata = (TableLayout) findViewById(R.id.table_data);
buttonAdd = (Button) findViewById(R.id.buttonAdd_data);
buttonAdd.setOnClickListener(this);
buttonAdd.setBackgroundColor(Color.parseColor("#4c9a59"));
buttonNext = (Button) findViewById(R.id.btnNext);
buttonNext.setOnClickListener(this);
buttonPrev = (Button) findViewById(R.id.btnPrevious);
buttonPrev.setOnClickListener(this);
tv_PageCount = (TextView) findViewById(R.id.txtPageCount);
tabledata.setColumnStretchable(0, true);
tabledata.setColumnStretchable(2, true);
tabledata.setStretchAllColumns(true);
tabledata.bringToFront();
int d =0;
Cursor c3=null;
tabledata.removeAllViews();
if(d==0)
{
String Query1 = " SELECT * FROM lespontes";
ArrayList<Cursor> alc3=sqLiteHelper.getData(Query1);
c3= alc3.get(0);
indexInfo.maincursor=c3;
}
if(d==1)
{
c3=indexInfo.maincursor;
}
if(c3!=null)
{
int counts = c3.getCount();
Log.d("counts",""+counts);
tv_PageCount.setText("Page "+(d+1+" of " +counts));
TableRow tableheader = new TableRow(getApplicationContext());
tableheader.setPadding(0, 2, 0, 2);
tableheader.setLayoutParams( new TableLayout.LayoutParams(TableLayout.
LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for(int k=0;k<c3.getColumnCount();k++)
{
TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f);
LinearLayout cell = new LinearLayout(TableDemos.this);
cell.setBackgroundColor(Color.parseColor("#87CEEB"));
cell.setLayoutParams(tableRowParams);
final TextView tableheadercolums = new TextView(getApplicationContext());
tableheadercolums.setPadding(0, 0, 4, 3);
tableheadercolums.setText(""+c3.getColumnName(k));
tableheadercolums.setTextColor(Color.parseColor("#000000"));
cell.addView(tableheadercolums);
tableheader.addView(cell);
}
tabledata.addView(tableheader);
c3.moveToFirst();
paginatetable(c3.getCount());
}
else{
TableRow tableheader2 = new TableRow(getApplicationContext());
tableheader2.setBackgroundColor(Color.BLACK);
tableheader2.setPadding(0, 2, 0, 2);
LinearLayout cell = new LinearLayout(TableDemos.this);
cell.setBackgroundColor(Color.parseColor("#87CEEB"));
cell.setLayoutParams(tableRowParams);
final TextView tableheadercolums = new TextView(getApplicationContext());
tableheadercolums.setPadding(0, 0, 4, 3);
tableheadercolums.setText(" Table Is Empty ");
tableheadercolums.setTextSize(30);
tableheadercolums.setTextColor(Color.RED);
cell.addView(tableheadercolums);
tableheader2.addView(cell);
tabledata.addView(tableheader2);
tv_PageCount.setText(""+0);
}
}
public void paginatetable(final int number)
{
final Cursor c3 = indexInfo.maincursor;
indexInfo.numberofpages=(c3.getCount()/10)+1;
indexInfo.currentpage=1;
c3.moveToFirst();
int currentrow=0;
do
{
final TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setBackgroundColor(Color.BLACK);
tableRow.setPadding(0, 2, 0, 2);
for(int j=0 ;j<c3.getColumnCount();j++)
{
TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f);
LinearLayout cell = new LinearLayout(this);
cell.setLayoutParams(tableRowParams);
if (j % 2 == 0){
cell.setBackgroundColor(Color.parseColor("#FFFAF0"));
} else {
cell.setBackgroundColor(Color.parseColor("#FAF0E6"));
}
final TextView columsView = new TextView(getApplicationContext());
String column_data = "";
try{
column_data = c3.getString(j);
}catch(Exception e){
}
columsView.setText(column_data);
columsView.setTextColor(Color.parseColor("#000000"));
columsView.setPadding(0, 0, 4, 3);
cell.addView(columsView);
tableRow.addView(cell);
}
tableRow.setVisibility(View.VISIBLE);
currentrow=currentrow+1;
tableRow.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
int position=0;
final ArrayList<String> value_string = new ArrayList<String>();
for(int i=0;i<c3.getColumnCount();i++)
{
LinearLayout llcolumn = (LinearLayout) tableRow.getChildAt(i);
TextView tc =(TextView)llcolumn.getChildAt(0);
String cv =tc.getText().toString();
value_string.add(cv);
}
indexInfo.value_string=value_string;
}
});
tabledata.addView(tableRow);
}while(c3.moveToNext()&¤trow <10);
indexInfo.index=currentrow;
buttonPrev.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{ int tobestartindex=(indexInfo.currentpage-2)*10;
if(indexInfo.currentpage==1)
{ Toast.makeText(getApplicationContext(),
"This is the first page",Toast.LENGTH_LONG).show();}
else { indexInfo.currentpage=indexInfo.currentpage-1;
c3.moveToPosition(tobestartindex);
boolean decider=true;
for(int i=1;i<tabledata.getChildCount();i++)
{ TableRow tableRow = (TableRow) tabledata.getChildAt(i);
if(decider)
{tableRow.setVisibility(View.VISIBLE);
for(int j=0;j<tableRow.getChildCount();j++)
{ LinearLayout llcolumn = (LinearLayout) tableRow.getChildAt(j);
TextView columsView = (TextView) llcolumn.getChildAt(0);
columsView.setText(""+c3.getString(j));
}
decider=!c3.isLast();
if(!c3.isLast()){c3.moveToNext();}
}
else
{
tableRow.setVisibility(View.GONE);
}
}
indexInfo.index=tobestartindex;
Log.d("index =",""+indexInfo.index);
}
}
});
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{if(indexInfo.currentpage>=indexInfo.numberofpages)
{ Toast.makeText(getApplicationContext(),
"This is the last page", Toast.LENGTH_LONG).show(); }
else
{ indexInfo.currentpage=indexInfo.currentpage+1;
boolean decider=true;
for(int i=1;i<tabledata.getChildCount();i++)
{ TableRow tableRow = (TableRow) tabledata.getChildAt(i);
if(decider)
{tableRow.setVisibility(View.VISIBLE);
for(int j=0;j<tableRow.getChildCount();j++)
{ LinearLayout llcolumn = (LinearLayout) tableRow.getChildAt(j);
TextView columsView =(TextView)llcolumn.getChildAt(0);
columsView.setText(""+c3.getString(j));
}
decider=!c3.isLast();
if(!c3.isLast()){c3.moveToNext();}
}
else
{
tableRow.setVisibility(View.GONE);
}
}
}
}
});
}
@Override
public void onClick(View view) { }
}
Here is the complete code.. So the problem is that if there are 9 records in the table and by clicking on the ButtonNext , the application does not crash and displays the message "This is the last page", and if there are 11 records and by clicking on the ButtonNext , the application does not crash too and goes to the second page, but if there are 10 records and by clicking on the ButtonNext the application does not display the message " This is the last page" and crash. So thank you for your help I count a lot.
public class TableDemos extends AppCompatActivity implements View.OnClickListener {
final Context context = this;
SQLiteHelper sqLiteHelper = new SQLiteHelper(this);
int i;
TableLayout tabledata;
private TableRow tablerow;
View view;
Button buttonAdd;
Button buttonNext;
Button buttonPrev;
TextView tv_PageCount;
Spinner select_table;
TextView tv;
ScrollView mainscrollview;
TableRow.LayoutParams tableRowParams;
static class indexInfo {
public static int index = 10;
public static int numberofpages = 0;
public static int currentpage = 0;
public static String table_name="";
public static Cursor maincursor;
public static int cursorpostion=0;
public static ArrayList<String> value_string;
public static ArrayList<String> tableheadernames;
public static ArrayList<String> emptytablecolumnnames;
public static boolean isEmpty;
public static boolean isCustomQuery;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.elevage_activity);
tabledata = (TableLayout) findViewById(R.id.table_data);
buttonAdd = (Button) findViewById(R.id.buttonAdd_data);
buttonAdd.setOnClickListener(this);
buttonAdd.setBackgroundColor(Color.parseColor("#4c9a59"));
buttonNext = (Button) findViewById(R.id.btnNext);
buttonNext.setOnClickListener(this);
buttonPrev = (Button) findViewById(R.id.btnPrevious);
buttonPrev.setOnClickListener(this);
tv_PageCount = (TextView) findViewById(R.id.txtPageCount);
tabledata.setColumnStretchable(0, true);
tabledata.setColumnStretchable(2, true);
tabledata.setStretchAllColumns(true);
tabledata.bringToFront();
int d =0;
Cursor c3=null;
tabledata.removeAllViews();
if(d==0)
{
String Query1 = " SELECT * FROM lespontes";
ArrayList<Cursor> alc3=sqLiteHelper.getData(Query1);
c3= alc3.get(0);
indexInfo.maincursor=c3;
}
if(d==1)
{
c3=indexInfo.maincursor;
}
if(c3!=null)
{
int counts = c3.getCount();
Log.d("counts",""+counts);
tv_PageCount.setText("Page "+(d+1+" of " +counts));
TableRow tableheader = new TableRow(getApplicationContext());
tableheader.setPadding(0, 2, 0, 2);
tableheader.setLayoutParams( new TableLayout.LayoutParams(TableLayout.
LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for(int k=0;k<c3.getColumnCount();k++)
{
TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f);
LinearLayout cell = new LinearLayout(TableDemos.this);
cell.setBackgroundColor(Color.parseColor("#87CEEB"));
cell.setLayoutParams(tableRowParams);
final TextView tableheadercolums = new TextView(getApplicationContext());
tableheadercolums.setPadding(0, 0, 4, 3);
tableheadercolums.setText(""+c3.getColumnName(k));
tableheadercolums.setTextColor(Color.parseColor("#000000"));
cell.addView(tableheadercolums);
tableheader.addView(cell);
}
tabledata.addView(tableheader);
c3.moveToFirst();
paginatetable(c3.getCount());
}
else{
TableRow tableheader2 = new TableRow(getApplicationContext());
tableheader2.setBackgroundColor(Color.BLACK);
tableheader2.setPadding(0, 2, 0, 2);
LinearLayout cell = new LinearLayout(TableDemos.this);
cell.setBackgroundColor(Color.parseColor("#87CEEB"));
cell.setLayoutParams(tableRowParams);
final TextView tableheadercolums = new TextView(getApplicationContext());
tableheadercolums.setPadding(0, 0, 4, 3);
tableheadercolums.setText(" Table Is Empty ");
tableheadercolums.setTextSize(30);
tableheadercolums.setTextColor(Color.RED);
cell.addView(tableheadercolums);
tableheader2.addView(cell);
tabledata.addView(tableheader2);
tv_PageCount.setText(""+0);
}
}
public void paginatetable(final int number)
{
final Cursor c3 = indexInfo.maincursor;
indexInfo.numberofpages=(c3.getCount()/10)+1;
indexInfo.currentpage=1;
c3.moveToFirst();
int currentrow=0;
do
{
final TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setBackgroundColor(Color.BLACK);
tableRow.setPadding(0, 2, 0, 2);
for(int j=0 ;j<c3.getColumnCount();j++)
{
TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams (TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT,1.0f);
LinearLayout cell = new LinearLayout(this);
cell.setLayoutParams(tableRowParams);
if (j % 2 == 0){
cell.setBackgroundColor(Color.parseColor("#FFFAF0"));
} else {
cell.setBackgroundColor(Color.parseColor("#FAF0E6"));
}
final TextView columsView = new TextView(getApplicationContext());
String column_data = "";
try{
column_data = c3.getString(j);
}catch(Exception e){
}
columsView.setText(column_data);
columsView.setTextColor(Color.parseColor("#000000"));
columsView.setPadding(0, 0, 4, 3);
cell.addView(columsView);
tableRow.addView(cell);
}
tableRow.setVisibility(View.VISIBLE);
currentrow=currentrow+1;
tableRow.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
int position=0;
final ArrayList<String> value_string = new ArrayList<String>();
for(int i=0;i<c3.getColumnCount();i++)
{
LinearLayout llcolumn = (LinearLayout) tableRow.getChildAt(i);
TextView tc =(TextView)llcolumn.getChildAt(0);
String cv =tc.getText().toString();
value_string.add(cv);
}
indexInfo.value_string=value_string;
}
});
tabledata.addView(tableRow);
}while(c3.moveToNext()&¤trow <10);
indexInfo.index=currentrow;
buttonPrev.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{ int tobestartindex=(indexInfo.currentpage-2)*10;
if(indexInfo.currentpage==1)
{ Toast.makeText(getApplicationContext(),
"This is the first page",Toast.LENGTH_LONG).show();}
else { indexInfo.currentpage=indexInfo.currentpage-1;
c3.moveToPosition(tobestartindex);
boolean decider=true;
for(int i=1;i<tabledata.getChildCount();i++)
{ TableRow tableRow = (TableRow) tabledata.getChildAt(i);
if(decider)
{tableRow.setVisibility(View.VISIBLE);
for(int j=0;j<tableRow.getChildCount();j++)
{ LinearLayout llcolumn = (LinearLayout) tableRow.getChildAt(j);
TextView columsView = (TextView) llcolumn.getChildAt(0);
columsView.setText(""+c3.getString(j));
}
decider=!c3.isLast();
if(!c3.isLast()){c3.moveToNext();}
}
else
{
tableRow.setVisibility(View.GONE);
}
}
indexInfo.index=tobestartindex;
Log.d("index =",""+indexInfo.index);
}
}
});
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{if(indexInfo.currentpage>=indexInfo.numberofpages)
{ Toast.makeText(getApplicationContext(),
"This is the last page", Toast.LENGTH_LONG).show(); }
else
{ indexInfo.currentpage=indexInfo.currentpage+1;
boolean decider=true;
for(int i=1;i<tabledata.getChildCount();i++)
{ TableRow tableRow = (TableRow) tabledata.getChildAt(i);
if(decider)
{tableRow.setVisibility(View.VISIBLE);
for(int j=0;j<tableRow.getChildCount();j++)
{ LinearLayout llcolumn = (LinearLayout) tableRow.getChildAt(j);
TextView columsView =(TextView)llcolumn.getChildAt(0);
columsView.setText(""+c3.getString(j));
}
decider=!c3.isLast();
if(!c3.isLast()){c3.moveToNext();}
}
else
{
tableRow.setVisibility(View.GONE);
}
}
}
}
});
}
@Override
public void onClick(View view) { }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论