我正在检索并显示文本列表,具体取决于点击/选择的按钮/类别。 我也试图让用户输入文本,并将保存在数据库中,并将显示在正在显示的活动/选定类别的所有当前文本的底部。但我遇到了问题我试图通过从数据库中获取元素的数量从父视图中删除所有子视图然后我试图将数据插入数据库,最后我试图在插入新的元素后获取所有元素并构建所有元素虽然这样做我得到了ArrayOutOfBound异常。我不知道为什么我会收到这个错误,以及这是否是一种有效的方法来实现这一点。请指导我。
public class Independent extends Activity implements OnClickListener {
public TextView[] mainText;
public LinearLayout[] textLayout;
public LinearLayout textLayoutContainer;
public ScrollView scrollView;
EditText tv;
int x, y, z;
public String catagory;
public Independent() {
catagory = "";
}// End of Constructor
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.catagories_list);
textLayoutContainer = (LinearLayout) findViewById(R.id.text_linearlayout);
scrollView = (ScrollView) findViewById(R.id.scrollView1);
Button addTextButton = (Button) findViewById(R.id.b_add);
Button editTextButton = (Button) findViewById(R.id.b_edit);
Button deleteTextButton = (Button) findViewById(R.id.b_delete);
addTextButton.setOnClickListener(this);
editTextButton.setOnClickListener(this);
deleteTextButton.setOnClickListener(this);
ImageButton generalButton = (ImageButton) findViewById(R.id.imageButton1);
ImageButton foodButton = (ImageButton) findViewById(R.id.imageButton2);
generalButton.setOnClickListener(this);
foodButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
if (catagory == "General") {
Dialog gend = new Dialog(Independent.this);
gend.setTitle("Message");
TextView tv2 = new TextView(Independent.this);
tv2.setText("General catagory is already selected.");
gend.setContentView(tv2);
gend.setCancelable(true);
gend.show();
} else {
catagory = "General";
IndependentDB genData = IndependentDB.getInstance();
genData.open(this);
ArrayList<TextHolder> genList = new ArrayList<TextHolder>();
genList = genData.getAllTextFromGenT();
genData.close();
x = genList.size();
String xstr = new StringBuilder().append(x).toString();
System.out.println(xstr);
mainText = new TextView[x];
textLayout = new LinearLayout[x];
if (!genList.isEmpty()) {
for (int i = 0; i < x; i++) {
TextHolder firstOne = genList.get(i);
String text = firstOne.getText();
System.out.println("2");
mainText[i] = new TextView(this);
System.out.println("3");
mainText[i].setId(i);
System.out.println("4");
mainText[i].setText("Text");
System.out.println("5");
mainText[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
System.out.println("6");
mainText[i].setTextSize(20);
System.out.println("7");
mainText[i].setGravity(Gravity.CENTER);
System.out.println("8");
mainText[i].setText(text);
System.out.println("9");
textLayout[i] = new LinearLayout(this);
System.out.println("10");
textLayout[i].setId(i);
System.out.println("11");
textLayout[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
System.out.println("12");
textLayout[i].setGravity(Gravity.CENTER);
System.out.println("13");
textLayout[i].addView(mainText[i]);
System.out.println("14");
textLayoutContainer.addView(textLayout[i]);
System.out.println("15");
}
}
}
break;
case R.id.b_add:
textLayoutContainer.removeAllViews();
tv = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please eneter the text below.");
builder.setCancelable(true);
builder.setView(tv);
builder.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
boolean Success = true;
Editable ttv = tv.getText();
final String stringttv = new StringBuilder()
.append(ttv).toString();
if (stringttv == "") {
Dialog mTag = new Dialog(Independent.this);
mTag.setTitle("Missing Text");
TextView tv2 = new TextView(Independent.this);
tv2.setText("Please enter the text you would like to add.");
mTag.setContentView(tv2);
mTag.setCancelable(true);
mTag.show();
} else {
try {
if (catagory == "General") {
//Clearing View
IndependentDB idb0 = IndependentDB
.getInstance();
idb0.open(Independent.this);
ArrayList<TextHolder> gentList = new ArrayList<TextHolder>();
gentList = idb0.getAllTextFromGenT();
int v = gentList.size();
for (int i = 0; i < v; i++) {
textLayout[i].removeAllViews();
}// End of for loop
idb0.close();
//Inserting new text
IndependentDB idb1 = IndependentDB
.getInstance();
idb1.open(Independent.this);
idb1.insertEventInGenT(stringttv);
idb1.close();
//Getting the updated DB values/text
IndependentDB idb2 = IndependentDB
.getInstance();
idb2.open(Independent.this);
//iddb.insertEventInGenT(stringttv);
ArrayList<TextHolder> genTList = new ArrayList<TextHolder>();
genTList = idb2.getAllTextFromGenT();
System.out.println("check1");
int w = genTList.size();
System.out.println("check2");
if (!genTList.isEmpty()) {
for (int i = 0; i < w ; i++) {
TextHolder firstOne = genTList
.get(i);
System.out.println("check3");
String text = firstOne
.getText();
System.out.println("check4");
mainText[i] = new TextView(
Independent.this);
mainText[i].setId(i);
mainText[i].setText("Text");
mainText[i]
.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mainText[i].setTextSize(20);
mainText[i]
.setGravity(Gravity.CENTER);
mainText[i].setText(text);
textLayout[i] = new LinearLayout(
Independent.this);
textLayout[i].setId(i);
textLayout[i]
.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout[i]
.setGravity(Gravity.CENTER);
textLayout[i]
.addView(mainText[i]);
textLayoutContainer
.addView(textLayout[i]);
}// End of for loop
}// End of If statement
idb2.close();
}
}catch (Exception e) {
Success = false;
String error = e.toString();
Dialog d = new Dialog(Independent.this);
d.setTitle("Error");
TextView tv = new TextView(Independent.this);
tv.setText(error);
d.setContentView(tv);
d.show();
} finally {
}
}
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
case R.id.b_edit:
break;
case R.id.b_delete:
break;
}
}
}
很抱歉之前没有发布堆栈跟踪。现在我删除了以下代码行:
IndependentDB idb0 = IndependentDB
.getInstance();
idb0.open(Independent.this);
ArrayList<TextHolder> gentList = new ArrayList<TextHolder>();
gentList = idb0.getAllTextFromGenT();
int v = gentList.size();
for (int i = 0; i < v; i++) {
textLayout[i].removeAllViews();
}// End of for loop
idb0.close();
我不再获得ArrayOutOfBound Exception了,但我仍然无法检索最后插入的值。
在最后四行logcat消息中显示堆栈跟踪消息:
01-17 14:48:55.617: I/System.out(408): check3
01-17 14:48:55.617: I/System.out(408): check4
01-17 14:48:55.628: I/System.out(408): null null [Ljava.lang.StackTraceElement;@44f9cd28
01-17 14:48:56.309: D/dalvikvm(304): GC_EXPLICIT freed 208 objects / 9952 bytes in 766ms
01-17 14:49:05.149: D/dalvikvm(134): GC_EXPLICIT freed 719 objects / 40696 bytes in 77ms
答案 0 :(得分:0)
解决了这个问题。 我正在将新添加的值插入到数据库中,但不依赖于数据库来检索和显示它,但我创建了新的textview,我直接从edittext获取文本并将其设置为我创建并显示它的新textview。它工作了:)