Setting the text to checkbox dynamically in android - android

I need to create checkboxes dynamically in android. I am getting the value in my code but unable to set that value to checkbox.
My Code:
CheckBox[] cbs = new CheckBox[20];
for(int k=0; k<stringList3.size(); k++)
{
System.out.println("stringlist3 in for loop"+stringList3.get(0));
arr = stringList3.get(k);
cbs[k] = new CheckBox(getContext());
System.out.println("arr values"+arr.get(0));
System.out.println("arr values"+arr.get(1));
System.out.println("arr values"+arr.get(2));
cbs[k].setText((CharSequence) arr.get(2));
Rl.addView(cbs[k]);
}
Here when I am setting the value arr.get(2) to checkbox it is not setting ...please help me regarding this...
Thanks in advance

I don't know how you written remaining code, just check your code with below example.
Dynamically adding views to layout
I think in your code, the problem may be stringList3.size() returning more than 20, so that you are getting force close. Just check it once.

Related

Remove items from array with auto generated onclick

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.

How to display horizontally a list of names (Strings) and make them each clickable?

I am looking for a way to display in may Android app a list of names horizontally (maybe in a TextView) and to make them all clickable. The names are pulled from an online database with their own id, which id points to their profile activities for more information about each person.
ListView is not an option I guess because its vertical nature, Is there a simple way to do this, like appending each name with their own click capability?
Thanks!!
i hope the following library will help you:
https://github.com/blazsolar/FlowLayout
To create a horizontal ListView you don't need any external library. I have found an article on web that shows how to do that.
http://sandyandroidtutorials.blogspot.in/2013/06/horizontal-listview-tutorial.html
The suggestions were probabily ok, but were not quite what I wanted, I came up with a solution like this:
for (int i=0; i< contacts.length();i++){
JSONObject json = contacts.getJSONObject(i);
final String names = json.getString("names");
final String uerIds = json.getString("userIds");
final TextView[] likers = new TextView[10];
LinearLayout layout = (LinearLayout) findViewById(R.id.likers);
//setContentView(layout);
likers[i] = new TextView(getApplicationContext());
likers[i].setText(names + ", ");
likers[i].setTextColor(Color.parseColor("#d52e2e"));
likers[i].setTypeface(null, Typeface.BOLD_ITALIC);
likers[i].setTextSize(13);
layout.addView(likers[i]);
likers[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent in= new Intent(getApplicationContext(), UserProfile.class);
in.putExtra("user", names);
in.putExtra("userId", uerIds);
startActivity(in);
}
});
I have created dynamically a TextView for each name, together with OnClickListeners. Hope to help anyone looking for a simple solution.

How to get dynamic CheckBox Id

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?

How to manage onclick on dyanamically generated checkbox in android

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.

Error adding textView to RadioGroup

i'll get to the point immediately, this is my code:
RadioButton[] rb=new RadioButton[n*3];
RadioGroup[] rg=new RadioGroup[n];
for(int i=0;i<n;i++)
{
rg[i] = new RadioGroup(MainActivity.this);
rg[i].setOrientation(RadioGroup.HORIZONTAL);
rg[i].setId(i);
//rg[i].addView(textView); doesn't work with this
for(int j=0;j<3;j++)
{
rb[i*3+j]=new RadioButton(MainActivity.this);
rb[i*3+j].setId(i*3+j);
rb[i*3+j].setText("something");
rg[i].addView(rb[i*3+j]);
}
myLayout.addView(rg[i]);
}
the code works fine without the line rg[i].addView(textView);, but when i put that line the application force closes... What's the problem? How can i put this textview into this group?
I think that your textView is null when your are trying to add it. Can you provide complete logcat trace.

Categories

Resources