Trying to replace spinner with buttons dynamically populated from database.
Normally spinner use array adapter and built-in List Item Layouts "android.R.layout.simple_spinner_item"etc. How should it be modified if instead of spinner you want to populate buttons?
How in the startQuiz() method Spinner spinnerDifficulty.getSelectedItem(); can be replaced with button index?
HERE HOW IT WORKED WITH SPINNER
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_screen);
spinnerDifficulty = findViewById(R.id.spinner_quizlist);
loadDifficulties();
Button startTest = findViewById(R.id.start_test);
startTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startQuiz();
}
});
}
private void startQuiz() {
ListQuiz selectedLevel = (ListQuiz) spinnerDifficulty.getSelectedItem();
int LevelListID = selectedLevel.getId();
String quizListName = selectedLevel.getName();
Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
intent.putExtra(EXTRA_DIFFICULTY_ID, LevelListID);
intent.putExtra(EXTRA_DIFFICULTY_NAME, quizListName);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
private void loadDifficulties(){
QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
List<ListQuiz> LevelList = dbHelper.getAllListQuiz();
ArrayAdapter<ListQuiz> adapterLevelList = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, LevelList); adapterLevelList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDifficulty.setAdapter(adapterLevelList);
}
Modifications done, so far...
private ArrayAdapter <ListQuiz> adapter;
private Button autobutton;
public int categorySize;
private List<ListQuiz> categoryName;
private LinearLayout QuizListLayout;
private Button levelButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_screen);
autobutton = findViewById(R.id.autobutton);
loadDifficulties();
QuizListLayout = findViewById(R.id.layoutForButtons);
for(int i=0; i<categorySize;i++){
levelButton =new Button(this);
levelButton.setText("" + categoryName.get(i));
levelButton.setId(i);
final int index = i;
levelButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
levelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startQuiz();
}
});
QuizListLayout.addView(levelButton);
}
}
//startQuiz still same
private void startQuiz() {
ListQuiz selectedLevel = (ListQuiz) spinnerDifficulty.getSelectedItem();
int LevelListID = selectedLevel.getId();
String quizListName = selectedLevel.getName();
Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
intent.putExtra(EXTRA_DIFFICULTY_ID, LevelListID);
intent.putExtra(EXTRA_DIFFICULTY_NAME, quizListName);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
private void loadDifficulties(){
QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
List<ListQuiz> LevelList = dbHelper.getAllListQuiz();
categorySize = dbHelper.getAllListQuiz().size();
categoryName = dbHelper.getAllListQuiz();
buttonlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/layoutForButtons">
</LinearLayout>
you can add buttons dynamically to your layout just add linearLayout in your XML file where you wants to add buttons.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/layoutForButtons">
</LinearLayout>
Create a varible of LinearLyout
LinearLayout layoutForButtons;
......
......
now initliase the varible in oncreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_screen);
layoutForButtons = findViewById(R.id.layoutForButtons);
.....
addingButtonsDynamically(numberOfButtons);
}
public void addingButtonsDynamically(int numberOfButtons){
// for adding n number of buttons
for(int i=0; i<numberOfButtons;i++){
Button buttton=new Button(this);
button.setText("Button" + i);
button.setId(i);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// adding button to layout
layoutForButtons.addView(button);
}
}
Related
Trying to replace spinner with buttons dynamically populated from database.
Normally spinner use array adapter and built-in List Item Layouts "android.R.layout.simple_spinner_item"etc.
How should it be modified if instead of spinner you want to populate buttons?
Should I replace layouts for spinner in my in loadDifficulties() method with layouts for buttons?
HERE HOW IT WORKED WITH SPINNER
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_screen);
spinnerDifficulty = findViewById(R.id.spinner_quizlist);
loadDifficulties();
Button startTest = findViewById(R.id.start_test);
startTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startQuiz();
}
});
}
private void startQuiz() {
ListQuiz selectedLevel = (ListQuiz) spinnerDifficulty.getSelectedItem();
int LevelListID = selectedLevel.getId();
String quizListName = selectedLevel.getName();
Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
intent.putExtra(EXTRA_DIFFICULTY_ID, LevelListID);
intent.putExtra(EXTRA_DIFFICULTY_NAME, quizListName);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
private void loadDifficulties(){
QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
List<ListQuiz> LevelList = dbHelper.getAllListQuiz();
ArrayAdapter<ListQuiz> adapterLevelList = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, LevelList); adapterLevelList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDifficulty.setAdapter(adapterLevelList);
}
onCreate(){
LinearLayout listOrders = findViewById(R.id.listOrders);
for (int i = 0; i < listForShowing.size(); i++){
String text = listForShowing.get(i);
listOrders.addView(createButton(text));
}
listOrders.requestLayout();
}
public Button createButton(String text){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Button button = inflater.inflate(R.layout.button, null);
button.setText(text);
}
Briefly
Created LinearLayout for buttons.
I didn't need to use ArrayAdapter in the loadDifficulties anymore.
Added all needed parameters to startQuiz() (in my case int & String) simplifying method to Intents only.
private ArrayAdapter <ListQuiz> adapter;
private Button autobutton;
public int categorySize;
private List<ListQuiz> categoryName;
private LinearLayout QuizListLayout;
private Button levelButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_screen);
autobutton = findViewById(R.id.autobutton);
loadDifficulties();
QuizListLayout = findViewById(R.id.layoutForButtons);
for(int i=0; i<categorySize;i++){
levelButton =new Button(this);
levelButton.setText("" + categoryName.get(i));
levelButton.setId(i);
final int index = i;
levelButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
levelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startQuiz(v.getId(), categoryName.get(index).toString());
}
});
QuizListLayout.addView(levelButton);
}
}
private void startQuiz(int LevelListID, String quizListName) {
Intent intent = new Intent(StartingScreenActivity.this, MainActivity.class);
intent.putExtra(EXTRA_DIFFICULTY_ID, LevelListID);
intent.putExtra(EXTRA_DIFFICULTY_NAME, quizListName);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
private void loadDifficulties(){
QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
List<ListQuiz> LevelList = dbHelper.getAllListQuiz();
categorySize = dbHelper.getAllListQuiz().size();
categoryName = dbHelper.getAllListQuiz();
buttonlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/layoutForButtons">
</LinearLayout>
I'm currently trying to add a user-defined number of TextViews and EditText to an activity, but can't seem to do it other than hard-coding it by creating a variety of different activities.
The objective of this activity is to take the names of the players, the number of which is relayed by the intent extra from the preceding activity.
I'm trying to add both a TextView saying "Player X: " and an EditText to type the name of the player for each player
I know from this post: How to create a variable number of textviews in android that I have to do this programmatically, however, it does not seem to work for me (the activity remains blank when tested)
I have tried creating a temp LinearLayout to which I add the two views in a for(), still nothing.
Any ideas? Am I on the right track?
Best regards
[EDIT] Code is here :
public class players extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_players);
Bundle extras = new Bundle();
final int numPlayers = extras.getInt("num");
final LinearLayout myLayout = findViewById(R.id.lin_Re);
final ArrayList<Player> players = new ArrayList<>();
int size = numPlayers; // total number of TextViews to add
TextView[] tv = new TextView[size];
TextView temp;
EditText[] et = new EditText[size];
EditText temp2;
LinearLayout temp3;
for (int i = 0; i < size; i++)
{
temp = new TextView(this);
temp2 = new EditText(this);
temp3 = new LinearLayout(this);
temp.setText("Player " + i + " : "); //arbitrary task
// add the textview to the linearlayout
temp3.addView(temp);
temp3.addView(temp2);
tv[i] = temp;
et[i] = temp2;
myLayout.addView(temp3);
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="#+id/lin_Re">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b_send"/>
</LinearLayout>
There is 2 way to achieve this:
1. Use a RecyclerView [Recommended]
2. Add TextView and EditText ( which is nested in a Horizontal LinearLayout) into a Vertical LinearLayout nested in a ScrollView programmatically
The first solution I describe below is quite simple if you're familiar with RecyclerView or ListView, the second solution (your current track) is a bit tricky but still achievable.
Solution 1:
MainActivity
public class MainActivity extends AppCompatActivity {
RecyclerView mPlayerList;
List<String> mPlayerNames;
PlayerAdapter mAdapter;
EditText mInput;
Button mCreateButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayerNames = new ArrayList<>();
// setup recycler view
mPlayerList = findViewById(R.id.player_list);
mPlayerList.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new PlayerAdapter();
mPlayerList.setAdapter(mAdapter);
// setup input EditText
mInput = findViewById(R.id.input);
// setup Create button
mCreateButton = findViewById(R.id.create_button);
mCreateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// clear old player names
mPlayerNames.clear();
// read user input: number of player:
String input = mInput.getText().toString();
int numberOfPlayer;
try {
numberOfPlayer = Integer.parseInt(input);
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Invalid input!!!", Toast.LENGTH_SHORT).show();
return;
}
for (int i = 0; i < numberOfPlayer; ++i) {
mPlayerNames.add("Player #" + (i + 1));
}
// make change on recycler view
mAdapter.notifyDataSetChanged();
// dismiss keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mInput.getWindowToken(), 0);
}
});
}
private class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.PlayerHolder> {
#Override
public PlayerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parent, false);
return new PlayerHolder(v);
}
#Override
public void onBindViewHolder(PlayerHolder holder, int position) {
holder.bind(mPlayerNames.get(position));
}
#Override
public int getItemCount() {
return mPlayerNames.size();
}
public class PlayerHolder extends RecyclerView.ViewHolder {
TextView mPlayerLabel;
EditText mPlayerName;
public PlayerHolder(View itemView) {
super(itemView);
mPlayerLabel = itemView.findViewById(R.id.player_label);
mPlayerName = itemView.findViewById(R.id.player_name);
}
public void bind(String playerName) {
mPlayerLabel.setText(playerName);
mPlayerName.setHint("Name of " + playerName);
}
}
}
}
The sample project can be found here:
https://github.com/raiytu4/stackcase004
i have 4 button create in programmatically button in for loop, is it possible to disable the onclicklistener function or ontouchlistener function by using button create in xml ? Sorry for my einglish, wish can be understand my question.
Sample Image
Code
public class Tab1 extends Fragment {
TextView textView, test1;
Button button, button2;
LinearLayout rl;
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab1, container, false);
//Returning the layout file after inflating
//Change R.layout.tab1 in you classest
textView = (TextView) h.findViewById(R.id.textView);
test1 = (TextView) h.findViewById(R.id.test1);
button = (Button) h.findViewById(R.id.button);
button2 = (Button) h.findViewById(R.id.button2);
rl = (LinearLayout) h.findViewById(R.id.tlayout);
for (int i = 1; i < 5; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
return h;
}
}
Add all the buttons you created from the for loop into an array.
ArrayList<Buttons> buttons = new ArrayList<Buttons>();
for(int i =1 ; i<5 ; i++) {
final Button btnAddARoom = new Button(getActivity());
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
buttons.add(YourDynamicallyCreatedButton)
}
Now in your onClick of xml button:
onClick(){
for(Button button: buttons){
button.setEnable(false)
}
}
Note:
When we add a view(button or text..) via xml we give it an id. We can access these elements with this ID. Similarly, when we create buttons dynamically, we need to set id to the buttons/views.
Call setEnable(false) on your Button reference
Example:
Button b ;
..
..
b.setEnable(false);
Keep reference of your programatically added buttons:
Button[] buttons = new Buttons[5];
for(int i =0 ; i<buttons.length ; i++) {
buttons[i] = new Button(getActivity());
final Button btnAddARoom = buttons[i];
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
rl.addView(btnAddARoom);
btnAddARoom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("qweqweqweqwe");
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnAddARoom.setOnClickListener(null);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test1.setText("111111111111111");
}
});
}
Now when you want to disable these buttons:
private disableButtons() {
for(int i=0;i<buttons.length;i++) {
buttons[i].setEnabled(false);
}
}
Hope it will help you!
If you have a dedicated parent for the four buttons then you can eliminate the Arralist or array.
LinearLayout ll = ..//your linear layout or any other parent
final int childcount = ll.getChildCount();
for (int i = 0; i < childcount; i++) {
View view = ll.getChildAt(i);
if (view instanceof Button) {
view.setEnabled(false);
}
}
I'm working on activity where user can add more input fields( 2 spinners and 1 edit text) on button click. This mechanism works fine until activity is recreated (rotation etc). All dynamically added views are lost.I'm looking for the the baest way to save state of views. Unfortunately I can not store list of views in onSaveInstanceState() method also I can not lock screen orientation. What can be a solution to resolve this problem?
Here is activity java code
public class ReportActivity extends AppCompatActivity {
private ImageButton addNew;
private LinearLayout mLayout;
private int categorySpinnerId =1001; //IDs of categorySpinner
private int currencySpinnerId =2001; //IDs of currencySpinner
private int editTextValueId =3001; //Ids of editText
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
Toolbar toolbar = (Toolbar) findViewById(R.id.report_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mLayout = (LinearLayout) findViewById(R.id.report_item_layout);
mLayout.addView(createNewCategorySpinner());
mLayout.addView(createNewEditText());
mLayout.addView(createNewCurrencySpinner());
addNew = (ImageButton) findViewById(R.id.btn_add_new);
addNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewCategorySpinner());
mLayout.addView(createNewEditText());
mLayout.addView(createNewCurrencySpinner());
}
});
}
//Generates operation category spinner
private Spinner createNewCurrencySpinner() {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final Spinner spinner = new Spinner(this);
spinner.setLayoutParams(lparams);
ArrayAdapter<CharSequence> currencySpinnerAdapter = ArrayAdapter.createFromResource(this,R.array.report_activity_currency_spinner,android.R.layout.simple_spinner_item);
currencySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(currencySpinnerAdapter);
spinner.setId(currencySpinnerId);
spinner.setSaveEnabled(true);
Log.d("ID", spinner.getId() + "");
currencySpinnerId++;
return spinner;
}
//Generates currency type spinner
private Spinner createNewCategorySpinner() {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final Spinner spinner = new Spinner(this);
spinner.setLayoutParams(lparams);
ArrayAdapter<CharSequence> categorySpinnerAdapter = ArrayAdapter.createFromResource(this,R.array.report_activity_category_spinner,android.R.layout.simple_spinner_item);
categorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(categorySpinnerAdapter);
spinner.setId(categorySpinnerId);
spinner.setSaveEnabled(true);
Log.d("ID", spinner.getId() + "");
categorySpinnerId++;
return spinner;
}
//Generates operation input value edit text
private EditText createNewEditText() {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final EditText editText = new EditText(this);
editText.setLayoutParams(lparams);
editText.setHint("Value");
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setId(editTextValueId);
editText.setSaveEnabled(true);
Log.d("ID", editText.getId() + "");
editTextValueId++;
return editText;
}
}
Store enough information in your saved instance state Bundle to be able to recreate and repopulate the dynamically-created views in the new activity instance.
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..