I am kinda new in android and java programming, and I have a question, wish you could help me to solve it :)
I have a Program, it will fetch and loop data from database and convert it into some checkboxes, then user must check the checkboxes and hit the submit button,,
The value(s) of the checked checkboxes will be stored to a String[], then will be send to another activity via Intent.putExtra..
So far, all I can do is fetch and loop the data from database, but I have no idea about how to store all the checked value (of the checkboxes) to string and sent it to another activity via intent. Can you guys please help me with this and where should I put the code?
And here is my code :
private void fetchFromDatabase() {
// TODO Auto-generated method stub
myDb.open();
int totalGroup = myDb.countHowManyGroups(Username);
String groupId[] = myDb.fetchGroupId(Username);
String groupName[] = myDb.fetchGroupName(Username);
String flag[] = null;
for (int i = 0; i < totalGroup; i++) {
listCheckBox = new CheckBox(this);
listCheckBox.setText(groupName[i]);
listCheckBox.setTag(groupId[i]);
if (listCheckBox.isChecked()) {
int x=0;
flag[x]=listCheckBox.getText().toString();
x++;
}
layout.addView(listCheckBox);
}
myDb.close();
Button bSubmit = new Button(this);
bSubmit.setText("Submit");
layout.addView(bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (listCheckBox.isChecked()) {
}
}
});
}
It depends of what you want to store. Do you want to store the id of the selected checkboxes? Then override their setOnCheckedChangeListener to save it's id on an array (remember that id is 0 unless you set it.)
Do you want to save it's tag? Then override the same method but save the tag instead.
Here is an example of the implementation of setOnCheckedChangeListener:
List<String> myTagList = new ArrayList<String>();
listCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
myTagList.add(buttonView.getTag());
else
myTagList.remove(buttonView.getTag());
}
});
Related
I've already tried this codes below but it only reads word not a phrase and this codes worked and didn't meet what I want.Each word that the user input it will only add and add to an empty array-list. What I want to achieve is when an user input a phrase it will split by space and store in an empty array-list. That array-list will compare to another array-list that contains words when the condition is true it will display a corresponding image and will now read the next word in the phrase.
private List<String> wordsList;
private ArrayList<String>wordsToDisplay=new ArrayList<>();
boolean missingGIF;
int counter;
wordsList= new ArrayList(Arrays.asList(getResources().getStringArray(R.array.sample)));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input = editText.getText().toString();
String[]splited = input.split("//s+");
int count = splited.length;
for(int i=0;i<count;i++){
String word = splited[i];
if(wordsList.contains(word)){
wordsToDisplay.add(splited[i]);
}
else
missingGIF = true;
}
sample();
}
});
}
This is the sample method.
if(wordsToDisplay.size()>0){
counter=0;
gifImageView.setImageResource(getResources().getIdentifier(wordsToDisplay.get(0),"drawable",getPackageName()));
animation = (GifDrawable)gifImageView.getDrawable();
resetAnimation();
startAnimation();
animation.addAnimationListener(this);
}
And this is my animation listener which display corresponding images in all the words that have been inputted by the user.
#Override
public void onAnimationCompleted(int loopNumber) {
if(wordsToDisplay.size()>1){
if(counter<wordsToDisplay.size()){
try{
counter++;
gifImageView.setImageResource(getResources().getIdentifier(wordsToDisplay.get(counter),"drawable",getPackageName()));
animation = (GifDrawable)gifImageView.getDrawable();
resetAnimation();
startAnimation();
animation.addAnimationListener(this);
}
catch(IndexOutOfBoundsException e){
Log.d("Expected",e.toString());
}
}
}
}
Remove counter and use below logic
#Override
public void onAnimationCompleted(int loopNumber) {
if(!wordsToDisplay.isEmpty()){
String nextWord = wordsToDisplay.remove(0);
gifImageView.setImageResource(getResources().getIdentifier(nextWord,"drawable",getPackageName()));
animation = (GifDrawable)gifImageView.getDrawable();
resetAnimation();
startAnimation();
animation.addAnimationListener(this);
}
}
I am building an android application where an user select their favorite stuff.
The name of stuff is added in an array when user clicks on the stuff's image.
Now I want to know how can I parse the value of that array to any fragment and show it in my spinner list.
For example: user select Mobile and tablet by clicking on respective images then these values added in to an array name 'stuffarray' now I want to pass this array to my fragment on an 'submitted' button and when I click on an spinner of my fragment it Should have mobile and tablet value in there list.
Here is my code for stuff selection :
I am building an android application where an user select their favorite stuff.
The name of stuff is added in an array when user clicks on the stuff's image.
Now I want to know how can I parse the value of that array to any fragment and show it in my spinner list.
For example: user select Mobile and tablet by clicking on respective images then these values added in to an array name 'stuffarray' now I want to pass this array to my fragment on an 'submitted' button and when I click on an spinner of my fragment it Should have mobile and tablet value in there list.
Here is my code for stuff selection :
submite = (ImageButton) findViewById(R.id.nextscreen);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent innext = new Intent(getApplicationContext(), MainActivitytabnew.class);
startActivity(innext);
});
img1 = (ImageButton) findViewById(R.id.imageButton1);
img1.setBackgroundResource(R.drawable.mobile);
img1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
isClicked1=!isClicked1;
if (isClicked1) {
img1.setImageResource(R.drawable.mobile);
start();
stuff1 = "mobile";
myList.add(stuff1);
}else {
img1.setImageResource(R.drawable.mobile);
myList.remove(sport1);
//sport1 = "";
txt1.setText("");
}
}
});
img2 = (ImageButton) findViewById(R.id.imageButton2);
img2.setBackgroundResource(R.drawable.tablet);
img2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
isClicked2=!isClicked2;
if (isClicked2) {
img2.setImageResource(R.drawable.tablet);
start();
stuff2 = "tablet";
myList.add(stuff2);
}else {
img2.setImageResource(R.drawable.tablet);
// sport2 = "";
myList.remove(sport2);
}
}
});
You could also set the value before you open it.
First activity
Intent intent = new Intent( this, YourOtherClass.class );
int[] myArray = { 0, 1, 2, 3 };
YourOtherClass.array = myArray;
startActivity( intent );
Second activity
public static int[] array;
Where this is your activity.
Use putExtra():
int helloworld = 100;
Intent intent = new Intent( this, Class2.class );
intent.putExtra( "myInt", helloworld );
startActivity( intent );
then in your next activity use getExtra()
see documentation for intent.
int passedInt = getIntent().getExtras().getInt( "myInt" );
I have an activity that allows user to enter notes and store them in dynamically created textviews. However, when the use leaves the activity and returns, all the created textviews disappear. How can i store store or retrieve all of these created textviews, whenever i return to the activity? Also, i think that my sharedpreferences will be overwriting each time a new textview is added. Any suggestions?
public class DocNoteActivity extends Activity {
private LinearLayout nLayout;
private EditText etDocNote;
private Button btnAdd1;
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.adddocnote);
etDocNote = (EditText) findViewById(R.id.editDocNote);
btnAdd1 = (Button) findViewById(R.id.btnAdd1);
nLayout = (LinearLayout) findViewById(R.id.linearLayout);
TextView tvNote = new TextView(this);
tvNote.setText("");
btnAdd1.setOnClickListener(onClick());
}
private OnClickListener onClick() {
// TODO Auto-generated method stub
return new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String newDocNote = etDocNote.getText().toString();
nLayout.addView(createNewTextView(newDocNote));
getSharedPreferences("myprefs", 0).edit().putString("etDocNote", newDocNote).commit();
}
};
}
private TextView createNewTextView(String newText) {
// TODO Auto-generated method stub
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tvNote = new TextView(this);
tvNote.setLayoutParams(lparams);
tvNote.setText(newText);
return tvNote;
}
}
If you want to retrieve the Notes you have to use the Database (i.e. SQLite) because every time you save the notes in SharedPreferences it will be override. So I recommend you to use the Database. For that here are some useful resources which i think help you.
1 - http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
2 - http://www.vogella.com/articles/AndroidSQLite/article.html
3 - http://android-er.blogspot.in/2011/06/simple-example-using-androids-sqlite_02.html
NOTE - SharedPreferences are used to store key-value pair data. Mostly it is we can say one kind of Session Management in Android way. I mostly store username & password kind of data in SharedPreferences.
Hope this helps.
So the thing I am trying to accomplish should be exactly like this : http://www.javascriptbank.com/simple-javascript-auto-sum-with-checkboxes.html/en/
Unfortunately im not shure if thats even possible so I would appreciate any kind of help or idea . Thanks in advance.Some thing that i have so far:
CheckBox chkone;
CheckBox chktwo;
CheckBox chkthree;
TextView tv;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkone = (CheckBox)findViewById(R.id.chk1);
chktwo = (CheckBox)findViewById(R.id.chk2);
thkthree = (CheckBox)findViewById(R.id.chk3);
checkBoxListener = new OnClickListener() {
//#Override
public void onClick(View v) {
tv = (TextView)findViewById(R.id.tv1);
if (chkone.isChecked())
{
// something
}
}
};
chkone.setOnClickListener(checkBoxListener);
chktwo.setOnClickListener(checkBoxListener);
chkthree.setOnClickListener(checkBoxListener);
You should implement the CompoundButton's OnCheckedChangeListener interface instead, to get notifications about the checkbox's state changes.
Assuming that you didn't extend the CheckBox class to ease your work by storing a numeric value somewhere, you need to parse the label (text) of the checkbox in order to access it's value, and based on the checked state increase / decrease the sum:
private TextView sumText;
private float currentSum = 0;
private final OnCheckedChangeListener checkBoxListener =
new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
currentSum += getNumber(buttonView);
else
currentSum -= getNumber(buttonView);
// TODO: you need to initialize this TextView in your
// onCreate method!
sumText.setText(Float.toString(currentSum));
}
};
private float getNumber(final CompoundButton chk)
{
try
{
return Float.parseFloat(chk.getText().toString());
}
catch (NumberFormatException e)
{
return 0;
}
}
Note, that for this to work as you expect it, you should modify the getNumber method to retrieve the values that is actually under that CheckBox instance!
i have a problem to retrieve the data from the map with key.
here is my code.
v is vector;s object that i get from another activity.
i got the value what i want with....
System.out.println("save in map********"+settingName); at the end but how i can the value one by one.
final Serializable v1 = getIntent().getSerializableExtra("v");
System.out.println("*****"+v1);
keyname = (EditText) findViewById(R.id.saveas);
saveEdit = (Button)findViewById(R.id.savebtn);
saveEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String keysetting = keyname.getText().toString();
System.out.println(keysetting);
settingName.put(keysetting, v1);
}
});
thanks in advance
settingName.get(keyname.getText());