Create a dynamic table of records, android? - android

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

Related

How can i add as well as remove layout pragmatically in android?

I want to work as AutoCompleteTextview. But I am not using auto complete text view in my project. I have used edit text and taking it the value for sorting the value of adapter using those values of adapter I am creating a dynamic button. But actually, I want to delete dynamically created button. When a user enters new value in edit text at that case it sorts new value in adapter according to the button has to created. But, my problem is that dynamically created button does not get deleted when a user enters new text on edit text view. It has to looking like this:
if (!s.equals("")) {
final String query = s.toString().trim();
filteredTags.clear();
((ViewManager) btnTag.getParent()).removeView(btnTag);
for (int i = 0; i < TagArray.size(); i++) {
final String tagName = TagArray.get(i).gettagName();
if (tagName.contains(query)) {
filteredTags.add(TagArray.get(i));
}
}
count1 = filteredTags.size();
layout = (LinearLayout) dialog.getCustomView().findViewById(R.id.layoutTags);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
layout.setWeightSum(1);
float rowneed = ((float) count1 / 5);
k = 0;
for (int i = 0; i < ceil(rowneed); i++) {
row1 = new LinearLayout(getContext());
row1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
/* layout.setVisibility(View.VISIBLE);
row.setVisibility(View.VISIBLE);*/
for (int j = 0; j < 5; j++) {
btnTag = new Button(getContext());
btnTag.setHeight(15);
btnTag.setWidth(0);
btnTag.setMinimumWidth(155);
btnTag.setMinimumHeight(135);
mTagList1 = new ArrayList<>();
if (k < count1) {
btnTag.setText(filteredTags.get(k).gettagName());
btnTag.setId(k);
k++;
btnTag.setVisibility(View.VISIBLE);
} else {
btnTag.setVisibility(View.INVISIBLE);
}
Log.e("count", " " + k + " " + count1 + " " + ceil(rowneed) + " " + edtTag.getText().toString());
btnTag.setTextSize(7);
btnTag.setGravity(0);
row1.addView(btnTag);
}
layout.addView(row1);
}
for (int btnId = 0; btnId < filteredTags.size(); btnId++) {
btnTag = (Button) dialog.getCustomView().findViewById(btnId);
final int finalId1 = btnId;
btnTag.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TagNameArray.add(new Tags(filteredTags.get(finalId1).gettagId(), filteredTags.get(finalId1).gettagName()));
// Log.e("button","Button clicked index = " + finalId +" "+ TagArray.get(finalId1).gettagName()+" "+TagNameArray.size());
}
});
}
}
Add this line of code :
layout.removeAllViews();
layout.invalidate();
row.removeAllViews();
row.invalidate();
This might help you, give me a feedback for what you got, wish I help you
set a dynamic tag for btnTag for example
btnTag.setTag(DynamicTagInt++);
and then
row1.removeView(btnTag.findViewById(DynamicTagInt));
//DynamicTagInt= the desired button that you want to delete
or by the ID of the button for example
row1.removeView(btnTag.findViewWithTag(k));

Android : How to add IDs Views to R.java dynamically with String resources?

I've created some buttons dynamically, when i start activity all is well, but when i rotate a screen or press back Button, i lose all buttons, i've googled then i've found that the views without IDs can't be saved, so i want to set IDs for them dynamically and i want to find them inside R.java file with there name like this :
public static final class id {
public static final int Button1=0x7f070027;
public static final int Button2=0x7f070024;
}
I've tried setId() but the problem is not solved, and i think it's not the same thing when use setID(); and set id manually inside String file, so i need your help thanks in advance(^_^).
here's my source code :
TableLayout TL = (TableLayout) findViewById(R.id.tlRDV);
TL.removeAllViews();
j = 0;
for (int i = 0; i < nbH; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tr.setId(View.generateViewId());
TextView text_view = new TextView(this);
Button b = new Button(this);
text_view.setWidth(1000);
j = j + averageConsultationTime;
HMA = ((BeganConsultationH * 60) + (BeganConsultationM))
+ j - averageConsultationTime;
int h = HMA / 60;
int m = HMA % 60;
b.setBackgroundColor(0xff99cc00);
b.setText(h + "H" + m);
tr.addView(b);
for (int k = 0; k < 7; k++) {
Button but = new Button(this);
but.getId(View.generateViewId());
but.setText("" + but.getId());
but.setOnClickListener(getOnClickDoSomething(but,
jours[k] + "_" + h + "H" + m));
tr.addView(but);
}
TL.addView(tr);
}
}
}
}

Could not create multiple rows with header(of column and row) in Android

I have problem with creating header of column and row with respective cell values. Only show last position value in both column and row.
ScrollView sv = new ScrollView(this);
TableLayout ll = new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6", "Row 7" };
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6" };
int nor = row.length;
int noc = column.length;
TextView tv = new TextView(this);
tv.setText("Matrix Implemention Test");
for (int i = 0; i < nor; i++)
{ // for rows
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++)
{ // for columns
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
} else if (j == 0) {
for (int c = 0; c < noc; c++) {
tv1.setText(column[c]);
Log.d("TAG", "j==0->"+row[c]);
}
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
hsv.addView(ll);
sv.addView(hsv);
setContentView(sv);
Output like this: I want of respective header of both column and row. first row 1, row 2.... and column 1, column 2....and so on. but only last position value display above code.
change your code to:
for (int i = 0; i < nor; i++) { // for row
TableRow tbrow = new TableRow(this);
for (int j = 0; j <= noc; j++) { // for column
TextView tv1 = new TextView(this);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
// tv1[i][j].setId(id);
if (i == 0 & j==0) {
tv1.setText("0=0");
Log.d("TAH", "Display00!!!");
} else if (i == 0) {
tv1.setText(column[j]);
} else if (j == 0) {
tv1.setText(row[i]);
} else {
tv1.setText("Table Cell No=>> " + id);
}
tbrow.addView(tv1);
}
ll.addView(tbrow);
}
First thing you seems to have confused columns with rows so i corrected that.
And second notice this for loop:
for (int r = 0; r < nor; r++) {
tv1.setText(row[r]);
Log.d("TAG", "i==0->"+row[r]);
}
is missing.
Here you are first setting row[0] value to tv1 and then row[1] ... so on.
And in the end tv1 has row[6] as the last value.

how to convert int into string in forloop

I want to print json array utrition arrray in textview dynamically problem is that if I write"1" in this line (school3.getJSONObject("1"));
then its print only first "name" and "Qty" from json. I want to write j at this line but problem is it shows error The method getJSONObject(String) in the type JSONObject is not applicable for the arguments (int)
for (int j = 1
school3.getJSONObject(j)
is there any method convert int j= string then add in
school3.getJSONObject(j) like this way
//for example
string z;
z=j.tostring();
school3.getJSONObject(z);
{
"status":1,
"data"
,
"dish_nutrition":
{
"1":
{
"name":"Cholesterol and Diet",
"qty":"2"
},
"2":
{
"name":"Cholesterol and Diet",
"qty":"1"
}
}
}
JSONObject school3 = json2.getJSONObject("dish_nutrition");
final TableLayout table = (TableLayout) findViewById(R.id.table2);
for (int j = 1; j < school3.length(); j++) {
final View row = createRow (school3.getJSONObject("1"));
table.addView(row);
num=num+1;
}
public View createRow(JSONObject item) throws JSONException {
View row = getLayoutInflater().inflate(R.layout.rows, null);
((TextView)
row.findViewById(R.id.localTime)).setText(item.getString("name"));
((TextView)
row.findViewById(R.id.apprentTemp)).setText(item.getString("qty"));
return row;
}
Use Integer.toString(int) or use int+"" in converts the integer to string like j+""
for (int j = 1; j < school3.length(); j++) {
final View row = createRow (school3.getJSONObject(j+""));
table.addView(row);
num=num+1;
}
OR
for (int j = 1; j < school3.length(); j++) {
final View row = createRow (school3.getJSONObject(Integer.toString(j)));
table.addView(row);
num=num+1;
}
String z = String.valueOf(j);
school3.getJSONObject('\"' + z + '\"');
Integer to string?
Integer.toString(i)
You can do something like this :
school3.getJSONObject('\"' + j + '\"');
If you need to convert int into string just use String.valueof(int values);
and if u need to convert string to int use
Integer.parseInt(string values);
Through it is basic java. you should know that.
String.valueof(int values) to convert Int to String
Integer.parseInt(string values) to convert String to Int

How do you loop through view id's?

For my android app I need to make an array of View ID's.
The array will hold 81 values so it's quite lengthy to add them one by one.
This is how it looks now:
cells[0] = R.id.Square00;
cells[1] = R.id.Square01;
cells[2] = R.id.Square02;
cells[3] = R.id.Square03;
cells[4] = R.id.Square04;
cells[5] = R.id.Square05;
//All the way to 80.
Is there a shorter/more efficient way of doing this?
Thankfully, there is. Use getIdentifier():
Resources r = getResources();
String name = getPackageName();
int[] cells = new int[81];
for(int i = 0; i < 81; i++) {
if(i < 10)
cells[i] = r.getIdentifier("Squares0" + i, "id", name);
else
cells[i] = r.getIdentifier("Squares" + i, "id", name);
}
Sam's answer is better but i think i should share an alternative
int [] ids = new int [] {R.id.btn1, R.id.btn2, ...};
Button [] arrayButton = new Button[ids.length];
for(int i=0 ; i < arrayButton.length ; i++)
{
arrayButton[i] = (Button) findViewById(ids[i]);
}
Modified form of Sam Answer
No need of if else use Integer String Formating
Resources r = getResources();
String name = getPackageName();
int[] resIDs = new int[81];
for(int i = 0; i < 81; i++)
{
resIDs[i] = r.getIdentifier("Squares0" + String.format("%03d", i), "id", name);
}

Categories

Resources