I'm working on a project at school, and I need help implementing ListViews properly. I have to do the Settings and Help sections on the project and as of now, I can display what's on my list and it displays Toast messages of the selected item when I click it. My question is, how can I create and show the content that is inside that specific item? For example, I have an option "Edit Password" and that when I click it, it should display my "Edit Password" screen. This applies to both my sections.
This is what I have so far. It's basically the android ListView tutorial but I added my options on there. My question is, when I click one of the options on my list, how can I display its specific details? Like what I said before, if I click "Edit Password", I want it to go to a "Edit Password" screen. Or on Help, if I click let's say, "Credits", I want it direct me to the Credits page.
public class Settings extends ListActivity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.settings, SETTINGS));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] SETTINGS = new String[]
{"Set Refresh Rate", "Edit Password", "Delete Account"};
}
When you extend ListActivity you already have the OnItemClickListener implemented, you should override the method onListItemClick. In that method you should use an Intent to get to a new Activity where you will display the stuff you want:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(this, SecondActivityName.class);
i.putExtra("pos", position); //pass the position of the clicked row so we know what to show.
startActivity(i); // let's go to the other activity
}
SeconActivityName is an Activity that you should create and where you would show the other screen you want(remember to add the activity to the manifest file!):
public class SecondActivityName extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
int rowPosition = i.getIntExtra("pos", -1); //we now have the row's position that was clicked in the other activity.
// based on that you show the screen you want for example after a switch
switch (rowPosition) {
case -1:
//this is the default value, something has gone terrible wrong
//finish the activity and hide:
finish();
break;
case 0:
//row 0 was clicked so show the "Set Refresh Rate" screen
setContentView(R.layout.refresh_rate__screen);
break;
//do the same for the other rows;
//...
This will work if the screens for the various settings are not that different. If they are you'll probably have to implement a different activity for each one and start the appropriate activity in the onListItemClick based on the position parameter.
Related
I am just days old to android.In my application I have a List View displaying planets name, and when user clicks on the list view item i.e any of the planet name, another activity will gets opened in respect of which planet is clicked on list view, like suppose if user choose "earth", an activity gets opened having Text View display and on that text view, I am displaying some facts(say 10 facts) about planet Earth that I have stored in string-array items(displaying only 1 fact at a time). Now, when user swipes left or right,the text gets changed depending on whether it is swiped left or right.
Tasks that I've managed to perform
I am able to set the list view items i.e lists of planet and I've implemented the onItemSelectListener() to it.
I've created the 9 activities also for each planet and I am able to call the particular activity for which the item is clicked using Intent().
I have declared the facts for each planet in string-array items.But don't know how to set them on Text View.
Tasks that I'm trying to achieve
Like when click on Earth, I am starting another activity that is displaying the first fact from the string-array items. I want when user swipes left or right, the second fact will gets displayed.
P.S.. Though I am able to change the facts from string-array items, but for that I was using next and previous buttons in which I using a counter, but I want to use the swipe.
I tried several tuts about swipes but did understand much. Please help and suggest.
One more thing, I am having a Samsung Galaxy Y S5360, how to install plug-ins in it, so that I can test my application directly because my emulator sucks.(i've heard using Kies slows down the system-any other alternative you can offer)
Thanks a lot
MainActivity.java
public class MainActivity extends Activity {
ListView lv;
String[] list = new String[] { "Mercury","Venus","Earth","Mars","Jupiter",
"Saturn","Uranus","Neptune","Pluto" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adps = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_list_item_1,
list);
lv.setAdapter(adps);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
switch (pos) {
case 0:
Intent intent = new Intent(MainActivity.this,
Mercury.class);
startActivity(intent);
break;
// similarly for all other 8 cases
default:
break;
}
}
});
// final String[] quo = getResources().getStringArray(R.array.quotes);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Mercury.java
public class motivatinal extends Activity {
int counter = 0;
TextView mercury;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.motivational);
String[] mer = getResources().getStringArray(R.array.motivational);
mercury.setText(mer);
// do not know how to display string array items on text view.
// mercury.settext is showing error.
}
}
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.
I am working on android applications. In my project I need to create Listview. My layout i.e info.xml contains topbar with one image view, footer with other imageview. Also in the center of the layout I kept an imageview and on that I have created the Listview. Also I have created row.xml for the textview to display listitems. Now when I click on each listitem a new intent should be called...i.e when I click on 1st listitem page1 should open. Similarly if I click on 2nd listitem page2 should open and so on. So how could I do that. I am struggling for this since 3 days but didnt find any correct solution. Please hgelp me regarding this.
My Code:
public class Information extends Activity
{
private String[] Countries;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
Countries = getResources().getStringArray(R.array.countries);
ListView list = (ListView)findViewById(R.id.listnew);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row, Countries);
list.setAdapter(adapter);
registerForContextMenu(list); }
}
All you have to add is
list.setOnItemClickListener(this);
Then you let your class implement the OnItemClickListener interface and create this method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
Intent firstIntent = new Intent(this, MyClass.class);
startActivity(firstIntentIntent);
break;
case 1:
Intent secondIntent = new Intent(this, MySecondClass.class);
startActivity(secondIntentIntent);
break;
[... etc ...]
}
}
In this case, if the first item is clicked, it will launch the MyClass Activity, if the second item is clicked, the MySecondClass Activity will be launched, etc.
Yes, it is a bit tedious but it is the best way.
i have made menu list containing play, settings, exit. but clicking the button doesn take me to the desired activity the listner is not working..can any one help me solve the problem.. will b thankfull..
yes its a list view control..
there are two errors in my code one is that
## The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)## in LINE1
And the other one is ## View cannot be resolved to a type## in LINE 2
"Actually i want to shift from one screen to another when i click on the item in main menu screen"
Here is the code of main menu
public class MenuActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.list);
String[] items = {
getResources().getString(R.string.pla),
getResources().getString(R.string.sco),
getResources().getString(R.string.set),
getResources().getString(R.string.hel),
getResources().getString(R.string.qui)
};
ArrayAdapter < String > adapt = new ArrayAdapter < String > (this, R.layout.menu_items, items);
menuList.setAdapter(adapt);
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { //LINE 1 error
public void onItemClick(AdapterView <? > parent, View itemClicked, //LINE 2 error
int position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(
R.string.pla))) {
// Game
startActivity(new Intent(MenuActivity.this,
GameActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.hel))) {
// Help
startActivity(new Intent(MenuActivity.this,
HelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.set))) {
//Settings
startActivity(new Intent(MenuActivity.this,
SettingsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(
R.string.sco))) {
// Scores
startActivity(new Intent(MenuActivity.this,
ScoresActivity.class));
}
}
});
}
}
ListView lv = ...;
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3)
{
//Do Your Stuff
}
});
or you can make your custom adapter and write the button's click event
use intent
Intent play= new Intent(getApplicationContext(),Play.class);
startActivity(play);
there's lots of tutorial about menu
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent) -- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move.
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities like in your setOnItemClickListener.
Im really confused when trying to do this:
Lets say i want to build a app which allow you to read information about animals.
on my main class i have a listview which items: Cat, Dog, Snake, Rabbit and so on.
If i tap example Dog my other class and my other layout.xml will pop up. In this details.xml layout got a Header text, a picture and a maintext.
However the text is just null or nothing. So how do i do so the text will change depend on which item i clicked.
For Example i dont want text about cat and a cat picture when o pressed the Rabbit item.
Is this possible? Ive tried to call methods but i just get a FC if the method is not in the current class. ive also tried the intent extra but that try didnt went well.
thank you!
Your Friend ;D
If i tap example Dog my other class
and my other layout.xml will pop up
I guess you have implemented the onListItemClick
protected void onListItemClick(ListView l, View v, int position, long id)
This method has an id that you can add in the intent to send to the next activity to know which row (so which item) has been clicked.
So in the next activity, you get the right layout.xml and information depending on the id received.
Edit
Let's say MyFirstActivity is your list activity:
public class MyFirstActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"Hello","World","Foo","Bar"}));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(this,MySecondActivity.class);
intent.putExtra("id",id);
startActivity(intent);
}
}
Then, in onListItemClick, it sends an intent to MySecondActivity with the id as extra where I can retrieve it and get the info according to it
public class MySecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Long lngId = getIntent().getExtras().getLong("id");
Integer id = lngId.intValue();
String message = null;
switch(id) {
case 0:
message = "Hello";
break;
case 1:
message = "World";
break;
case 2:
message = "Foo";
break;
case 3:
message = "Bar";
break;
}
Toast.makeText(this,message + " was clicked",Toast.LENGTH_SHORT).show();
}
}
Hope this helps