Converting string into TextView? - android

Have a look at this code:
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView storeText = new TextView(this);
idText.setText(row.get(1).toString());
tableRow.addView(storeText);
TextView maggiText = new TextView(this);
idText.setText(row.get(2).toString());
tableRow.addView(maggiText);
I have to manually create a TextView, set it to some string and then pass it. Is there a way by which I can directly pass the string to row that can be put in a for loop?
I am looking for this approach for scalability issues.

///you did mistake while copy paste
//idText using in all Tv
let do it as below
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView storeText = new TextView(this);
storeText.setText(row.get(1).toString());
tableRow.addView(storeText);
TextView maggiText = new TextView(this);
maggiText.setText(row.get(2).toString());
tableRow.addView(maggiText);

ArrayList row = data.get(position);
for(int i=0; i<row.size(); i++)
{
TextView text= new TextView(this);
idText.setText(row.get(i).toString());
tableRow.addView(idText);
}

Related

Accepting user data into a table

I'm designing an app which will accept user input into a TableLayout . The table has 4 headings . I'm unsure of how to get the data into the table . I have a successfully added a test piece of code but I can only add it once. I've attached the code below.
What I wish to know is how to get this to add more than one line to the table?
public void addNew(){
TableLayout tL = (TableLayout)findViewById(R.id.table);
TableRow tR = new TableRow(getApplicationContext());
TextView tV = new TextView(getApplicationContext());
tV.setTextColor(0xff000000);
tV.setText("TEST");
TextView tV2 = new TextView(getApplicationContext());
tV2.setTextColor(0xff000000);
tV2.setText("TEST");
TextView tV3 = new TextView(getApplicationContext());
tV3.setTextColor(0xff000000);
tV3.setText("TEST");
TextView tV4 = new TextView(getApplicationContext());
tV4.setTextColor(0xff000000);
tV4.setText("TEST");
tR.addView(tV);
tR.addView(tV2);
tR.addView(tV3);
tR.addView(tV4);
tL.addView(tR);
}
for (/* x rows */)
{
tL.addView(tR);
}
This should work but if you run into an issue with text view id's clashing you can create an array of text views and an array of table rows. Would make managing and creating them easier as well.
If you make them in an array by doing something like this.
TextView[] textViewArray = new TextView[textViewCount];
for(int i = 0; i < textViewCount; i++)
{
textViewArray[i] = new TextView(this);
}
then you would be able to handle individual values by doing something like this.
textViewArray[i].setText(/* This stuff */);

Cant get the value of an Edittext [duplicate]

This question already has answers here:
Android getText from EditText field
(6 answers)
Closed 8 years ago.
hi i made a program that have EditText... i used for loop to set the number of my EditText, i cant get the value of my EditText(excellent), how can i do that? please help me..my output goes this way...
i want that 3,2,5,1,4 will display in my EditText,,
here are my codes...
final TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
TextView txt2 = new TextView (getApplicationContext());
TextView txt3 = new TextView (getApplicationContext());
TextView txt4 = new TextView (getApplicationContext());
TextView txt5 = new TextView (getApplicationContext());
TextView txt6 = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.addView(txt2);
tableRow.addView(txt3);
tableRow.addView(txt4);
tableRow.addView(txt5);
tableRow.addView(txt6);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Question ");
txt2.setText("Excellent ");
txt3.setText("Best ");
txt4.setText("Better ");
txt5.setText("Good ");
txt6.setText("Poor ");
txt.setTextColor(Color.BLACK);
txt2.setTextColor(Color.BLACK);
txt3.setTextColor(Color.BLACK);
txt4.setTextColor(Color.BLACK);
txt5.setTextColor(Color.BLACK);
txt6.setTextColor(Color.BLACK);
table.addView(tableRow);
TableRow tableRow2 = null;
EditText excellent = null;
EditText best = null;
EditText better = null;
EditText good = null;
EditText poor = null;
TextView name = null;
int j=0;
for(j = 1; j<=count; j++){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
tableRow2 = new TableRow (getApplicationContext());
excellent = new EditText (getApplicationContext());
best = new EditText (getApplicationContext());
better = new EditText (getApplicationContext());
good = new EditText (getApplicationContext());
poor = new EditText (getApplicationContext());
name = new TextView (getApplicationContext());
//i want to retrive the value of this --->//excellent.setBackgroundColor(color);
best.setBackgroundColor(color);
better.setBackgroundColor(color);
good.setBackgroundColor(color);
poor.setBackgroundColor(color);
name.setText("Q#"+Integer.toString(j));
tableRow2.addView(name);
tableRow2.addView(excellent);
tableRow2.addView(best);
tableRow2.addView(better);
tableRow2.addView(good);
tableRow2.addView(poor);
table.addView(tableRow2);
}
final StringBuilder output = new StringBuilder();
final String[] a = excellent.getText().toString().split(",");
output.append(a+",");
TableRow tableRow1 = new TableRow (getApplicationContext());
Button get = new Button(getApplicationContext());
tableRow1.addView(get);
get.setText("Get!");
get.setTextSize(8);
//******************************************************************************//
// GET! //
//******************************************************************************//
get.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText x = new EditText (getApplicationContext());
x.setText(output);
table.addView(x);
}
});
//******************************************************************************//
// END OF GET! //
//******************************************************************************//
The problem is that this
output.append(a+",");
Prints a representation of the object a, not the strings that a contains. Try somthing like this:
for(String s : a){
output.append(s);
}
There are multiple things going wrong here.
you split on "," but that is not what you want. you want the excellent.getText().toString(); of the EditTexts
You append the text it is just after creation. It will be empty string at that time.
What you want to do
Make an ArrayList of EditText
Put all excellent EditTexts in it.
In the onClick go through this list and append all the getText().toString() of these EditTexts
I won't give you an exact implementation. You should be able to figure that out on your own.
Your split function should be followed by a FOR loop. Split gives you a array.
So you need to iterate the array and append inside the lopp. Example
String str = "one-two-three";
for (String val: str.split("-", 2)){
// Append your output with val here
}
Cheers

How to add multiple columns in a row of an android table layout

I have a custom list with name,age and grade .I want to loop through the list and show the contents in a table layout.Each row will have following attributes.Row will have some kind of styling
Name,
Age,
Age_Value,
Grade,
Grade_Value
Here Age and Grades are just a label.Given below is a sample table
Dolly
Age
23
Grade
A+
Roman
Age
22
Grade
C
Ben
Age
23
Grade
B+
I want to add these details to the Table layout in my layout dynamically.
Here is the code im trying
for(int j=0;j<_attributeList.size();j++)
{
TableRow tr = new TableRow(MyActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tvName = new TextView(MyActivity.this);
tvName.setPadding(3,0,0,0);
tvName.setText(_attributeList.get(j).getName() +" : ");
tr.addView(tvName);
TextView tvAgeL = new TextView(MyActivity.this);
tvAgeL .setText("Age");
tr.addView(tvAgeL );
TextView tvAgeValue = new TextView(MyActivity.this);
tvAgeValue .setPadding(3,0,0,0);
tvAgeValue .setText(_attributeList.get(j).getValue() +" : ");
tr.addView(tvAgeValue );
attributeTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Since these items are adding to the same row im getting results in a single line. How can i show them in seperate rows
You should try like this
Code
for(int i = 0; i < _attributeList.size();i++){
TableRow newRow = new TableRow(getApplicationContext());
newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
newCheckBox = new CheckBox(getApplicationContext());
TextView feeTypeText = new TextView(getApplicationContext());
feeTypeText.setText(jsonObj.getString("feeType"));
feeTypeText.setTextColor(Color.BLACK);
feeTypeText.setTextSize(16f);
feeTypeText.setTypeface(tf);
TextView dueAmountText = new TextView(getApplicationContext());
dueAmountText.setText(jsonObj.getString("dueAmount"));
dueAmountText.setTextColor(Color.BLACK);
dueAmountText.setTextSize(16f);
dueAmountText.setTypeface(tf);
TextView dueDateText = new TextView(getApplicationContext());
dueDateText.setText(jsonObj.getString("dueDate"));
dueDateText.setTextColor(Color.BLACK);
dueDateText.setTextSize(16f);
dueDateText.setTypeface(tf);
newRow.addView(newCheckBox,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(feeTypeText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,0.8f)));
newRow.addView(dueAmountText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
newRow.addView(dueDateText,(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1.0f)));
attributeTable.addView(newRow);
}
you can add like this way...here i added TableRow Programatically in which i added textView.
addtree.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
tr1 = new TableRow(YourActivity.this);
tr1.setClickable(true);
tr1.setId(jj);
tr1.setPadding(0, 5, 0, 5);
Log.d("id", "id is......"+tr1.getId());
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView textview = new TextView(New_Quote_Activity.this);
textview.setText("Job "+jj);
textview.setTextColor(Color.BLACK);
textview.setTypeface(null, Typeface.BOLD);
textview.setTextSize(14);
textview.setPadding(5, 0, 0, 0);
View vv=new View(New_Quote_Activity.this);
vv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv.setBackgroundResource(R.color.viewcolor);
View vv1=new View(New_Quote_Activity.this);
vv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,1));
vv1.setBackgroundResource(R.color.viewcolor);
tr1.addView(textview);
table_crete_invoice.addView(vv1);
table_crete_invoice.addView(tr1,layoutParams);
table_crete_invoice.addView(vv);
}

how can i sort linearlayout data by name and type in android?

LinearLayout lin2 = new LinearLayout(this);
lin2.setOrientation(LinearLayout.VERTICAL);
LinearLayout lin3 = new LinearLayout(this);
lin3.setOrientation(LinearLayout.HORIZONTAL);
mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
TextView txtfeaturename = new TextView(this);
txtfeaturename.setTextColor(Color.WHITE);
txtfeaturename.setPadding(5, 0, 0, 0);
String strtxtfeaturename =mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
txtfeaturename.setText(strtxtfeaturename);
lin3.addView(txtfeaturename);
mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
TextView txtfeaturedistance = new TextView(this);
txtfeaturedistance.setTextColor(Color.WHITE);
txtfeaturedistance.setPadding(90, 0, 0, 0);
String strtxtfeaturedistance =mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
txtfeaturedistance.setText(strtxtfeaturedistance+"ml");
lin3.addView(txtfeaturedistance);
lin2.addView(lin3);
//mainlinear.addView(lin2);
LinearLayout linsecond = new LinearLayout(this);
linsecond.setOrientation(LinearLayout.HORIZONTAL);
mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
TextView txtfeaturevenuetype = new TextView(this);
txtfeaturevenuetype.setTextColor(Color.WHITE);
String strtxtfeaturevenuetype =mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
txtfeaturevenuetype.setText(strtxtfeaturevenuetype+"-");
linsecond.addView(txtfeaturevenuetype);
mapfeautre.put("othervenuetype",node.getAttributes().getNamedItem("othervenuetype").getNodeValue());
TextView txtfeatureothervenuetype = new TextView(this);
txtfeatureothervenuetype.setTextColor(Color.WHITE);
String strtxtfeatureothervenuetype =mapfeautre.put("othervenuetype",node.getAttributes().getNamedItem("othervenuetype").getNodeValue());
txtfeatureothervenuetype.setText(strtxtfeatureothervenuetype);
linsecond.addView(txtfeatureothervenuetype);
mapfeautre.put("phonenumber",node.getAttributes().getNamedItem("phonenumber").getNodeValue());
TextView txtfeaturephonenumber = new TextView(this);
txtfeaturephonenumber.setTextColor(Color.WHITE);
String strtxtfeaturephonenumber =mapfeautre.put("phonenumber",node.getAttributes().getNamedItem("phonenumber").getNodeValue());
txtfeaturephonenumber.setText(strtxtfeaturephonenumber);
linsecond.addView(txtfeaturephonenumber);
lin2.addView(linsecond);
//TextView txtaddress = new TextView(this);
LinearLayout linthy = new LinearLayout(this);
linthy.setOrientation(LinearLayout.HORIZONTAL);
mapfeautre.put("address",node.getAttributes().getNamedItem("address").getNodeValue());
TextView txtfeatureaddress = new TextView(this);
txtfeatureaddress.setTextColor(Color.WHITE);
String strtxtfeatureaddress =mapfeautre.put("address",node.getAttributes().getNamedItem("address").getNodeValue());
txtfeatureaddress.setText(strtxtfeatureaddress+",");
linthy.addView(txtfeatureaddress);
//lin2.addView(linsecond);
//mainlinear.addView(lin2);
//lin.addView(mainlinear);
mapfeautre.put("zip",node.getAttributes().getNamedItem("zip").getNodeValue());
TextView txtfeaturezip = new TextView(this);
txtfeaturezip.setTextColor(Color.WHITE);
String strtxtfeaturezip =mapfeautre.put("zip",node.getAttributes().getNamedItem("zip").getNodeValue());
txtfeaturezip.setText(strtxtfeaturezip);
linthy.addView(txtfeaturezip);
lin2.addView(linthy);
mainlinear.addView(lin2);
linhh.addView(mainlinear);
linm.addView(linhh);
//dataListfeture.add(mapfeautre);
dataListfeture.add(j, mapfeautre);
mylistfeature.add(j,mapfeautre);.
guys help me how can i sort my data in linear layout by name and type.
I think there it is not possible to to sort a LinearLayout directly.
We have sort the Sting and then give it to LinearLayout in sorted order.
To sort the String following code is the easy solution.
Map<String, String> sortedMap1;
sortedMap1=new TreeMap<String, String>(StringArray);

How can I to add an array of `TextView` data into an existing text view?

How can I to add an array of TextView data into an existing text view? I tried the following code:
TextView tvArrQuestion[]=(TextView) findViewById(R.id.Textparse);
...but I get this error:
Type mismatch: cannot convert from TextView to TextView[]
How can I resolve this?
You can do solve like ths:
first take a string append all the text arraydata to it.
String txt="";
for(int i=0;i<tvArrQuestion.length();i++)
{
txt=txt+tvArrQuestion[i].getText().toString();
}
than you can set it to textview
lets take textview tv so code is:
tv.setText(txt);
You cant give like that in ur code. Place ur code as:
TextView tvArrQuestion=(TextView) findViewById(R.id.Textparse);
and give
tvArrQuestion.setText(your array data);
Another Concept is below As this type u also add Text view
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
TextView name[];
TextView website[];
TextView category[];
for (int i = 0; i < sitesList.getName().size(); i++) {
name[i] = new TextView(this);
name[i].setText("Name = "+sitesList.getName().get(i));
website[i] = new TextView(this);
website[i].setText("Website = "+sitesList.getWebsite().get(i));
category[i] = new TextView(this);
category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
Try this code using existing Layout
Textview name=(TextView)findViewById(R.id.textView1);
String txt="";
for(int i=0;i
}
name.setText(txt);

Categories

Resources