which is the use of position and name in putextra? - android

How do I use .get[position] and name in .putextra in Android, and how can I use position in putextra using an array?
ainListView = (ListView) findViewById( R.id.mainListView );
mainListView.setOnItemClickListener(new OnItemClickListener() {
int[] myImageList = new int[]{R.drawable.koala_copy, R.drawable.lighthouse_copy};
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
Intent b = new Intent(SimpleListViewActivity.this, Nextclass.class);
b.putExtra("Name", myImageList.get[position]);
b.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(b);
}
private ArrayAdapter<String> getListAdapter() {
// TODO Auto-generated method stub
return null;
}
});

it is very unclear what you mean. and your code as well. Firstly what are you trying to put in the intent? It looks like you're trying to get a position in an array. .get is not a valid array method. an arraylist yes, but not array. Even so, you wouldn't just put a blank variable.
What i suspect that you want the position of a listview selected put into an intent and then fire off to the next activity.
b.putExtra("Name", pos);
is all you need for that, believe it or not, since the int pos (position) conveniently comes as a parameter on the onclicklistener. Perhaps on the next activity you want to display 1 of the two images in an imageview. There you can use a simple if statement, or case if you plan to put more, to choose what image to show. As of now i can see no business of that int array there being where it is.
EDIT:
There is no magic in the implementation of displaying the image in the next activity. As one would intuit, you get the int that you just put in the intent. Then depending on what the number is, you perform an imageview drawable assignment on a new drawable.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int listPos = getIntent().getExtras().getInt("Name");
ImageView image = (ImageView) findViewById(R.id.imageView1);
switch (listPos) {
case 0: image.setImageResource(R.drawable.koala_copy);
break;
case 1: image.setImageResource(R.drawable.lighthouse_copy);
break;
}
}
as for getting the image from an sd card, perhaps this web page will help:
http://android-er.blogspot.com/2010/07/load-bitmap-file-from-sd-card.html
it has a sample file you can download, too.
Finally, in order to not just give you fish for the day, but help you fish - may i suggest some video tutorials? This is a great resource in addition to stackoverflow for acquiring knowledge about android development: http://thenewboston.org/list.php?cat=6

Related

Sort ListView elements in ascending order using Adapter

In my app, my goal is for the user to upload an image from their gallery, and save it in a database. Each image will have a name and that name and image will be displayed in a listview. The listviews will be sorted kind of like contacts in a phone. What they are exactly are maps of properties, such as different hotels or resorts, or even schools, for the purpose of delivering. I have everything working the way that I want, but for some reason I'm having trouble getting them into alphabetical order. I've tried other examples and snippets, but with no luck. The code I have for it is as follows:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_hotels);
db = new DatabaseHandler(this);
lv = (ListView) findViewById(R.id.list1);
pic = (ImageView) findViewById(R.id.pic);
fname = (EditText) findViewById(R.id.txt1);
final ArrayList<Hotel> hotels = new ArrayList<>(db.getAllHotels());
data = new dataAdapter(this, hotels);
Collections.sort(hotels, new Comparator<Hotel>()
{
public int compare(Hotel a1, Hotel a2)
{
return (a1.toString()).compareTo(a2.toString());
}
});
lv.setAdapter(data);
}
Before I put the code in for the comparisons:
Collections.sort(hotels, new Comparator<Hotel>()
{
public int compare(Hotel a1, Hotel a2)
{
return (a1.toString()).compareTo(a2.toString());
}
});
It would simply display the images in the order that they were saved. But with this chunk of code, it seems that something is wrong. I tried saving 3 different images of properties: Westgate Resort, Grand Country Inn, and College of the Ozarks, in that order. And for some reason, Westgate was shown first, College of the Ozarks second, and Grand Country Inn third. If I add the same image and name it Grand Country Inn as well, Westgate Resort will fall to the bottom of the list. Which seems correct, but upon adding another image, Westgate will go back to the top. Any help would be greatly appreciated! Thank you for your time.
I managed to figure it out. Instead of using the Collections.sort() method, I just used sort() on my adapter.
data = new dataAdapter(this, hotels);
data.sort(new Comparator<Hotel>()
{
#Override
public int compare(Hotel arg0, Hotel arg1)
{
return arg0.getFName().compareTo(arg1.getFName());
}
});
data.notifyDataSetChanged();
lv.setAdapter(data);
I'm not sure if it's bad practice or not, but for what I want, it works perfect.
Try initializing the adapter after you call the sort method.
Please do consider adding
data.notifyDataSetChanged()
each time you add an element into the list.
Another thing that you can do is to lower case each element before adding them into the list. Or try this code
Collections.sort(hotels, new Comparator<Hotel>(){
public int compare(Hotel a1, Hotel a2)
{
return (a1.toString().toLowerCase()).compareTo(a2.toString().toLowerCase());
}
});

Made a method each for multiple Spinners - how to pass this data on to new activities?

This might sound a bit convoluted.
I have roughly 15 Spinners in one activity and made a distinct method for each of these spinners. I then initiate the methods in the onCreate method.
Method example:
//Relative Position Spinner
public void relativePositionSpinner() {
Spinner relativePositionSpinner = (Spinner) findViewById(R.id.spinner_relativePosition);
ArrayAdapter relativePositionAdapter = ArrayAdapter.createFromResource(this, R.array.relativePosition, R.layout.spinner_item);
relativePositionSpinner.setAdapter(relativePositionAdapter);
//what happens when selected
relativePositionSpinner.setOnItemSelectedListener(this);
}
OnCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_hand);
//initiate all Spinners
relativePositionSpinner();
absolutePositionSpinner();
etc.
Now what I want is to send the data of each Spinner to another Activity with the click of a Button. I know that I can do this with an intent and using putExtra in the Button method like this:
public void openHandSummary() {
//Find the Button that gives option to enter new hand
Button handInputButton = (Button) findViewById(R.id.hand_input_button);
//set a click listener on Hand Analyzer Button
handInputButton.setOnClickListener(new View.OnClickListener() {
//below code will be executed when the new Hand Button is clicked
#Override
public void onClick(View view) {
Intent handSummaryIntent = new Intent(NewHandActivity.this, HandSummaryActivity.class);
handSummaryIntent.putExtra("RelPosString", WHATTOENTERHERE??)
startActivity(handSummaryIntent);
}
});
}
However I do not know how to retrieve the value/variable out of my Spinners to put them into the Button/intent method? Because if I make a String in the Spinner method, then I can't access this in the Button method.
So I feel like I have too many methods? So is there a way to pass data from one method to another method, or do I have to cancel some methods? What would be the easiest way to set this up?
I also made an onItemSelected to make some toasts, which worked. Can I use OnItemSelected somehow to create variables or initiate a data transfer to another Activity?
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
switch (parent.getId()) {
case R.id.spinner_relativePosition:
makeText(NewHandActivity.this, "Relative Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner_absolutePosition:
makeText(NewHandActivity.this, "Absolute Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
I'm very new to coding, and I just can't figure out the logic how I get the Spinner methods, Button/iniate method and OnItemSelected method to work together and exchange variables. Would appreciate if someone can point me in the right direction. Have already browsed the internet a day or so to find an answer, with no success.
I like your idea of separating out the code to create each spinner into its own method. However, if you are copying and pasting the whole method and making a few changes, you should step back and think about how you can do it even more easily. Often the changes you make after copy and paste give a hint that you should add some parameters to the method. If you do it correctly, you can write just a single method and then copy and paste the method call instead of the entire method and then make appropriate changes to the arguments.
As for your actual question, you are making this much more complicated than necessary. Specifically, you do not need to setOnItemSelectedListener() on any of the Spinners. Instead, the OnClickListener for the button should just get the selected item from each spinner and send it to the new activity in the Intent.

Setting images in gridview to transparent

So I have images in gridview. These images are shown in an activity. When one of the images is clicked, new activity is started with this image. In this activity, If user inputs correct answer, new activity is started that indicates the answer is correct. After user inputs correct answer, I want to set this image more transparent with a check mark in gridwiew. If the user clicks again to this correct view, I want to show same activity which indicates it was solved correct. How can I do that?
Here is my activity which contains grid view:
public class LogoSelectionActivity extends Activity {
.
.
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(LogoSelectionActivity.this, LogoActivity.class);
intent.putExtra ("clicked_position", position);
startActivity(intent);
}
Here is my activity where user enter inputs:
public class LogoActivity extends Activity{
EditText text;
Button check;
Boolean a;
int id;
Names name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
check = (Button) findViewById(R.id.Check_button);
text = (EditText) findViewById(R.id.editText1);
ImageView view = (ImageView)findViewById(R.id.imageView1);
final ImageView incorrect = (ImageView)findViewById(R.id.incorrect);
switch (getIntent().getIntExtra ("clicked_position", -1))
{
case 0:
view.setImageResource(R.drawable.adese);
id = R.drawable.adese;
break;
case 1:
view.setImageResource(R.drawable.birvar);
id = R.drawable.birvar;
break;
case 2:
view.setImageResource(R.drawable.agaoglu);
break;
case 3:
view.setImageResource(R.drawable.akinsoft);
break;
default:
view.setImageResource(R.drawable.afra);
id = R.drawable.afra;
}
name = Names.forDrawable(id);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a=name.isCorrect(text.getText().toString());
if(a==true){
Intent intent = new Intent(LogoActivity.this, CorrectActivity.class);
startActivity(intent);
}
else{
incorrect.setVisibility(0);
incorrect.setVisibility(4);
}
}
});
}
}
I hope I could explain my intention clearly.
There are number of ways to do this. Because this is not problem this is functionality that you want in your project.
According to my logic I am giving you one solution.
Steps:
Create one static array with the length of your items in grid view with default value is 0.
static int[] items = {0,0,0,...};
Now check in getView methode of grid view with the value according to position in array that if it is 0 then it will looks like normal otherwise it will look with different (Here you want transparent with check box this will you get by adding one more image on it.)
If(items[position] == 0){
}else{
}
Now when user taps any image and navigate to other screen (Suppose Activity B) that time pass value of that position from that array and check in Activity B's OnCreate().
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(LogoSelectionActivity.this, LogoActivity.class);
intent.putExtra ("clicked_position", position);
intent.putExtra ("answered_or_not", items[position]);
startActivity(intent);
}
And if answered means if its value is 1 then show accordingly other wise as it is.
And when it user give answer unanswered question make its value 1 in that array.
I hope it will help you can ask if any question.

Get the values from one layout to next layout?

I've listed some items in list that extends Activity and the list are listed using Custom Adapter. My question is, this is my xml File I've added some items to this spinner. How can i get the spinner values to next layout. Anyone knows then please tell me? Advance thanks.
I'm not clear on what you're actually asking here, but as a guess there are two possible things you're asking
How to get the currently selected value out of the Spinner
How to set the same value to a spinner in the next layout
1.
Is simple enough
((Spinner)findViewById(R.id.spinner1)).getSelectedItem()
Will return the object selected by your spinner.
Is slightly more complex, you'll need to determine what index in the data supplied corresponds to the result you get from getSelectedItem(), for example if you had an array of Strings then you could search through until you found the index and then set it on the new spinner.
For example:
String[] options = new String[] {"One","Two","Three","Four"};
String val = (String)((Spinner)findViewById(R.id.spinner1)).getSelectedItem();
//.......pass this to a layout/activity etc.........
for (int i=0; i<options.length; i++)
{
if (options[i].equals(test))
{
((Spinner)findViewById(R.id.spinner2).setSelection(i);
break;
}
}
But your best bet would be to try and explain more clearly what you're asking.
first you have to select data from spinner using
spinnerobject.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Spinner spinnerobject = (Spinner) findViewById(R.id.Spinner02);
string value = (String)spinnerobj.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
then u hava to use intent for sending it to next activity..
Use
Intent.putExtra(..):
intent.putExtra("keyName", "somevalue");
This method is overloaded and takes various types as second argument: int, byte, String, various arrays..
To get the data out use appropriate getXYZExtra(). For String this is:
getStringExtra(String keyName)
First thing :: you can pass value from one activity to second activity not one layout to second
second :: if you need to pass value from one activity to second use
first activity::
activity.putExtra("lastpage", lastscore5);
*here lastpage is key which unique for aplication
second activity::
Intent i1 = getIntent();
var = i1.getIntExtra("lastpage", 1);

ListView with a dynamic intents that change a TextView and WebView on each call

Activity that is sending the putExtra()
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent();
// Set up different intents based on the item clicked:
switch (position)
{
case ACTIVITY_0:
intent.putExtra("Value1", "Display this text!");
intent.setClass(this, com.a.someclass.class);
}
startActivityForResult(intent, position);
}
Activity receiving the putExtra()
#Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.information);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
if (value1 != null) {
informationTitle = (TextView)findViewById(R.id.informationTitle);
informationText = (WebView)findViewById(R.id.informationText);
informationTitle.setText(value1);
}
Original Message:
i have been searching everywhere for a good tutorial on this, i have posted my code online so people can look at it but have not found the help i needed.
I am new to this and what i am basically trying to do is just have a list of items which are all linked to one class that has a dynamic TextView that will be used for the title and a WebView for content, And so basically when a item is clicked on the list it will open up the new activity/intent and it will also pass arguments to change the TextView and WebView accordingly.
I know how to open a new activity by making a new class for each item on the list but i am pretty sure there is an easier way where i can reuse one class and just keep changing the TextView and WebView. The reason i say this is because i have 15 items on my list, but that will expand overtime so i dont want to be making 50-60 different classes to open up each new item.
If some could point me to the right tutorial or give me some insight on here i will really really appreciate it!
Thank you
To accomplish that, you would, instead of using a different Intent for each list item, call the same Activity with the same Intent, but pass extras along with it.
Let's say, for instance, that you want to pass a different String depending on which list item is clicked. You would want to
myIntent.putExtra(String key, String value);
startActivity(myIntent);
The Activity that you start with this Intent will then be able to grab these extras in its onCreate() using
Bundle extras = getIntent().getExtras();
and access the extras you put in the Intent using the methods outlined here. This way you can use a single Activity for all list items but have the Activity display different information based on the extra values.
I'll give you a link where you can get the best tutorial for that...
http://www.vogella.de/articles/Android/article.html

Categories

Resources