Android set TextView from other class - android

I have code:
LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
I would like to that this code was called from another class, for example:
In main class:
new PrimaryFieldsView().setPrimaryFields();
Other class:
public class PrimaryFieldsView extends Activity{
public void setPrimaryFields(){
LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
}
When i put this code in Main class that work:
LinearLayout primaryFieldsView = (LinearLayout) findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
At this moment i have error
10-27 21:32:58.389: E/AndroidRuntime(2907): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.passreader/com.example.passreader.views.CouponView}: java.lang.NullPointerException

public void setPrimaryFields(Activity activity)
{
LinearLayout primaryFieldsView = (LinearLayout) activity.findViewById(R.id.mainLayout);
for(int j=0; j<5; j++){
TextView text = new TextView(this);
text.setText("The Value of i is :"); // <-- does it really compile without the + sign?
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
primaryFieldsView.addView(text);
}
}
in main:
setPrimaryFields(this);
PrimaryFieldsView class does not have to extend anything though.

Related

Unable to get row id of dynamically added rows in table layout of android

Here is my code to add table rows dynamically,`
for (int j = 0; j < list.size(); j++) {
TableRow tableRow = new TableRow(getActivity());
table_layout.addView(tableRow);
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow.setId(j);
tableRow.setClickable(true);
tableRow.setOnClickListener(tablerowOnClickListener);
TextView textView0 = new TextView(getActivity());
textView0.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
textView0.setText(currencySymbol.get(j));
textView0.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CLIP_VERTICAL);
tableRow.addView(textView0);
TextView textView1 = new TextView(getActivity());
textView1.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
textView1.setGravity(Gravity.RIGHT);
tableRow.addView(textView1);
}
and here is onclicklistener
OnClickListener tablerowOnClickListener = new OnClickListener() {
public void onClick(View v) {
//GET TEXT HERE
//String currenttext = ((TextView)v).getText().toString());
Toast.makeText(getActivity(), "row selected" + View.getId , 30).show();
}
};
however i am not getting the row id by this approach, i am referring this question and this one also.
You need to use v.getId() to get ID of clicked view inside OnClickListner.
Replace this
Toast.makeText(getActivity(), "row selected" + View,getId , 30).show();
By this one
Toast.makeText(getActivity(), "row selected --> " + v.getId() , 30).show();

Edittext - copy values

i made a program and in my program, i have an editText. How can i get the value of my edittext...please help me i can't get excellent.getText().toString();.
Is there a remedy to get the value of my edittext? Help me please thank you in advance..
here are my codes..
TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
TableRow tableRow = new TableRow(getApplicationContext());
TextView txt = new TextView(getApplicationContext());
tableRow.addView(txt);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Excellent ");
table.addView(tableRow);
int j = 0;
for (j = 1; j <= count; j++) {
TableRow tableRow2 = new TableRow(getApplicationContext());
EditText excellent = new EditText(getApplicationContext());
tableRow2.addView(excellent);
}
TableRow tableRow1 = new TableRow(getApplicationContext());
Button showtable = new Button(getApplicationContext());
tableRow1.addView(showtable);
showtable.setText("Show Table");
showtable.setTextSize(8);
showtable.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
excellent.getText().toString();// i cant get the value of
// excellent here!
}
});
create the excellent reference in outside of the method.
TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
EditText excellent;
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Excellent ");
table.addView(tableRow);
int j=0;
for(j = 1; j<=count; j++){
TableRow tableRow2 = new TableRow (getApplicationContext());
excelent = new EditText (getApplicationContext());
tableRow2.addView(excellent);
}
TableRow tableRow1 = new TableRow (getApplicationContext());
Button showtable = new Button(getApplicationContext());
tableRow1.addView(showtable);
showtable.setText("Show Table");
showtable.setTextSize(8);
showtable.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
excellent.getText().toString();//i cant get the value of excellent.
}
});
TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
EditText excellent = new EditText (getApplicationContext());
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.setBackgroundColor(Color.GRAY);
txt.setText("Excellent ");
table.addView(tableRow);
int j=0;
for(j = 1; j<=count; j++){
TableRow tableRow2 = new TableRow (getApplicationContext());
tableRow2.addView(excellent);
}
TableRow tableRow1 = new TableRow (getApplicationContext());
Button showtable = new Button(getApplicationContext());
tableRow1.addView(showtable);
showtable.setText("Show Table");
showtable.setTextSize(8);
showtable.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
excellent.getText().toString();//i cant get the value of excellent here!
}
});

I got a force close when adding table row programatically?

HI am doing table layout programatically for that when i add table row to the layout application shows force close can any body help me and following is my code
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
// Go through each item in the array
for (int current = 0; current < numProvinces; current++)
{
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
}
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow[] tr;
TextView[] labelTV;
TextView[] valueTV;
tr = new TableRow[numProvinces];
labelTV = new TextView[numProvinces];
valueTV= new TextView[numProvinces];
tl.removeAllViews();
tl.refreshdrawablestate();
for (int current = 0; current < numProvinces; current++)
{
tr[current] = new TableRow(this);
labelTV[current] = new TextView(this);
labelTV[current] = new TextView(this);
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
setContentView(tl);
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
for (int current = 0; current < numProvinces; current++)
{
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
/// Do your stuff
tr.addView(labelTV );
tr.addView(valueTV );
tbl.addView(tr);
}
Try this answer
The view already exist may be the Exception you are getting so,
TableRow tr = new TableRow(this);
add the above line in side for loop..
This is happening because you are trying to add same TableRow again and again..
Updated:
try this
tl.addView(tr);
instead of
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
I tested your code with a change in xml file and it work. I post my code so that you can compare with your own one.
I copy your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//I add some data for test here:
int numProvinces = 5;
String[] provinces =new String[]{"1","2","3","4","5"};
//below is the same with your code:
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
// Go through each item in the array
for (int current = 0; current < numProvinces; current++)
{
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
}
}
and this is my main layout (main.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

How to get list of product in memory in Android

I have list of product.That may be contain 5000 records.I put pagination.First It load 50 records.Then if we press next option(>) then load next 50 option likewise.I designed table-layout.
ArrayList<Product> productList = new ArrayList<Product>();
productList = getProducts();
TableLayout tl;
tl = (TableLayout) findViewById(R.id.tableLayout1);
if(productList.size() >0){
for (int i = 0; i < productList.size(); i++) {
TableRow tr = new TableRow(this);
tr.setTag(i);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
TextView txtCode = new TextView(this);
TextView txtDes = new TextView(this);
EditText txtQty = new EditText(this);
TextView txtVal = new TextView(this);
TextView txtDisVal = new TextView(this);
TextView txtDisQty = new TextView(this);
txtQty.setId(i);
txtQty.setFocusable(true);
txtQty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txtQty.setHeight(5);
txtQty.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
createView(tr, txtCode, productList.get(i).getProductCode());
createView(tr, txtDes, productList.get(i).getDescription());
txtQty.setText(Double.toString(productList.get(i).getPrice()));
tr.addView(txtQty);
createView(tr, txtVal,"Value");
createView(tr, txtDisVal,"0.00");
createView(tr, txtDisQty,"0");
tr.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
System.out.println("Row Clicked with tag " + view.getTag());
}
});
tl.addView(tr);
}
}
}
public void createView(TableRow tr, TextView t, String viewdata) {
t.setText(viewdata);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t.setTextColor(Color.DKGRAY);
t.setPadding(1, 0, 0, 0);
tr.setPadding(0, 0, 0, 0);
tr.addView(t);
}
This is my implementation.I didn't implement pagination part yet.
How can I get it into the memory and load it?
Please help me
I suggest you create a function to get products with a param pageindex。
Then you can set a trigger which activated by pagination event。
And update you UI by a handler

TableRow dynamically created with different contents

I am creating TableRows dynamically. And there are two types of content for these TableRows.
Some have 4 Views.
And some have 2 Views.
The problem is that TableRows with two, try to occupy the same space as the layout of which have four.
This is happening:
Img ThisTextViewHasThisSize
Img TV0 __________________TV1 TV2
...
TableLayout tl = (TableLayout) findViewById(R.id.myTableLayout);
for (int i = 0; i < array.length; i++) {
TableRow tr = new TableRow(this);
ImageButton button = new ImageButton(this);
tr.addView(button);
TextView tv0 = new TextView(this);
tv0.setText(array[i].something0());
if (another type of TableRow) {
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText(array[i].something1());
tv2.setText(array[i].something2());
tr.addView(tv1);
tr.addView(tv2);
}
tr.addView(tv0);
tl.addView(tr);
}
Someone can tell me how to let the TableRows layouts totally independent from each other?
You can set the span of a view within the table by using TableRow.LayoutParams. Eg:
TableRow tr = new TableRow(this);
//all rows have this button
ImageButton button = new ImageButton(this);
tr.addView(button);
TextView tv0 = new TextView(this);
tv0.setText("hey");
if (some condition == true) {
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText("heay 2");
tv2.setText("hey 3");
tr.addView(tv0);
tr.addView(tv1);
tr.addView(tv2);
}else {
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
trlp.span = 3;
tv.setLayoutParams(trlp);
tr.addView(tv0);
}

Categories

Resources