如何在android中动态循环textView而不会崩溃?

时间:2013-03-26 09:37:08

标签: android

我想在Sqlite的textView中创建setText。当我尝试填充textView并将textview添加到TableRow并将其添加到TableLayout时,我的手机崩溃然后显示警报ANR。如何让我的手机不崩溃? 在这里我的代码:

cursor = db.selectQueryStock("select * from Stock where 1=1 " + cww);
    cursor.moveToFirst();
    if (cursor != null && cursor.getCount() > 0 ) {
        if (cursor.moveToFirst()) {
            count = 1;
                    do {
                        System.out.println("COUNT = " + count);

                                tr1 = new TableRow(MainActivity.this);

                                txtDataStock = new TextView[3];
                                b = new Button[1];// 0 = "", 1 = no, 2 =
                                                    // itemid / 110222

                                for (int a = 0; a <= 3; a++) {
                                    if (a < 3) {
                                        txtDataStock[a] = new TextView(
                                                MainActivity.this);
                                        txtDataStock[a].setTextSize(12.0f);
                                        txtDataStock[a]
                                                .setTextColor(Color.BLACK);
                                        tr1.addView(txtDataStock[a]);
                                        // System.out.println("IF TextView");
                                    } else {
                                        b[0] = new Button(MainActivity.this);
                                        b[0].setTextSize(12.0f);
                                        b[0].setTextColor(Color.BLACK);
                                        tr1.addView(b[0]);
                                    }
                                }
                                tr1.setBackgroundResource(R.drawable.cell_shape);
                                txtDataStock[1].setText((count++) + ".");
                                txtDataStock[2].setText(cursor
                                        .getString(cursor
                                                .getColumnIndex("cItemID"))); // ITEMID
                                txtDataStock[2].setPadding(5, 0, 0, 0);
                                txtDataStock[2].setWidth(1);

                                b[0].setText(cursor.getString(cursor
                                        .getColumnIndex("cItemName")));
                                b[0].setPadding(5, 0, 0, 0);
                                b[0].setOnClickListener(new OnClickListener() {
                                    public void onClick(View v) {
                                        Button bOnclick = (Button) v;
                                        cTempItemName = bOnclick.getText()
                                                .toString();
                                        System.out
                                                .println("BUTTON 0N CLICK = "
                                                        + cTempItemName);
                                        cLabelButton = "Picture";
                                        layout.setVisibility(TableLayout.GONE);
                                        progress.setVisibility(ProgressBar.GONE);
                                        txtLoading
                                                .setVisibility(TextView.GONE);
                                        btnShowStock
                                                .setVisibility(Button.GONE);
                                        btnClassPic
                                                .setVisibility(Button.GONE);
                                        autoCompleteStock
                                                .setVisibility(TextView.GONE);
                                        LabelSearchStock
                                                .setVisibility(TextView.GONE);
                                        txtLabelDateDownload
                                                .setVisibility(TextView.GONE);
                                        txtDateDownload
                                                .setVisibility(TextView.GONE);
                                        btnDownloadStock
                                                .setVisibility(Button.GONE);
                                        btnSend.setVisibility(Button.VISIBLE);
                                        btnLoad.setVisibility(Button.VISIBLE);
                                        imageView
                                                .setVisibility(ImageView.VISIBLE);
                                        txtLabelItemName
                                                .setVisibility(TextView.VISIBLE);
                                        txtItemName
                                                .setVisibility(TextView.VISIBLE);
                                        txtItemName.setText(cTempItemName);
                                    }
                                });

                                layout.addView(tr1);
                    } while (cursor.moveToNext());                      
            iNmr = iNmr + count;
        } else {
            Toast.makeText(MainActivity.this, "DATA KOSONG",
                    Toast.LENGTH_SHORT).show();
        }
    }

0 个答案:

没有答案