2012-01-17 2 views
-2

클릭/선택에 따라 텍스트 목록을 검색하여 표시하고 있습니다. 나는 또한 사용자가 텍스트를 입력 할 수 있도록 노력하고있어 그리고 그 데이터베이스에 저장되며 현재/선택된 catagory thats의 현재 텍스트의 하단에 표시됩니다.하지만 문제가 있어요. 이 부분에서. 부모 뷰에서 데이터베이스에서 요소 수를 가져 와서 모든 자식 뷰를 제거한 다음 데이터베이스에 데이터를 삽입하고 마지막으로 새 요소를 삽입 한 후 모든 요소를 ​​가져 오려고합니다. 모든 자식 뷰.이 작업을하는 동안 ArrayOutOfBound 예외가 발생합니다.이 오류가 발생하는 이유와 이것이 효율적인 수행 방법인지 여부가 확실하지 않습니다. 제발 안내하십시오.android에서 데이터베이스의 업데이트 된 값을 표시 할 수 없습니다.

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; 
    } 
} 

}

미안 코드의 다음 줄을 제거 before.Now 스택 추적을 게시하지 않는 :

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 예외를받지 못했습니다을하지만 난 여전히 해요 마지막으로 삽입 된 값을 검색 할 수 없습니다.

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 
+3

그래서 예외가 있으며,이 거대한 코드에서 어디에서 발생했는지 알아야합니다. 최소한 스택 추적을 제공하고 코드의 어느 줄이 예외를 발생시키는 줄인지 알려줍니다. –

+0

Stack trace before.I를 게시하지 않아서 죄송합니다. 내 질문을 편집하고 스택 추적을 추가했습니다. 또한 일부 변경 작업을 수행했지만 ArrayIndexOutOfBound 예외는 사라졌지만 여전히 새로 추가 된 마지막 값을 검색하고 표시 할 수 없습니다. – Viking

답변

0

이 문제를 해결 : 스택 추적 메시지가 나타났다 로그 캣 메시지의 마지막 4 개 개의 라인에 따라

. 새로 추가 된 값을 데이터베이스에 삽입하고 있지만 데이터베이스에 따라 검색하고 표시하지는 않지만 새로운 textview를 만들었고 edittext에서 텍스트를 직접 가져 와서 그것을 만든 새 텍스트 뷰로 설정했습니다. .it worked :)

관련 문제