无法从Android中显示数据库中的更新的值 [英] Not able to display updated values from the database in android

查看:104
本文介绍了无法从Android中显示数据库中的更新的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检索和显示依赖于任何按钮文本列表/产品类别被点击/选中。
我也试图让用户输入文字,并且将被保存在数据库中,并会在活动/选择产品类别这就是被displayed.B​​ut我遇到一个问题的所有当前文本的底部显示这part.I试图通过获取从数据库中元素的个数去除父视图中的所有子视图,然后我试图将数据插入到数据库,最后我想要得到的所有元素插入新的后建立所有孩子view.While这样做,我得到ArrayOutOfBound Exception.I'm不知道为什么我得到这个错误以及是否这是实现this.Please指导我的有效途径。

 公共类独立扩展活动实现OnClickListener {
公众的TextView []的mainText;
公众的LinearLayout [] TextLayout的;
公众的LinearLayout textLayoutContainer;
公共滚动型滚动视图;
的EditText电视;
INT X,Y,Z;
公共字符串产品类别;公共独立(){
    产品类别=;
} //构造函数结束@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.catagories_list);
    textLayoutContainer =(的LinearLayout)findViewById(R.id.text_linearlayout);
    滚动视图=(滚动型)findViewById(R.id.scrollView1);
    按钮addTextButton =(按钮)findViewById(R.id.b_add);
    按钮editTextButton =(按钮)findViewById(R.id.b_edit);
    按钮deleteTextButton =(按钮)findViewById(R.id.b_delete);
    addTextButton.setOnClickListener(本);
    editTextButton.setOnClickListener(本);
    deleteTextButton.setOnClickListener(本);
    的ImageButton generalButton =(的ImageButton)findViewById(R.id.imageButton1);
    的ImageButton foodButton =(的ImageButton)findViewById(R.id.imageButton2);
    generalButton.setOnClickListener(本);
    foodButton.setOnClickListener(本);}@覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.imageButton1:
        如果(产品类别==常规){
            对话GEND =新的对话框(Independent.this);
            gend.setTitle(信息);
            TextView中TV2 =新的TextView(Independent.this);
            tv2.setText(常规产品类别已经被选中。);
            gend.setContentView(TV2);
            gend.setCancelable(真);
            gend.show();
        }其他{
            产品类别=一般;
            IndependentDB genData = IndependentDB.getInstance();
            genData.open(本);
            ArrayList的< TextHolder> genList =新的ArrayList< TextHolder>();
            genList = genData.getAllTextFromGenT();
            genData.close();
            X = genList.size();
            字符串XSTR =新的StringBuilder()追加(X)的ToString()。
            的System.out.println(XSTR);
            的mainText =新的TextView [X];
            TextLayout的=新的LinearLayout [X];
            如果(!genList.isEmpty()){
                的for(int i = 0; I< X,我++){
                    TextHolder firstOne = genList.get(ⅰ);
                    字符串文本= firstOne.getText();
                    的System.out.println(2);
                    的mainText [I] =新的TextView(本);
                    的System.out.println(3);
                    的mainText [I] .setId(ⅰ);
                    的System.out.println(4);
                    的mainText [I] .setText(文字);
                    的System.out.println(5);
                    的mainText [I] .setLayoutParams(新的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(文本);
                    的System.out.println(9);
                    TextLayout的[I] =新的LinearLayout(本);
                    的System.out.println(10);
                    TextLayout的[I] .setId(ⅰ);
                    的System.out.println(11);
                    TextLayout的[I] .setLayoutParams(新的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);
                }
            }
        }
        打破;
案例R.id.b_add:
    textLayoutContainer.removeAllViews();
        电视=新的EditText(本);
        AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setMessage(请eneter下面的文字。);
        builder.setCancelable(真);
        builder.setView(电视);
        builder.setPositiveButton(保存,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释的id){
                        布尔成功=真实的;
                        可编辑TTV = tv.getText();
                        最后弦乐stringttv =新的StringBuilder()
                                .append(TTV)的ToString();
                        如果(stringttv ==){
                            对话MTAG =新的对话框(Independent.this);
                            mTag.setTitle(缺少文本);
                            TextView中TV2 =新的TextView(Independent.this);
                            tv2.setText(请输入你想添加的文字。);
                            mTag.setContentView(TV2);
                            mTag.setCancelable(真);
                            mTag.show();
                        }其他{
                            尝试{
                                如果(产品类别==常规){
                                    //清除浏览
                                    IndependentDB IDB0 = IndependentDB
                                            .getInstance();
                                    idb0.open(Independent.this);
                                    ArrayList的< TextHolder> gentList =新的ArrayList< TextHolder>();
                                    gentList = idb0.getAllTextFromGenT();
                                    INT V = gentList.size();
                                    的for(int i = 0; I<伏;我++){
                                        TextLayout的[I] .removeAllViews();
                                    } // for循环结束
                                    idb0.close();                                    //插入新的文本
                                    IndependentDB IDB1 = IndependentDB
                                            .getInstance();
                                    idb1.open(Independent.this);
                                    idb1.insertEventInGenT(stringttv);
                                    idb1.close();                                    //获取更新的DB值/文
                                    IndependentDB idb2 = IndependentDB
                                    .getInstance();
                                    idb2.open(Independent.this);
                                    //iddb.insertEventInGenT(stringttv);
                                    ArrayList的< TextHolder> genTList =新的ArrayList< TextHolder>();
                                    genTList = idb2.getAllTextFromGenT();
                                    的System.out.println(CHECK1);
                                    INT W = genTList.size();
                                    的System.out.println(CHECK2);
                                    如果(!genTList.isEmpty()){
                                        的for(int i = 0; I<瓦;我++){
                                            TextHolder firstOne = genTList
                                                    获得(ⅰ);
                                            的System.out.println(check3);
                                            字符串文本= firstOne
                                                    .getText();
                                            的System.out.println(check4);
                                            的mainText [I] =新的TextView(
                                                    Independent.this);
                                            的mainText [I] .setId(ⅰ);
                                            的mainText [I] .setText(文字);
                                            的mainText [I]
                                                    .setLayoutParams(新的LayoutParams(
                                                            LayoutParams.WRAP_CONTENT,
                                                            LayoutParams.WRAP_CONTENT));
                                            的mainText [I] .setTextSize(20);
                                            的mainText [I]
                                                    .setGravity(Gravity.CENTER);
                                            的mainText [I] .setText(文本);
                                            TextLayout的[I] =新的LinearLayout(
                                                    Independent.this);
                                            TextLayout的[I] .setId(ⅰ);
                                            TextLayout的[I]
                                                    .setLayoutParams(新的LayoutParams(
                                                            LayoutParams.FILL_PARENT,
                                                            LayoutParams.WRAP_CONTENT));
                                            TextLayout的[I]
                                                    .setGravity(Gravity.CENTER);
                                            TextLayout的[I]
                                                    .addView(的mainText [I]);
                                            textLayoutContainer
                                                    .addView(TextLayout的[I]);
                                        } // for循环结束
                                    } // if语句结束
                                    idb2.close();
                                }
}赶上(例外五){
                                成功= FALSE;
                                字符串错误= e.toString();
                                对话D =新的对话框(Independent.this);
                                d.setTitle(错误);
                                TextView的电视=新的TextView(Independent.this);
                                tv.setText(错误);
                                d.setContentView(电视);
                                d.show();
                            } {最后
                            }
                        }
                    }
                });
        builder.setNegativeButton(取消,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释的id){
                        dialog.cancel();
                    }
                });
        AlertDialog警报= builder.create();
        alert.show();
        打破;    案例R.id.b_edit:
        打破;    案例R.id.b_delete:
        打破;
    }
}

}

对不起,我before.Now删除code以下行不张贴堆栈跟踪:

  IndependentDB IDB0 = IndependentDB
                                            .getInstance();
                                    idb0.open(Independent.this);
                                    ArrayList的< TextHolder> gentList =新的ArrayList< TextHolder>();
                                    gentList = idb0.getAllTextFromGenT();
                                    INT V = gentList.size();
                                    的for(int i = 0; I<伏;我++){
                                        TextLayout的[I] .removeAllViews();
                                    } // for循环结束
                                    idb0.close();

我没有得到ArrayOutOfBound异常了,但我仍然不能够检索最后插入的价值。

继上次四线制logcat的消息,其中的堆栈跟踪消息出现了:

  01-17 14:48:55.617:我/的System.out(408):check3
01-17 14:48:55.617:我/的System.out(408):check4
01-17 14:48:55.628:我/的System.out(408):空空[Ljava.lang.StackTraceElement; @ 44f9cd28
01-17 14:48:56.309:D / dalvikvm(304):GC_EXPLICIT释放208对象/在766ms 9952字节
01-17 14:49:05.149:D / dalvikvm(134):GC_EXPLICIT释放719对象/在77ms 40696字节


解决方案

解决了这个问题。
即时插入新的增值到数据库中,但不依赖于数据库检索和显示它,但我创造了新的TextView,我直接从获得的的EditText文本并将其设置为我创造了新的TextView并显示它。它的工作:)

I'm retrieving and displaying the list of text depending on whichever button/catagory is clicked/selected. I'm also trying to let user enter the text and that that will be saved in the database and will be displayed at the bottom of all the current text of the active/selected catagory thats being displayed.But i'm having a problem in this part.I tried to remove all the child views from parent views by getting number of elements from the database then i tried to insert the data into database and at last i'm trying to get all the elements after inserting new one and build all the child view.While doing this i'm getting ArrayOutOfBound Exception.I'm not sure why i'm getting this error and also whether this is an efficient way to accomplish this.Please guide me.

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

}

Sorry for not posting the stack trace before.Now I removed the following line of code:

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();

I'm not getting ArrayOutOfBound Exception anymore but i'm still not able to retrieve the last inserted value.

Following the last four line of logcat messages where the stack trace message showed up:

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

解决方案

Solved this problem. i'm inserting the newly added value into the database but not depending on the database to retrieve and display it but i created new textview and i'm directly getting the text from edittext and setting it to new textview that i created and displaying it.it worked :)

这篇关于无法从Android中显示数据库中的更新的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆