How to display database table records using TableLayout - android

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 ?

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.

A couple of things:
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
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);

Related

Android Studio: getting an input from user

I'm new to Android Studio. I would like to read input from user from a dynamically created elements. I've already managed to create the elements. Now I do not know at which place should I place the reading element. Can someone give me a MWE here ? I do not know also if I should use getText(this) or editText(this) or something else.
public void click(View view) {
String col1;
String col2;
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
TextView c = new TextView(this);
EditText etUserInfoNewValue = (EditText)findViewById(R.id.simpleEditText);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
for (int x = 0; x < 5; x++) {
col1 = "(" + x + ")"+Integer.toBinaryString(x);
col2 = "1";
TableRow newRow = new TableRow(this);
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
EditText editText1 = new EditText(this);
TextView column3 = new TextView(this);
column1.setText(col1);
column1.setText(col1);
column2.setText(col2);
newRow.addView(column1);
newRow.addView(editText1);
newRow.addView(column3);
tl.addView(newRow, new TableLayout.LayoutParams());
}
}

textviews in table.i want user input on those textviews on a submit button click

I want to set user input to the textviews in a tablelayout for a word game application.i am new to android please help me through this.
tl = (TableLayout)findViewById(R.id.table);
TableLayout.LayoutParams tablelparam = new TableLayout.LayoutParams(30,30);
for (int r = 1; r <=9; r++) {
r1= new TableRow(this);
r1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
for (int c = 1; c<=5; c++) {
TextView txtview=new TextView(MainActivity.this);
txtview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
txtview.setId(counter);
counter++;
txtview.setText( +counter + inputWord);
r1.addView(txtview);
}
tl.addView(r1,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
}
Submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ArrayList<String>words = new ArrayList<String>();
for (int j = 0; j < words.size(); j++) {
TextView txtview = new TextView(MainActivity.this);
txtview.setId(j);
txtview.setText("" + words.get(j));
txtview.getLayoutParams();
t3.setText(t3.getText() + " " + txtview.getText());
}
};
Set the onClickListener() for each TextView you instantiate inside your for loop.

Table get selected row index

I am having some problem when trying to get the value of the first column when the checkbox of certain row is checked using Android. Here is my codes:
private ArrayList<String> exerciseIDList = new ArrayList<String>();
private void BuildTable() {
try {
String sql = "SELECT * FROM exercise";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur.getCount() != 0) {
if (mCur.moveToFirst()) {
do {
int cols = mCur.getColumnCount();
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for (int j = 0; j < cols + 1; j++) {
if (j == cols) {
CheckBox cb = new CheckBox(this);
cb.setLayoutParams(new TableRow.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
cb.setGravity(Gravity.LEFT);
row.addView(cb);
final View rowIndex = table_layout.getChildAt(j);
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int rowID = table_layout.indexOfChild(rowIndex);
exerciseIDList.add(Integer.toString(rowID));
}
});
break;
}
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.LEFT);
tv.setTextSize(10);
tv.setText(mCur.getString(j));
row.addView(tv);
}
table_layout.addView(row);
} while (mCur.moveToNext());
}
}
} catch (SQLException mSQLException) {
throw mSQLException;
}
}
Basically I have 3 columns in my database, ID, exerciseType and amount. When I do a for loop, I +1 the cols to add checkbox to the end of each row. And when the checkbox is checked, I supposed to get the value of the first column which is ID and store into the list.
However, from the codes I provided above, it does not get the value of the first column of checked row. It is just simply looping start from 0.
Any guides?
The issue you are having is that you are referring to a non-final variable inside an anonymous class (View.OnClickListener()). See this post for more information. I would suggest the following:
final int k = mCur.getInt(mCur.getColumnIndex("id")); //I'm not sure what you named your ID column, but put that here
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
exerciseIDList.add(Integer.toString(k));
}
});
Basically, just create a new final variable k and assign it to your return from the Cursor.getInt(). That should get you through this error.

Android - Dynamic Table Rows NullPointerException

I am making an android application, which creates a dynamic interface, according to a string read from your preferences:
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String Favs = SP.getString("Favs", "None");
String[] Genveje;
if (!Favs.equals("None"))
{
Genveje = Favs.split(";");
tr = new TableRow[Genveje.length];
for (int i = 0; i < Genveje.length; i++)
{
String BtnName = Genveje[i].split("#")[0];
String BtnPath = Genveje[i].split("#")[1];
Button btn = new Button(this);
btn.setText(BtnName);
btn.setTag(BtnPath);
btn.setOnClickListener(this);
btn.setOnLongClickListener(this);
tr[i].addView(btn);
.........
}
The problem is: when i try to do ANYTHING with tr[i] i get a NullPointerException, and i have no clue why.
Anyone with a suggestion?
tr[i]=new TableRow(this);
tr[i].addView(btn);

Print out ArrayList content in Android TableLayout

I have this ArrayList:
ArrayList<Double> debtList = datasource.debtList;
ArrayList<Double> feeList = datasource.feeList;
How would I print out these two Lists side by side (formatting doesn't matter) in a TableLayout in a loop? Here is layout:
TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
Ok, you have two arraylists debtList and feeList, I assume both the arraylist contains equal number of elements, now iterate through this list, Create Table Row add two textViews to table row, and add tablerow to the tableLayout, so you can do following:
ArrayList<Double> debtList = datasource.debtList;
ArrayList<Double> feeList = datasource.feeList;
TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
for(int i=0;i<debtList.size();i++)
{
TableRow row=new TableRow(this);
double debt = debtList.get(i);
double fee = feeList.get(i);
TextView tvDebt=new TextView(this);
tvDebt.setText(""+debt);
TextView tvFee=new TextView(this);
tvFee.setText(""+fee);
row.addView(tvDebt);
row.addView(tvFee);
table.addView(row);
}
I have found out the solution if the arrayLists is not of equal size.
Here is the logic which i have implemented:
Here is my activity:
public class TestStringActivity extends Activity {
private ArrayList<String> input1 = new ArrayList<String>();
private ArrayList<String> input2 = new ArrayList<String>();
private TableRow row;
private TableLayout inflate;
private TextView txtcol1, txtcol2;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Populating the arrayList
input1.add("1 ");
input1.add("2 ");
input1.add("3 ");
input2.add(" Red");
input2.add(" Blue");
input2.add(" Green");
input2.add(" White");
inflate = (TableLayout) TestStringActivity.this
.findViewById(R.id.mytable);
for (int i = 0, j = 0; i < input1.size() || j < input2.size();) {
row = new TableRow(TestStringActivity.this);
txtcol1 = new TextView(TestStringActivity.this);
if (input1.size() > i) {
if ((input1.get(i) != null)) {
txtcol1.setText(input1.get(i));
i++;
}
} else {
txtcol1.setText("");
}
row.addView(txtcol1);
txtcol2 = new TextView(TestStringActivity.this);
if ((input2.size() > j)) {
if (input2.get(j) != null) {
txtcol2.setText(input2.get(j));
j++;
}
} else {
txtcol2.setText("");
}
this.row.addView(txtcol2);
inflate.addView(row);
}
}
}
Here is my Table Layout main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mytable"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableLayout>
Hope this helps if the arrayLists size are not of equal size.
Assuming the number of entries in the ArrayLists are equal to the number of cells in the TableLayout, you can try something like this:
int index = 0;
TableLayout tableLayout = findViewById(R.id.table_layout);
for(int n = 0, s = tableLayout.getChildCount(); n < s; ++n) {
double debt = debtList.get(index);
double fee = feeList.get(index);
TableRow row = (TableRow) tableLayout.getChildAt(n);
TextView cell = (TextView) row.findViewById(R.id.textview_cell);
name.setText("debt = " + debt + ", fee = " + fee);
index++;
}

Categories

Resources