Remove dynamically generated textviews on dynamic button click event - android

I need to remove each Table Rows on an individual button click event.
The Buttons are dynamically generated.
How will i achieve this?
Below is my code:
public class S2 extends Modify implements OnClickListener {
Button b;
String arry1[], category_main,str = null, catarray[];
int i,key = 0;
private static String url = "http://ashapurasoftech.com/train/test.json";
private static final String TAG_a = "menu",TAG_Name = "Name",TAG_Cat = "Category";
JSONArray items = null;
ArrayList<HashMap<String, String>> itemList;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.modify);
category_main = "";
new Getitems().execute(category_main);
b =(Button) findViewById(R.id.Order);
b.setOnClickListener(this);
itemList = new ArrayList<HashMap<String, String>>();
}
#Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.Order:try{ onbuttonclick();} catch(JSONException e){}
break;
}
}
private void onbuttonclick() throws JSONException {
TableRow[] tr = new TableRow[arry1.length];
final TextView[] tx = new TextView[arry1.length];
GridLayout gl = new GridLayout(S2.this);
gl.setRowCount(arry1.length);
gl.setColumnCount(1);
final TableLayout tl = (TableLayout) findViewById(R.id.tb1);
TableRow row = null;
for (i = 0; i < arry1.length; i++) {
final String cat = arry1[i].toString();
tx[i] = new TextView(S2.this);
tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tx[i].setAllCaps(true);
tx[i].setTextSize(15);
tx[i].setText(cat);
tr[i] = new TableRow(S2.this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i],new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tl.addView(tr[i],new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
int buttonsInRow = 0;
int numRows = tl.getChildCount();
if( numRows > 0 ){
row = (TableRow) tl.getChildAt( numRows - 1 );
buttonsInRow = row.getChildCount();
}
if( numRows == 0 || buttonsInRow == 3 ){
row = new TableRow( this );
tl.addView( row );
buttonsInRow = 0;
}
if( buttonsInRow < 3 ){
Button b = new Button( this );
b.setText("Cancel");
b.setId(i);
row.addView( b, 100, 50 );
}
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
b = (Button) findViewById(i);
tl.removeView(tx[i]);
}
});
}
}
this is my screen where i want to clear each items when i click on the button aside it.

you can probably achieve what you need in
onClick(View arg0)
by getting the arg0 parent. check it is
instanceof
Table row or has a tag you set up and then set its visibility to GONE

You can use removeView method in the similar fashion you are using addView:
tr[i].removeView(<YourTextView>;

Related

Rendering EditText of SortableTableView

I'am using SortableTableView to show sqlite table in android application. I want to add edit-button that are outside tables. How to enable editing mode for SortableTableView when edit-button is pressed?
I tried the code like this but it does not work.
/* EditDataAdapter.java */
// THIS IS NOT WORKED ???
public void setRenderEditable() {
int row = 0;
LinearLayout rowView = new LinearLayout(_tableView.getContext());
for (int col = 0; col < _tableView.getColumnModel().getColumnCount(); col++) {
View cellView = getLongPressCellView(row, col, rowView);
int cellWidth = _tableView.getColumnModel().getColumnWidth(col, _tableView.getWidth());
LinearLayout.LayoutParams cellLayoutParams = new LinearLayout.LayoutParams(cellWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
cellView.setLayoutParams(cellLayoutParams);
_tableView.addView(cellView);
_tableView.invalidate();
}
}
/* EditFragment.java */
// Calling EditDataAdapter
ServerSortableTableView tableView = (ServerSortableTableView) rootView.findViewById(R.id.server_table);
if (tableView != null) {
EditDataAdapter editDataAdapter = new EditDataAdapter(getContext(), ServerDataFactory.readServerList(), tableView);
tableView.setDataAdapter(editDataAdapter);
}
Button editButton = (Button) rootView.findViewById(R.id.edit_button);
if (editButton != null) {
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editButton.getText().toString().equals("edit")) {
editDataAdapter.setRenderEditable(); // This is not worked
editButton.setText("save");
} else {
editButton.setText("edit");
}
}
});
}
I found a solution to my problem. I will share here, it may be useful to others.
public void setRenderEditable() {
_tableView.setMinimumWidth(900);
_tableView.removeViewAt(1);
int row = 0;
LinearLayout rowView = new LinearLayout(_tableView.getContext());
for (int col = 0; col < _tableView.getColumnModel().getColumnCount(); col++) {
View cellView = getLongPressCellView(row, col, rowView);
int cellWidth = _tableView.getColumnModel().getColumnWidth(col, _tableView.getWidth());
LinearLayout.LayoutParams cellLayoutParams = new LinearLayout.LayoutParams(cellWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
cellView.setLayoutParams(cellLayoutParams);
rowView.addView(cellView);
}
_tableView.addView(rowView, 1);
}

How can get dynamic button text from another dynamic button OnClickListener event

I want get second dynamic button text from another dynamic button OnClickListener event:
Here is define some dynamic buutons:
LinearLayout lv=(LinearLayout)findViewById(R.id.lv);
for (int k = 1; k <= str[0].length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
btnTopEn = new Button(this);
btnTopEn.setId(k);
final int id_ = btnTopEn.getId();
btnTopEn.setText(" ");
lv.addView(btnTopEn, params);
btnTopEn = ((Button) findViewById(id_));
final Button finalBtnT = btnTopEn;
btnTopEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finalBtnT.setText("");
}
});
}
Now I want text of second button from OnClickListener Event:
TableLayout layout = (TableLayout)findViewById(R.id.TableL);
String stt="RZCEADHPTAUJTSFR";
int l=0;
for (int f=0; f<=1; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=7; c++) {
btnCEn = new Button (this);
String ss=(String.valueOf(stt.charAt(l)));
btnCEn.setText(ss);;
final Button finalBtnB = btnCEn;
btnCEn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int m=2;
btnTopEn = ((Button) findViewById(m));
final Button finalBtnT = btnTopEn;
if (finalBtnT.getText().equals("")) {
String stGetText=finalBtnB.getText().toString();
finalBtnT.setText(stGetText);
break;
}
}
}
});
TableRow.LayoutParams lp = new TableRow.LayoutParams(100,100);
tr.addView(btnCEn, lp);
}
layout.addView(tr);
}
I wrote some code in OnClickListener event but none happen!
What is the value of str in the first loop ?
Also you are setting the text to a space .
btnTopEn.setText(" ");
And while checking you check for empty :
if (finalBtnT.getText().equals("")){
}
Try changing to
if (finalBtnT.getText().toString().trim().equals("")){
}

pass value from stringbuilder to textview

I am trying to get words from several EditTexts and display them using TextView without using any layout. But the program is force closed every-time I run it.
LOGCAT: java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.tablelayout/com.example.tablelayout.MainActivity}:
java.lang.NullPointerException
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(VERTICAL);
int count = 10;
linearLayout.addView(tableLayout(count));
linearLayout.addView(submitButton());
setContentView(linearLayout);
}
private Button submitButton() {
Button button = new Button(this);
button.setHeight(WRAP_CONTENT);
button.setText("Submit");
button.setOnClickListener(submitListener);
return button;
}
TextView txt=new TextView(this);
// Access the value of the EditText
private View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View view) {
StringBuilder stringBuilder = new StringBuilder();
for (EditText editText : editTextList) {
stringBuilder.append(editText.getText().toString());
}
txt.setText(stringBuilder.toString().trim());
}
};
// Using a TableLayout as it provides you with a neat ordering structure
private TableLayout tableLayout(int count) {
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
int noOfRows = count / 5;
for (int i = 0; i < noOfRows; i++) {
int rowId = 5 * i;
tableLayout.addView(createOneFullRow(rowId));
}
int individualCells = count % 5;
tableLayout.addView(createLeftOverCells(individualCells, count));
return tableLayout;
}
private TableRow createLeftOverCells(int individualCells, int count) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
int rowId = count - individualCells;
for (int i = 1; i <= individualCells; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private TableRow createOneFullRow(int rowId) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
for (int i = 1; i <= 5; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private EditText editText(String hint) {
EditText editText = new EditText(this);
editText.setId(Integer.valueOf(hint));
editText.setHint(hint);
editTextList.add(editText);
return editText;
}
}
TextView txt=new TextView(this);
you can't declare and initialize a class member View at the same time, because the Context is not yet valid
change it to:
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
private TextView txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txt = new TextView(this);
// other code
also I noticed that you are not adding this View to any of the containers that your provided to setContentView. The suggestion will fix the crash but you will not see the TextView's content
Exception shows problem is in your manifest file.
Your package name is manifest tag and your activity are in different locations.

refresh activity with dynamic content

I have an activity with some dynamic content.
For each item of an array, there is a different content (number of textViews, checkboxes, ..)
The user clicks on a "save" button and then the screen has to refresh itself to display the content of the next item.
I was wondering if it was a good practice to reload my activity like this :
Intent refresh = new Intent(this, conflictActivity.class);
startActivity(refresh)
finish()
instead of removing all the views in my layouts, etc ...
I think it's easier to reload it.
Is there another way to do it ?
EDIT : added Code.
public class ConflicFieldTest extends Activity {
private int nbField;
private int nbChecked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conflict);
final Button saveButton = (Button) findViewById(R.id.save);
final LinearLayout leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
final LinearLayout rightLayout = (LinearLayout) findViewById(R.id.rightLayout);
final TextView fieldName = new TextView(this);
final LinearLayout editLayoutRight = new LinearLayout(this);
final LinearLayout editLayoutLeft = new LinearLayout(this);
final LinearLayout fieldRight = new LinearLayout(this);
final LinearLayout fieldLeft = new LinearLayout(this);
final ArrayList<String> allIdInConflict = getAllIdInConflict();
for (int i = 0; i < allIdInConflict.size(); i++) {
new AccountSyncTask() {
#Override
public void onPostExecute(
final ArrayList<ArrayList<String>> result) {
nbField = 0;
nbChecked = 0;
final Map<String, Object> fieldsInConflict = conflict
.conflictWithFields(memoAccountMap,
serverAccountMap, modifiedAccountMap);
for (Iterator<Map.Entry<String, Object>> field = fieldsInConflict
.entrySet().iterator(); field.hasNext();) {
final Map.Entry<String, Object> entry = field.next();
fieldName.setText(entry.getKey() + " : ");
final EditText[] editTextArrayRight = new EditText[fieldsInConflict
.size()];
final EditText[] editTextArrayLeft = new EditText[fieldsInConflict
.size()];
final CheckBox[] checkboxRight = new CheckBox[fieldsInConflict
.size()];
final CheckBox[] checkboxLeft = new CheckBox[fieldsInConflict
.size()];
checkboxRight[nbField] = new CheckBox(
ConflicFieldTest.this);
checkboxLeft[nbField] = new CheckBox(
ConflicFieldTest.this);
editLayoutLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setOrientation(LinearLayout.HORIZONTAL);
fieldRight.setOrientation(LinearLayout.VERTICAL);
fieldLeft.setOrientation(LinearLayout.VERTICAL);
rightLayout
.addView(
editLayoutRight,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
leftLayout
.addView(
editLayoutLeft,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editTextArrayRight[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayRight[nbField].setText((String) entry
.getValue());
editTextArrayRight[nbField].setTextColor(Color.BLACK);
editTextArrayRight[nbField].setFocusable(false);
editTextArrayRight[nbField].setClickable(false);
editTextArrayRight[nbField].setWidth(180);
editTextArrayRight[nbField].setPadding(0, 10, 0, 0);
editTextArrayLeft[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayLeft[nbField]
.setText((String) serverAccountMap.get(entry
.getKey()));
editTextArrayLeft[nbField].setTextColor(Color.BLACK);
editTextArrayLeft[nbField].setFocusable(false);
editTextArrayLeft[nbField].setClickable(false);
editTextArrayLeft[nbField].setWidth(180);
editTextArrayLeft[nbField].setPadding(0, 10, 0, 0);
final int i1 = nbField;
checkboxLeft[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
checkboxRight[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
editLayoutLeft.addView(fieldName);
editLayoutRight
.addView(
editTextArrayRight[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutLeft
.addView(
editTextArrayLeft[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutRight.addView(checkboxRight[nbField]);
editLayoutLeft.addView(checkboxLeft[nbField]);
nbField++;
}
saveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//save data & update UI
}
}
.execute(appState.getSessionName(), id, "getObjectOnServer");
}
}
Sorry for the code, it is a litle bit dirty (i have deleted some parts), i am reworking it.
int flag = 0;
public void updateUI() {
if(flag == 0) {
//your normal codes...
} else {
//display view with user given data
}
}
Call this function in oncreate. At first flag will be set to zero. When u enter the data in textview and tick checkboxes, update the flag with some value. Then on save Button click call this function again. Now flag is set. So it will display the updated UI.
I think this will give u an idea..

table row selection error in scrollview

i am creating a table layout with radio group by dynamically adding radiobuttons to it and i want to know which row is selected containing the radio group and i want to retrive the texview data also in the same row .how can i do that any suggestion will be a great help for me.
public class TabActivity extends Activity {
TableLayout table;
RadioGroup mRadioGroup;
ArrayList<String> list_name;
int color_blue = -16776961;
int color_gray = -7829368;
int color_black = -16777216;
int color_white = -1;
final int CHECK_BUTTON_ID = 982301;
int ids_check[];
boolean bool_check[];
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
table = (TableLayout) findViewById(R.id.tableLayout1);
list_name = new ArrayList<String>();
list_name.add("Close");
list_name.add("Cristiano");
list_name.add("David");
list_name.add("Fernando");
list_name.add("Messi");
list_name.add("Kaka");
list_name.add("Wayne");
list_name.add("use");
list_name.add("e");
list_name.add("eff");
list_name.add("euyr");
list_name.add("ejjyytuty");
list_name.add("madre");
list_name.add("yuir");
list_name.add("eyrty");
list_name.add("etytr");
list_name.add("ewrrtt");
bool_check = new boolean[list_name.size()];
ids_check = new int[list_name.size()];
createTableRows();
}
public void createTableRows()
{
for (int i = 0; i < list_name.size(); i++)
{
final TableRow table_row = new TableRow(this);
TextView tv_name = new TextView(this);
Button btn_check = new Button(this);
ImageView img_line = new ImageView(this);
table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table_row.setBackgroundColor(color_black);
table_row.setGravity(Gravity.CENTER_HORIZONTAL);
// table_row.setFocusable(true);
mRadioGroup = new RadioGroup(this);
// test adding a radio button programmatically
final RadioButton[] mbutton=new RadioButton[7];
for(int l=0;l<7;l++){
mbutton[l]=new RadioButton(this);
mbutton[l].setText("test"+l);
mRadioGroup.addView(mbutton[l]);
}
// LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
// RadioGroup.LayoutParams.WRAP_CONTENT,
// RadioGroup.LayoutParams.WRAP_CONTENT);
// mRadioGroup.addView(newRadioButton);
tv_name.setText((CharSequence) list_name.get(i));
tv_name.setTextColor(color_blue);
tv_name.setTextSize(30);
tv_name.setTypeface(Typeface.DEFAULT_BOLD);
tv_name.setWidth(150);
btn_check.setLayoutParams(new LayoutParams(30, 30));
btn_check.setBackgroundResource(R.drawable.small_checkbox_unchecked);
img_line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
img_line.setBackgroundResource(R.drawable.separater_line);
table_row.addView(tv_name);
table_row.addView(btn_check);
table_row.addView(mRadioGroup);
table.addView(table_row);
table.addView(img_line);
//table.addView(mRadioGroup);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
for(int i=0; i<mRadioGroup.getChildCount(); i++) {
RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
int t=table.indexOfChild(table_row);
System.out.println(t);
if(btn.getId() == checkedId) {
String text = btn.getText().toString();
// do something with text
Log.d(text," event1");
return;
}
}
}
});
}
}
}
Why are you not using ListView ? there's nothing in the code that cannot be done by a list view?
You can add you layout by setting the custom adapter.

Categories

Resources