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++;
}
Related
I have a string, comma separated, from mysql database witch I split into an array.
My problem is that when I try to put them in a tablerow programatically it display each split value in a new row. I want to display it in same row with 4 columns.
This is my code:
String currentString = Order_list;
String[] separated = currentString.split(",");
TableLayout secondTbl = findViewById(R.id.secondTable);
for(int i=0;i<separated.length;i++){
TableRow row= new TableRow(getApplicationContext());
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setBackgroundResource(R.drawable.row_drawable);
txtOrdprod = new TextView(getApplicationContext());
row.addView(txtOrdprod);
TableRow.LayoutParams params = (TableRow.LayoutParams) txtOrdprod.getLayoutParams();
params.leftMargin = 18;
params.rightMargin = 10;
params.topMargin = 10;
txtOrdprod.setLayoutParams(params);
txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
txtOrdprod.setTextColor(Color.BLACK);
txtOrdprod.setTypeface(null, Typeface.BOLD);
txtOrdprod.setText(separated[i]);
secondTbl.addView(row,i);
}
You have to create a row, and put your columns (seperated strings) into that row. For example:
String orders = "2, Product, 100 Tablets, 50 Eur, 3, Product, 100 Tablets, 75 Eur";
int k = 0;
int startPoint = 0;
List<String> orderList = new ArrayList<>();
for(int i = 0; i < orders.length(); i++)
{
if(orders.charAt(i) == ',')
{
k++;
if(k == 4)
{
String ab = orders.substring(startPoint, i);
orderList.add(ab);
startPoint = i + 1;
k = 0;
}
}
}
TableLayout secondTbl = findViewById(R.id.secondTable);
int rowIndex = 0;
for(String order : orderList)
{
String[] separated = order.split(",");
TableRow row = new TableRow(getApplicationContext());
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setBackgroundResource(R.drawable.row_drawable);
secondTbl.addView(row, rowIndex);
for(int i = 0; i < separated.length; i++)
{
txtOrdprod = new TextView(getApplicationContext());
row.addView(txtOrdprod);
TableRow.LayoutParams params = (TableRow.LayoutParams)
txtOrdprod.getLayoutParams();
params.leftMargin = 18;
params.rightMargin = 10;
params.topMargin = 10;
txtOrdprod.setLayoutParams(params);
txtOrdprod.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
txtOrdprod.setTextColor(Color.BLACK);
txtOrdprod.setTypeface(null, Typeface.BOLD);
txtOrdprod.setText(separated[i]);
}
rowIndex++;
}
But keep in mind that the orders string needs to be formatted like the on in the example, otherwise it will not work. It would be better if you have a POJO for an order and work with this one.
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.
I need to add at least 10 tables in my application to show some statistics to the user.
The data of the table comes from the server side even the table headers.
Instead of creating 10 different tables I am looking for an approach where I can reuse the table. Changing the size at runtime.
In what data structure should I store the table values and then display in the table?
All I end up doing was this
For header
static void header() {
TableRow tb_head = new TableRow(c);
for (int i = 0; i < row.size(); i++) {
TextView tv1 = new TextView(c);
tv1.setText(row.get(i));
tb_head.addView(tv1);
}
table.addView(tb_head);
}
For table values
static void tableValues()
{
for (int i = 0; i < row.size()-1; i++) {
TableRow tbrow = new TableRow(c);
for (int j = 0; j <= 7; j++) {
TextView tv1 = new TextView(c);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
tv1.setId(id);
tv1.setText("TextView no: " + id);
tbrow.addView(tv1);
}
table.addView(tbrow);
}
}
Any help would be much appreciated.
hope these tutorial links may help you
http://www.lucazanini.eu/2012/android/dynamic-tablelayout-in-android/?lang=en
http://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/
http://www.tutorialsbuzz.com/2014/02/android-building-tablelayout-at-runtime.html
http://wowjava.wordpress.com/2011/04/01/dynamic-tablelayout-in-android/
http://www.tutorialsbuzz.com/2014/01/android-loading-sqlite-data-tablelayout.html
What I have is two arrays one is static string array and the other one is an array list .. what I want to do is to make a table dynamic layout and rows to it dynamically which works just fine .. and what I am trying to do is to add a single row after each seven rows with a different source of data but it keeps adding it after the 14 rows, which is so weird. Please any smart solution for this problem? Here is my android activity code:
public class ProductInfoDetails extends Activity{
private static final String product = "product";
private static final String quantity = "quantity";
private static final String unit = "unit";
private static final String price = "price";
private static final String totalprice = "totalprice";
private static final String totalpriceafterdiscount = "totalpriceafterDiscount";
private static final String note = "note";
ArrayList<String> prodlist=new ArrayList<String>();
int count;
int fixed;
String text[]={product,quantity,unit,price,totalprice,totalpriceafterdiscount,note};
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.penddinginfodetails);
try{
prodlist = getIntent().getExtras().getStringArrayList("prodlist");
count=prodlist.size();
fixed=count/7;
}
catch(Exception e){
e.printStackTrace();
}
Log.e("prodlist",prodlist+"");
Log.e("count",count+"");//14
Log.e("fixed",fixed+"");//2
TableLayout ll = (TableLayout) findViewById(R.id.table1);
int razan=0;
for(int s=0; s<fixed;s++){
if(razan<=count){
for (int i = 0; i <text.length; i++) {
TableRow row= new TableRow(this);
TableRow.LayoutParams tr = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(tr);
TextView product = new TextView(this);
TextView product_j = new TextView(this);
product_j.setText(prodlist.get(razan));
product_j.setBackground(getResources().getDrawable(R.drawable.back));
product_j.setGravity(Gravity.RIGHT);
product.setText(text[i]);
product.setBackground(getResources().getDrawable(R.drawable.back));
product.setGravity(Gravity.RIGHT);
row.addView(product_j);
row.addView(product);
ll.addView(row,i);
razan++;
//here is the code for adding an empty row after each seven inner rows !!!
if(i % 7==0){
TableRow row1= new TableRow(this);
TableRow.LayoutParams tr1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row1.setLayoutParams(tr1);
TextView emptyrow = new TextView(this);
emptyrow.setText("sereen");
emptyrow.setBackground(getResources().getDrawable(R.drawable.back));
row1.addView(emptyrow);
ll.addView(row1);
}
}//inner for
}
}//outer fors
}
}
There are seven items in text[].
for (int i = 0; i <text.length; i++)
so i will have values 0 to 6
if(i % 7==0)
This will be true for i == 0. And never for 1== 7 as i==7 does not occur.
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);