1

I followed the "How to get data from database in my android table view as columns" link to resolve my problem but a blank screen appears without data. On clicking the button I have written code to fetch the records from MS SQL DB.

My XML, for layout purpose, activity_display_result.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tab" >
</TableLayout>

And the code snippet from MainActivity.java:

@SuppressLint("NewApi") @Override
public void onCreate(Bundle b)
{
    super.onCreate(b);
    setContentView(R.layout.activity_main);

    if( Build.VERSION.SDK_INT >= 9)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 
    }

    initilize();

    button1.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            List <Map<String,String>> dataList  = querySQL(e1.getText().toString(),e2.getText().toString(),e.getText().toString());

            StringBuilder builder = new StringBuilder();
            for (int i=0;i<dataList.size();i++)
            {               
               Log.i("onClick Method()", "UserName : "+dataList.get(i).get("UserName"));
               Log.i("onClick Method()", "Password : "+dataList.get(i).get("Password"));
               builder.append(dataList.get(i).get("UserName")).append(";").
               append(dataList.get(i).get("Password")).append("_");
            }

            builder.toString();

            String st = new String(builder);
            String[] rows =st.split("_");
            setContentView(R.layout.activity_display_result);
            TableLayout tableLayout =(TableLayout) findViewById(R.id.tab);
            tableLayout.removeAllViews();

            for(int i=0;i<rows.length;i++)
            {
                String row  = rows[i];
                TableRow tableRow = new TableRow(getApplicationContext());
                                                tableRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

                final String[] cols = row.split(";");

                Log.i("MainActivity ","Column length :"+cols.length);

                //Handler handler = null;

                for (int j = 0; j < cols.length; j++) 
                {             
                    final String col = cols[j];                                 
                    TextView TextView11 = new TextView(getApplicationContext());
                    TextView11.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                    TextView11.setTextColor(color.black);
                    //TextView11.setText(String.format("%7s", col));
                    TextView11.setText(col);
                    tableRow.addView(TextView11);
                }

                tableLayout.addView(tableRow);
                setContentView(R.layout.activity_display_result);
            }
        }
    });
}

Declaring all

public void declere()
{
    e = (EditText)findViewById(R.id.editText3);
    e1 = (EditText)findViewById(R.id.editText1);
    e2 = (EditText)findViewById(R.id.editText2);
    button1=(Button)findViewById(R.id.button1);
}

//initializing 
public void initilize()
{
    Log.i("initilize() Method", "initilize() Method is called..Now Calling declere() method");
    declere();
    Log.i("initilize() Method", "declere() method is called ");
    Log.i("initilize() Method", "Before Calling CONN() method..");
    connect=CONN("sa","C!@C2@!2","RequestCenter");
    Log.i("initilize() Method", "CONN() method called Successfully..");

 }

I also added one CONN method for getting connection. Can anyone help me to determine why the screen appears blank ?

2 Answers 2

0

You are calling setContentView(R.layout.activity_display_result) at the end of your onClick method, which will reset the view to the content of the xml, hence giving you a blank screen as defined in activity_display_result.xml. You need to remove that line and see if it works.

Sign up to request clarification or add additional context in comments.

1 Comment

i delete that line but still no result on that new window
0

A couple of things:

  1. You continue to reset the setContentView(R.layout.activity_display_result); many times (via the loop). Is there a a reason? You probably only have to set it once at the end once all your controls have been placed in the layout. I've moved it to where I think it should be in the code below. If that doesn't work, reconsider taking it out as mentioned above by 2Dee
  2. A question for my own understanding: Why is col set to Final? Would that not prevent the variable from being assigned the next value in the cols array? Thanks!

        for(int i=0;i<rows.length;i++){
            String row  = rows[i];
            TableRow tableRow = new TableRow(getApplicationContext());
                                            tableRow.setLayoutParams(new     TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    
            final String[] cols = row.split(";");
    
            Log.i("MainActivity ","Column length :"+cols.length);
    
            //Handler handler = null;
    
            for (int j = 0; j < cols.length; j++) 
            {             
                final String col = cols[j];                                 
                TextView TextView11 = new TextView(getApplicationContext());
                TextView11.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                TextView11.setTextColor(color.black);
                //TextView11.setText(String.format("%7s", col));
                TextView11.setText(col);
                tableRow.addView(TextView11);
    
            }
    
           tableLayout.addView(tableRow);
        }
        setContentView(R.layout.activity_display_result);
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.