how to get the data from map by the key in android - android

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

Related

ImageButton shows random images

Hi i have a problem while creating imageview when i click previous(b1) and next(b2) button it shows next and previous images from img array but sometimes it shows random images like android layout images etc.
public class FullTable extends Activity{
int[] img={
R.drawable.t1,R.drawable.t2,R.drawable.t3,
R.drawable.t4,R.drawable.t5,R.drawable.t6,
R.drawable.t7,R.drawable.t8,R.drawable.t9,
R.drawable.t10,R.drawable.t11,R.drawable.t12,
R.drawable.t13,R.drawable.t14,R.drawable.t15,
R.drawable.t16,R.drawable.t17,R.drawable.t18,
R.drawable.t19,R.drawable.t20};
ImageButton b1,b2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_table);
b1 =(ImageButton) findViewById(R.id.buttonback);
b2=(ImageButton) findViewById(R.id.buttonnext);
// get intent data
Intent i = getIntent();
// Selected image id
final int position = i.getExtras().getInt("id");
final ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
//imageView.setImageResource(imageAdapter.mThumbIds[position]);
imageView.setImageResource(img[position]);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setImageResource(img[position]-1);
img[position]--;
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setImageResource(img[position]+1);
img[position]++;
}
});
}
}
On your code you're not incrementing and decrementing your position variable at all. And your logic seems somewhat wrong. That's why you're getting some random IDs.
If you already have your img array with the Drawables IDs you want, you can simply set your position variable to zero and remove the final from it, and then increment it and decrement it directly.
For example, change this:
img[position]--;
To this:
img[--position];
And set:
int position = 0; // Default value.
Later, change the method ImageView#setImageResource to ImageView#setImageDrawable. Like:
Drawable myDrawable = getResources().getDrawable(img[position]); // Get the Drawable Object.
imageview.setImageDrawable(myDrawable); // Set the Drawable on your ImageView.
Also, consider using a List like ArrayList instead of a simple array.
EDIT: As you asked on the comments, you can start over again by reseting your position variable to zero if you're on the last image. You can do that by getting the length of the array. Something like:
if (position >= img.length) {
position = 0;
}
create a variable position at class scope and not inside onCreate() method. then in your b1 onClick() method decrement position and then show image, posting some code to demonstrate the same: ->
// at class level and not inside `onCreate()`
ImageButton b1,b2;
int position = 0; // Initializing with default value
// then inside onCreate()
position = i.getExtras().getInt("id");
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
position--;
if(position < 0){
position = img.length;
}
imageView.setImageResource(img[position]);
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
position++;
if(position > img.length - 1){
position = 0;
}
imageView.setImageResource(img[position]);
}
});
Inside onClick() for b1:
imageView.setImageResource(img[position]-1);: this line of code is getting the id of drawables from img array and decreasing that value by 1.
But what you want is to display previous image so you need to modify your code to:
imageView.setImageResource(img[**position-1**]);
similary modify your code for b2 onCLick
imageView.setImageResource(img[**position+1**]);

Get and Store text from looped checkboxes into a String

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());
}
});

How to pass value of array from one Activity to other?

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

Retrieve text from edittext to array string in android eclipse

I am making text to voice conversion app. I want to take text from user and change it to voice. when i made input text hard coded. my app works perfectly.
static final String[] texts = {"what's up dude"};
TextToSpeech tts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.btexttovoice);
b.setOnClickListener(this);
tts = new TextToSpeech(textvoice.this,new TextToSpeech.OnInitListener(){
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status!= TextToSpeech.ERROR)
{
tts.setLanguage(Locale.US);
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
if(tts!= null)
{
tts.stop();
tts.shutdown();
}
super.onPause();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random();
String random = texts[r.nextInt(3)];
tts.speak(random, TextToSpeech.QUEUE_FLUSH, null);
}
}
it works great but if i want to take input from user through Edit text
i make the following changes:
String[] texts ;
text = (EditText)findViewById(R.id.ttexttovoice);
texts = text.getText().toString();
Here i get error since texts is the array type. How can i get text from edit text to array type of string?
if i do this without array string
String texts ;
text = (EditText)findViewById(R.id.ttexttovoice);
texts = text.getText().toString();
i get no error but i didn't find the desired output. In fact no voice is received.
it seems a simple problem but i am stuck here. kindly help me. Thanks in advance.
String[] texts = new String[3];
text = (EditText)findViewById(R.id.ttexttovoice);
texts[0] = text.getText().toString();

Android Button Problem

i am making a app which generate buttons according to the value entered by user. each button have have there own function defined in XML. Now my main problem is how to shorten these codes.
name[0].setClickable(true);
name[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[0].setText("kjghjbjhb");
}
});
name[2].setClickable(true);
name[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[2].setText("kjghjbjhb");
}
});name[1].setClickable(true);
name[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[1].setText("kjghjbjhb");
}
});
and soo on.....writing these codes again and again is not possible as button generated are dynamic, i dunno how many buttons will be generated. Please tell if there is a some other way to do this.
Something like this?
createButton(int i){
name[i].setClickable(true);
name[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name[i].setText("kjghjbjhb");
}
});
}
With this method you can also make a for-loop:
for (int i = 0; i<name.length; i++){
createButton(i);
}
Here I am specifying the steps to be executed.
You must be creating the buttons by new Button(); just hold its reference in a Collection say ArrayList
ArrayList ar = new ArrayList();
Button b1 = new Button();
ar.add(b1);
Now create a private inner class which is implementing the View.OnClickListener. Now as per rules implement theOnClick() and so the stuff which you want to be done at there
class A extends Activity{
// your stuff here for OnCreate and other business logic
private final class MyListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
v.setText("kjghjbjhb");
}
}
}
Notice that I am setting the text with the reference of object v in onClick. Also make this class singleton.
Now set create the instance of this class (as the MyListerner will be singleton the object will be one) in the setOnClickListener() like this:
MyListener listener = MyListener.getInstance();
b.setOnClickListener(listener);
You can opt this way when the buttons are created on some event or user action. In case if you need to create the buttons in loop you can use the 1st and 3rd step in loop.

Categories

Resources