refresh activity with dynamic content - android

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..

Related

Views don`t appear on the screen

Clicking on the button creates a view. Then I add this view to layout, but it doesn`t appear on the screen.
private ArrayList<EditText> viewArray = new ArrayList<EditText>();
String str = "text";
LinearLayout linLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linLayout = new LinearLayout(this);
linLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
setContentView(linLayout, linLayoutParam);
Button btnAdd = new Button(this);
btnAdd.setText("Add EditText");
LinearLayout.LayoutParams vParams = new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linLayout.addView(btnAdd, vParams);
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
viewArray.add( new EditText(getApplicationContext()) );
viewArray.get(viewArray.size()-1).setText(str + viewArray.size());
viewArray.get(viewArray.size()-1).setId(viewArray.size());
LinearLayout.LayoutParams vParams2 = new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linLayout.addView(viewArray.get(viewArray.size()-1), vParams2);
Toast.makeText(getApplicationContext(),"size: " + viewArray.size(), Toast.LENGTH_LONG).show();
}
});
}
}
The application is not closed, there is no error, the view simply does not appear

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("")){
}

Add ImageButton dynamically from Database not displayed

I'm here today for my soundboard app ! son to make it simple i have a G_Son object which is the controller of the "Son" model. i get the list of sound from my database (everything is fine until here) but then when I dynamically try to create ImageButtons and add them on my activity (manageLayout() ), I have absolutely nothing appearing on my activity ! not even a single button. So if you have any Idea, or want to give me a hand, I'm aware of any suggestion
private G_Son gson;
private OurNiceSoundPlayer soundPlayer;
private List<Son> sons;
RelativeLayout gameBoard;
private Son selectedSound;
private View.OnClickListener mSoundOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlayer.setSound(sons.get(Integer.parseInt(v.getTag().toString())));
Log.i("Board", "Clicked on ImgButton ->" + v.getTag());
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board);
gson = G_Son.getInstance();
sons = gson.getSons(getApplicationContext());
soundPlayer = new OurNiceSoundPlayer(getApplicationContext());
gameBoard = (RelativeLayout) findViewById(R.id.soundboard);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
manageLayout();
gameBoard.invalidate();
}
private void manageLayout() {
if (sons.size()>0)
{
int rawNbr = (int) Math.ceil((double) sons.size() / 3);
int currentSon = 0;
Son displayed = sons.get(currentSon);
for (int i = 0; i < rawNbr; i++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++)
{
ImageButton btnTag = new ImageButton(this);
if (displayed.getIsPerso()) {
File imgFile = new File(displayed.getPathImage());
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
btnTag.setImageBitmap(myBitmap);
} else {
btnTag.setImageResource(R.drawable.defaultimage);
}
}
else
{
int id = getResources().getIdentifier("com.example.m.sbst:drawable/" + displayed.getPathImage(), null, null);
btnTag.setImageResource(id);
}
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setOnClickListener(mSoundOnClickListener);
btnTag.setBackgroundColor(Color.TRANSPARENT);
btnTag.setTag(currentSon);
btnTag.setId(i);
row.addView(btnTag);
currentSon++;
if (currentSon>=sons.size())
{
break;
}
else
{
displayed = sons.get(currentSon);
}
}
gameBoard.addView(row);
}
}
}
Forgot to mention a size, moreover, after months of learning android, I should advice people to use a grid view to do so, it allows with custom layout to do event better than creating your own layout with dynamic image buttons

Remove dynamically generated textviews on dynamic button click event

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>;

Approach to get values of a dynamically generated textView

I successfully created a dynamic TextView and a Button now when ever the button was clicked the value of a TextView changes.
But the problem is I have a final "submit button" outside a loop which should get the INDIVIDUAL values of each TextView and I cant think of a way how to do it can someone pls give me an approach for this thanks!. pls be nice..
code
Cursor citem= sdb.rawQuery("SELECT * FROM ITEM INNER JOIN CATEGORY ON item.categoryid = category.id where category.categoryname='"+fcat+"'", null);
ScrollView scrollView= new ScrollView(this);
LinearLayout mainLayout= new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
Button border = new Button(this);
border.setId(Integer.parseInt(cuser.getString(cuser.getColumnIndex("id"))));;
border.setText("ORDER");
while (citem.moveToNext())
{
byte[] blob =citem.getBlob(citem.getColumnIndex("itemimage"));
int id = Integer.parseInt(citem.getString(citem.getColumnIndex("id")));
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setTag(id);
int i;
ImageView iv = new ImageView(this);
iv.setId(id);
iv.setImageBitmap(dh.getPhoto(blob));
final TextView txtquantity = new TextView(this);
txtquantity.setId(id);
txtquantity.setText("0");
txtquantity.setTextSize(20);
final TextView txtprice = new TextView(this);
txtprice.setId(id);
txtprice.setText(citem.getString(citem.getColumnIndex("itemprice")));
txtprice.setTextSize(30);
ImageButton btn1 = new ImageButton(this);
btn1.setId(id);
final int id_ = btn1.getId();
btn1.setImageResource(R.drawable.ic_launcher);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int i = 0;
i=Integer.parseInt((String) txtquantity.getText())+1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())+(Integer.parseInt(txtprice.getText().toString())*1)));
}
});
ImageButton btn2 = new ImageButton(this);
btn2.setId(id);
final int id2_ = btn2.getId();
btn2.setImageResource(R.drawable.ic_launcher);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(Integer.parseInt((String)txtquantity.getText())<=0)
{
}
else
{
int i=0;
i= Integer.parseInt((String) txtquantity.getText())-1;
txtquantity.setText(String.valueOf(i));
totalprice.setText(String.valueOf(Integer.parseInt(totalprice.getText().toString())-(Integer.parseInt(txtprice.getText().toString())*1)));
}
}
});
linearLayout.addView(iv);
linearLayout.addView(txtprice);
linearLayout.addView(btn1);
linearLayout.addView(txtquantity);
linearLayout.addView(btn2);
mainLayout.addView(linearLayout);
}
mainLayout.addView(totalprice);
mainLayout.addView(border);
scrollView.addView(mainLayout);
setContentView(scrollView);
Not quite sure what problem you're having? If it's just reading all the TextViews you created in the loop, then you should just keep a list, and send it off for processing when you submit...
List<TextView> tv_list = new ArrayList<TextView>();
while(...){
//In loop..add your tv's
TextView some_tv = new TextView()
tv_list.add(some_tv);
...
}
//In the submit, send them off for processing...
private void process_tvs(List<TextView> tv_list){
for(TextView tv:tv_list){
//Assuming your tv's have numbers...
int val = Integer.valueOf(tv.getText().toString());
//do something....
}
}

Categories

Resources