I am trying to get well versed with ListAvtivity. I have written some code that renders a List and when you click an Item in the List, you are taken to an Activity that gives some arbitrary information.
I am trying to figure some way in which, if the user click on an Item in the list, he is shown a Dialog Box with some information instead of going to a whole new Activity.
Source :
package com.mavenmaverick.listviewtest;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ActivityExample extends ListActivity{
static final String[] CHOICES = new String[]{
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
"Pluto",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 0:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 1:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 2:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 3:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 4:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 5:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 6:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 7:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
case 8:
startActivity(new Intent(ActivityExample.this,TestActivity.class));
break;
default:
break;
}
}
});
}
}
You can directly fetch your content in the onItemClick. Use the parent.getItemAtPosition() to do this. Since you are filling in your ListView with string arrays, the getItemAtPosition will return a string.
Your code should look like this:
getListView().setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String content = (String)parent.getItemAtPosition(position);
// do something with the dialog
showDialog(content);
}
}
You should google around ListView some more.
You can use/create a dialog within your onItemClick and set the title to the name of the planet and you can even set the view of the dialog to the style you want.
sample:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 0:
Dialog dialog = new Dialog(this);
dialog.setTitle(CHOICES[args]);
dialog.setContentView(R.layout.your_layout_to_dialog); //<-- if you want to add some view to your dialog
dialog.show();
break;
Try this dude it will help you......
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Youractivity.this);
// Setting Dialog Title
alertDialog.setTitle("AlertDialog");
switch (arg2) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
default:
break;
}
// Setting Dialog Message
alertDialog.setMessage("A Planet in the Solar System"+arg2+"listview Item clicked");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
Related
I have developed an App in Android which translates text (from EditText), Application uses Firebase API. it translates successfully when I select Source and Target Language in hard code. (FirebaseTranslatorOption's setSource and setTarget language settings even integer code i.e. EN (11), FR(17) etc. but it is not taking any dynamic input from Spinner object's setOnItemSelectedListener.
'''
//Spinners itemselection events
input_lang_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
selected_source_lang = parent.getItemAtPosition(position).toString();
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
case 1:
SourceLangCode=11;
break;
case 2:
SourceLangCode=17;
break;
case 3:
SourceLangCode=56;
break;
case 4:
SourceLangCode=48;
break;
case 5:
SourceLangCode=9;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
}
});
//end of input language selection
output_lang_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
selected_target_lang = parent.getItemAtPosition(position).toString();
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
case 1:
targetLangCode=11;
break;
case 2:
targetLangCode=17;
break;
case 3:
targetLangCode=56;
break;
case 4:
targetLangCode=48;
break;
case 5:
targetLangCode=9;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
}
});//end of output language selection
//firebaseTranslatorOptions object
FirebaseTranslatorOptions options =
new FirebaseTranslatorOptions.Builder()
.setSourceLanguage(SourceLangCode) //this is not taken as I select item
.setTargetLanguage(targetLangCode) //item selection is working
.build();
final FirebaseTranslator languageTranslator =
FirebaseNaturalLanguage.getInstance().getTranslator(options);
'''
I want to store the fMessage to the variable favoritekafe and return the favoritekafe after the onItemLongClick method ends.
Is that possible in any way?
OR can i accomplish with a different way to return a variable except true/false from "onItemLongClick" method?
Thats my code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.content.SharedPreferences;
public class MainActivity2 extends AppCompatActivity {
ListView kafeteries;
String favoritekafe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.coffee);
if (favoritekafe==null) {
Toast.makeText(getApplicationContext(), "you don't have a favorite
caffe yet", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), favoritekafe,
Toast.LENGTH_SHORT).show();
}
/* oi kafeteries se lista */
kafeteries = (ListView) findViewById(R.id.kafeteries);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>
(MainActivity2.this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.kafeteries_syros));
kafeteries.setAdapter(mAdapter);
// mnmta TOAST otan kanw click kapoio stixeio tis listas */
kafeteries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String sMessage = "";
switch(position) {
case 0: sMessage = "Coffee and drinks with hospitable locals\nArea:Ermoupoli\nPhone:2281083354"; break;
case 1: sMessage = "Coffee in the narrow streets of the city\nArea:Ermoupoli\nPhone:2281079225"; break;
case 2: sMessage = "The smallest and most adorable coffee in town\nArea:Ermoupoli\nPhone:2281300880"; break;
case 3: sMessage = "Coffee and snacks at the city's harbor\nArea:Ermoupoli\nPhone:2281076144"; break;
case 4: sMessage = "The city's most famous café\nArea:Ermoupoli\nPhone:2281085337"; break;
}
Toast.makeText(getApplicationContext(), sMessage, Toast.LENGTH_LONG).show();
}
});
// prospathia gia long clik add sta favorite kai save*/
kafeteries.setLongClickable(true);
kafeteries.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int position, long id) {
// prepei na vrw ena tropo na epistrefw kai to position gia na apothikevw tin agapimeni epilogi */
String fMessage = "";
switch(position) {
case 0: fMessage = "Boheme del mar is your favorite caffe"; break;
case 1: fMessage = "Jar is favorite caffe"; break;
case 2: fMessage = "Kouchico is your favorite caffe"; break;
case 3: fMessage = "Okio is your favorite caffe"; break;
case 4: fMessage = "Plaza is your favorite caffe"; break;
}
Toast.makeText(getApplicationContext(), fMessage, Toast.LENGTH_SHORT).show();
final SharedPreferences prefs=getApplicationContext().getSharedPreferences("settings",MODE_PRIVATE);
prefs.edit().putString(favoritekafe,fMessage).commit();
return true;
}
});
}
}
Create a function inside your class
void gotStringFromLongClick(String fMessage){
favoritekafe=fMessage;
// do anything else you want here
}
and call it like gotStringFromLongClick(fMessage); from outside switch
String sMessage = "";
switch(position) {
case 0: sMessage = "Coffee and drinks with hospitable locals\nArea:Ermoupoli\nPhone:2281083354"; break;
case 1: sMessage = "Coffee in the narrow streets of the city\nArea:Ermoupoli\nPhone:2281079225"; break;
case 2: sMessage = "The smallest and most adorable coffee in town\nArea:Ermoupoli\nPhone:2281300880"; break;
case 3: sMessage = "Coffee and snacks at the city's harbor\nArea:Ermoupoli\nPhone:2281076144"; break;
case 4: sMessage = "The city's most famous café\nArea:Ermoupoli\nPhone:2281085337"; break;
}
gotStringFromLongClick(fMessage);
// save value to preference
SharedPreferences.Editor editor = getSharedPreferences("pref", MODE_PRIVATE).edit();
editor.putString("data", fMessage);
editor.commit();
Retrieve value in onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.coffee);
favoritekafe = getSharedPreferences("pref", MODE_PRIVATE).getString("data", null);
if (favoritekafe==null) {
Toast.makeText(getApplicationContext(), "you don't have a favorite
caffe yet", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), favoritekafe,
Toast.LENGTH_SHORT).show();
}
You can store it in SharedPreference file.
inside your onLongClickListener do this
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString("fav_kafe", favoritekafe).commit();
then in onCreate before making the Toast load it to fafavoritekafe
like this
favoritekafe=PreferenceManager.getDefaultSharedPreferences(context)
.getString("fav_kafe", "nothing");
if (favoritekafe.equals("nothing") {
Toast.makeText(getApplicationContext(), "you don't have a favorite
caffe yet", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), favoritekafe,
Toast.LENGTH_SHORT).show();
}
I'm trying to figure out the right way to implement this feature into this algorithm. I would like to be able to open a specific activity for "Log History", "New Log", "Analytics", "Settings".
private void addDrawerItems() {
String[] osArray = { "Log History", "New Log", "Analytics", "Settings"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, NewLogActivity.class);
startActivity(intent);
}
});
}
Assuming you never change your array,
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0: //Log History
startActivity(this, LogHistory.class);
break;
case 1: //Log History
startActivity(this, NewLog.class);
break;
case 2: //Log History
startActivity(this, Analytics.class);
break;
case 3: //Log History
startActivity(this, Settings.class);
break;
default:
throw new InvalidArgumentException("wtf, unknown position");
}
}
I can't remember if position starts at 0 or 1, but it should give you an idea of what to do.
This is how i define the spinner
s_province = (Spinner) findViewById(R.id.s_province);
ArrayAdapter<String> provinceAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Data.provinces);
provinceAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_province.setAdapter(provinceAdapter);
s_province.setOnItemSelectedListener(this);
my class is implement from OnItemSelectedListener and i override this methods
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch (arg1.getId()) {
case R.id.s_province:
Log.d("here", "there");
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
but the onItemSelect is not triged , why please?
Two things:
If you actually want to check if your method is working put a Log statement outside the switch or in the default case so that you can be sure the method is being called.
You should be using arg2 since that represents position. Make your switch work with positions instead of whatever View is passed. Also rename your variables from the default names Eclipse assigns. arg0,1,2, etc are not helpful for you nor for anyone else looking at your code.
Eg
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(view.getContext(),"onItemSelected called", Toast.LENGTH_LONG).show();
int spinnerId = parent.getId();
if (spinnerId == R.id.s_province)
{
switch (position)
{
case 0:
//do something if first position was clicked
break:
case 1:
//do something else
break;
default:
//if for any reason no position matches.
break;
}
}
else if (spinnerId == R.other_id_in_xml)
{
switch (position)
{
case 0:
//do something if first position was clicked
break:
case 1:
//do something else
break;
default:
//if for any reason no position matches.
break;
}
}
//etc
}
I am developing an android application in which i have 5 items in a listview,I have to show different view on each click at the item,,,How to do this,also i have to put an arrow on the left and right side of the each item,,,
Here is my listview java code
http://pastebin.com/LsunQU9z
and here is my listview xml
http://pastebin.com/1S4uD5mH
Thanks in advance
Tushar
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
switch(position)
case 0:
//write for first item
break;
case 1:
//second
break;
case 2:
//third
break;
case 3:
//for
break;
}
});
Well it all depends on what you are going to do or show in those 5 items.
One way would be to only use one class, let's call it ShowActivity and then pass extras to that activity to see what it should show. And then just fetch that in onCreate in the ShowActivity.
One way would just to show it in the activity that you are in.
Two different examples are below:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Intent intent = new Intent(this,ShowActivity.class);
intent.putExtra("ITEM_INDEX",position);
startActivity(intent);
}
});
With the one above use something similar in the onCreate # ShowActivity
int id = getIntent().getIntExtra("ITEM_INDEX", 0);
switch(id) {
case 0:
//Show item 0
break;
case 1:
//Show item 1
break;
case 2:
//Show item 2
break;
case 3:
//Show item 3
break;
case 4:
//show item 4
break;
}
This is the second example, use this in your list activity.
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
switch(position) {
case 0:
//Show item 0
break;
case 1:
//Show item 1
break;
case 2:
//Show item 2
break;
case 3:
//Show item 3
break;
case 4:
//show item 4
break;
}
}
});