I have created multiple checkboxes via applying looping.
for(int l=0;l<len;l++)
{
chkBox = dynamicUiComponents.myCheckBox(context, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT), 100+i, "Unchecked", opts[l]);
myLayout.addView(chkBox);
}
All the check boxes are showing. But when I am applying setOnCheckedChangeListener(l) on that check box, then only last added check box text is printer. Its because every time in loop, I provide a new object reference to the chkBox variable. So here how to identify that which chechbox is clicked.
In your code, you did not create an array of CheckBoxes, you only created one. So, using setOnCheckChangedListener(I) will not refer to the checkBox. Either you set the listener inside the loop, or give each a unique ID to refer to it later and set the listener:
for(int l=0;l<5;l++) { chkBox = new CheckBox(context);
chkBox.setOnCheckChangedListener(
//your implementation
);
myLayout.addView(chkBox); }
you can add id to tag:
for(int id=0;id<5;id++) {
chkBox = new CheckBox(context);
chkBox.setTag(id);
myLayout.addView(chkBox);
}
and you may use:
Integer i = (Integer) chkBox.getTag();
Try the following
for(int l=0;l<5;l++)
{
chkBox = new CheckBox(context);
myLayout.addView(chkBox);
chkBox.setTag(""+l);
chkBox.setOnCheckChangeListener(new ...(){
int x = Integer.ValueOf(chkBox.getTag());
//do whatever you want to do here
});
}
You can set a different tag for each Checkbox and set the listener on THIS, you activity should implement the listener and then do whatever you want when the event is trigger.
Related
I create a vertical list of textviews with an arraylist and attach on onclicklistener to each one. In the onclick I set code to remove that item. When I click in sequence from the last generated to the first this works fine. But if I remove the first one and then the last one it gives me a null pointer exception. I know this is happening because it is attempting to remove an index that is no longer present, or at least that is what I think is happening. But I cannot figure out how to solve that.
private void generateViews(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView[] textView = new TextView[questionArray.size()];
for(int i = 0; i < questionArray.size(); i++){
final int Index = i;
textView[Index] = new TextView(getActivity());
textView[Index].setText(questionArray.get(i));
textView[Index].setId(Index);
textView[Index].setGravity(Gravity.CENTER);
textView[Index].setPadding(15,15,15,15);
textView[Index].setLayoutParams(params);
textView[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textView[Index].getId() == (v).getId()) {
questionArray.remove(Index);
answerArray.remove(Index);
saveVariables();
updateViews();
((ViewGroup) textView[Index].getParent()).removeView(textView[Index]);
Toast.makeText(getActivity(), "Question and Answer removed!", Toast.LENGTH_SHORT).show();
}
}
});
mainLayout.addView(textView[Index]);
}
EDIT:
I figured out a small fix but it has it's problems. Instead of removing the items from the arrays with the index I can remove them by searching for the text within the textview.
The problem with this solve is that if my array contains 2 items that are identical then it may remove the wrong index.
questionText = textView[Index].getText().toString();
answerText = textView[Index].getText().toString();
if(questionArray.contains(questionText) && questionArray.size() > 0){
questionArray.remove(questionText);
answerArray.remove(answerText);
}
Solved:
I solved it by first searching for the index of the question text and removing that index from both arrays. The arrays are user generated and I plan on preventing the user from entering the same question twice.
questionText = textView[Index].getText().toString();
int questionIndex = questionArray.indexOf(questionText);
questionArray.remove(questionIndex);
answerArray.remove(questionIndex);
Also, I did it this way because I am still an amateur and was not aware of the Recyclerview. I plan on educating myself on that function and hopefully implementing it.
I really have no idea about why you want do this? if you just want remove textview in a list , why don't you use listview or recyclerview instead ?
You should consider using RecyclerView.
I have created checkbox dynamically. Based on the require list size just am creating new dynamic check box in a repeated manner and am also setting the Id for that. Now i want to do check it in other loop.
for(int i=0;i<require.size();i++)
{
//From Requirements
requirement=require.get(i);
RelativeLayout rl1 = new RelativeLayout(getActivity());
rl1.setBackgroundResource(R.drawable.listviewdesign);
l1.setOrientation(LinearLayout.VERTICAL);
req1 = new CheckBox(getActivity());
rl1.addView(req1);
req1.setId(Integer.parseInt(requirement.r_id));
Log.i("getid",Integer.toString(req1.getId()));
li.add(Integer.toString(req1.getId()));
}
In this loop am just checking element of li and proj_require1 values. If both are equal then I want to make the CheckBox as checked. For that i have written the code here.
for(int i=0;i<li.size();i++)
{
//li.get(i);
req1 = (CheckBox) container.findViewById(i);
String sr = req1.toString();
for(int j=0;j<proj_require1.size();j++)
{
pr = proj_require1.get(j);
if(sr.equals(pr.rid))
{
req1.setChecked(!req1.isChecked());
}
else
{
req1.setChecked(req1.isChecked());
}
}
}
But my doubt is in first loop am creating the CheckBox based on the size of require object. So every time it creates the CheckBox inside the loop. But in second loop am trying to access the checkbox which am created in the first loop. Could anyone please help me to solve this problem? The only way is i can create the CheckBox in the first loop. I want to access it in other loop. Is it possible?
I created a dynamic form without much problems, but I need to recover the values from the fields (controls) of the form, but I'm not sure of how to do this.
For example, I have this piece of code:
if(tipoP.equals("TEXTAREA")){
EditText ta = new EditText(this);
ta.setId(i);
LayoutParams params3 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, (float) 0.3);
params3.setMargins(20, 0, 20, 0);
ta.setLayoutParams(params3);
ta.setLines(3);
ta.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
ll.addView(ta);
}
How do I add a listener that captures the text of the EditText and put it inside a Vector variable?
I tried this:
ta.setOnClickListener(new OnClickListener(){
public void onClick(View view){
EditText t = (EditText) findViewById(i);
res.add(t.getText().toString);
}
});
But I'm not getting the id (variable i) because its in another execution environment. How do I solve this? Any help would be appreciated!!
You should not use setId for dynamically created views but setTag and findViewByTag.
You could create a button dynamically and set an onClickListener on it. Inside the listener, you can just reference the EditText directly (no need for tags or ids) as long as you have made it final.
I've developed one Android application which uses a HorizontalScrollView, and the HorizontalScrollView has one child as a LinearLayout.
Now I want to add buttons on LinearLayout at Runtime means dynamically.
I added the button successfully, But the problem is that my button click event does not work in Android.
ArrayList listClassItems = objCompany.getListClassItems();
Button[] btnCategory = new Button[listClassItems.size()];
for(int i=0;i<listClassItems.size();i++)
{
System.out.println("OTHER_CLASS LENGTH : " + listClassItems.size());
System.out.println("CLASS ID : " + listClassItems.get(i).getClassId());
System.out.println("CLASS NAME : " + listClassItems.get(i).getClassName());
btnCategory[i] = new Button(myContext);
btnCategory[i].setId(i);
btnCategory[i].setTag(listClassItems.get(i).getClassId());
btnCategory[i].setText(listClassItems.get(i).getClassName());
btnCategory[i].setClickable(true);
btnCategory[i].setPadding(10,10,10,10);
LayoutParams layParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
if(i!=0 || i!=listClassItems.size()-1)
{
layParams.leftMargin = 10;
layParams.rightMargin = 10;
}
tabRowBottom.addView(btnCategory,layParams);
btnCategory[i].setOnClickListener(null);
tabRowBottom.addView(btnCategory[i]);
btnCategory[i].setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myContext, "=== Button CLICKED ===",Toast.LENGTH_SHORT).show();
btnCategory.setBackgroundColor(Color.BLACK);
}
});
}
Instead of an array, create a list of buttons, you can create buttons and set their id, tags and onclicklistenners like this and add them to the button list:
buttonList = new ArrayList<Button>();
for (int i=0;i<5;i++){
Button button = new Button(getApplicationContext());
button.setOnClickListener(customListenner);
button.setId(i);
button.setTag(i);
myLayout.addView(button);
buttonList.add(button);
}
and when you need to use the button again, just call with their id or tags from the list.
If you need different listenners, you can control them by using the unique tag check in if function and declare another action.
This is the method that I always use when I create dynamic views programmatically.
In my application I create dynamic rows in table much as in this tutorial:
http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout
for(int i = startDay; i < startDay + 7; i++){
/* Create a TextView to be the row-content. */
TextView day = new TextView(this);
day.setText(Integer.toString(i));
day.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
day.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Listener: ", "Click");
}
So now when I click on a TextView I can register click event, but how do I determine which TextView was clicked?
Not just an object which I get with an event but data like which day number was clicked?
Ideally I would want to have data attached to every view I create dynamically.
Something like data() method in Javascript jQuery.
Right now I can see only 1 way to solve this - while creating TextView add id with data and when clicked - get id back and parse it to get my data. But it strikes me as ugly approach.
Is there a way to attach arbitrary data to android views?
Found answer going through view methods. Maybe it will be useful for someone.
Methods I needed were:
setTag() and getTag()
http://developer.android.com/reference/android/view/View.html#setTag%28java.lang.Object%29