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;
}
}
});
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 have the following code:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " +item.toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(this, "You didn't select any preset", Toast.LENGTH_SHORT).show();
}
And the following array:
<string-array name="presets_array">
<item>Light Car</item>
<item>Medium Car</item>
<item>Heavy Car</item>
<item>Van</item>
</string-array>
It's working fine, when I select Light car it displays the correct text and so on with the others.
But I want to do more things, if selected thing is Van, change weight value, if medium car, other cost, etc.
I think I have to use a switch but I'm not sure how it works.
There are some options. You should choose the best which fits to you...
Just checking position
Good performance but if you change array order, you must re-order the switch statement
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " + item.toString(), Toast.LENGTH_SHORT).show();
swith(position) {
case 0:
// CODE for Light Car
break;
case 1:
// CODE for Medium Car
break;
case 2:
// CODE for Heavy Car
break;
case 3:
// CODE for Van
break;
}
}
String Checking
Low performance since you have to compare Strings several times in the worst case.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " + item.toString(), Toast.LENGTH_SHORT).show();
if(item.toString().equals("Light Car")) {
// CODE
} else if(item.toString().equals("Medium Car")) {
// CODE
} else if(item.toString().equals("Heavy Car")) {
// CODE
} else if(item.toString().equals("Van")) {
// CODE
}
}
you can write code similar to this code inside onItemSelected method:
switch (item)
{
case Light Car:
// TODD
break;
case Van:
// TODO
break;
.......
}
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();
}
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
}
In Activity I have two ListViews but now I must to detect on what ListView user clicked.
I have added adapters and setOnItemClickListener(this); for each ListView.
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
switch (v.getId()) {
case R.id.list_1:
Toast.makeText(this, "111111111", 0).show();
break;
case R.id.list_2:
Toast.makeText(this, "222222222", 0).show();
break;
}
}
But v.getId() returns -1
use a.getId() instead of v.getId()
i mean use AdapterView<?> a
switch (a.getId()) {
case R.id.list_1:
Toast.makeText(this, "111111111", 0).show();
break;
case R.id.list_2:
Toast.makeText(this, "222222222", 0).show();
break;
}
in onCreate(), get references to your listviews:
listview1 = findViewById(R.id.list_1);
listview2 = findViewById(R.id.list_2);
then, in onItemClicked() you can test like this:
if (a == listview1){
//something
}
else if(a==listview2){
//something
}