How to detect on what ListView onItemClick was happened? - android

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
}

Related

How to know item selected on spinner?

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;
.......
}

How do I open specific activities when selecting from an array in a navigation drawer?

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.

android spinner onselectitemclick listener no triger

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
}

Multiple OnClickListeners Android

I have 10 buttons set up which are the answers to ten questions. When a certain button is clicked, I have a switch statement set up in my onClick method shown below. My question is what is the best way to set up the OnClickListeners for all the buttons seeing that I need to pass 2 arrays to the onClick method in order to tell if it is correct or not? Also, I need to return and integer value. Thanks
public void onClick(View v, int[] qaarray, int questionorder) {
int x=0;
switch(v.getId())
{
case R.id.imageButton0:
if(qaarray[0] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton1:
if(qaarray[1] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton2:
if(qaarray[2] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton3:
if(qaarray[3] == questionorder){
// correct
}else{
//incorrect
}
break;
case R.id.imageButton4:
if(qaarray[4] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton5:
if(qaarray[5] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton6:
if(qaarray[6] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton7:
if(qaarray[7] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton8:
if(qaarray[8] == questionorder){
//correct
}else{
//incorrect
}
break;
case R.id.imageButton9:
if(qaarray[9] == questionorder){
//correct
}else{
//incorrect
}
break;
default:
throw new RuntimeException("Unknown button ID");
}
}
The OnClickListener only gives you one parameter, which is the View:
void onClick(View v);
But you don't have to pass the questions and 'order' to the method to have what you want. One of the technique you can use is the setTag() method of View:
int[] button = new int[] { R.id.imageButton1, R.id.imageButton2.... };
private class AnswerPair{
public int questionOrder;
public int answer;
}
public void onCreate(Bundle bundle){
for(int i=0; i<NO_OF_BUTTON; i++){
AnswerPair ans = new AnswerPair();
ans.questionOrder = i;
ans.answer = 0; // SET this
getViewById(button[i]).setTag(ans);
getViewById(button[i]).setOnClickListener(this);
}
}
public void onClick(View v){
if (v.getTag() == null) return;
try{
AnswerPair answer = (ans)v.getTag();
// Check answer == question order? index?
}catch(exception e) return;
}
You can implement as many OnClickListeners as you want and assign different listeners for each button.
public void onCreate(Bundle bundle){
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.myButton)
b.setOnClickListener(new MyListener());
}
private class MyListener implements OnClickListener {
#Override
public void onClick(View v) {
// Your code here
}
}
i think a lot of people know this already, but there's a shortcut you can use instead of having different instances of onClickListeners and assigning them in code using setOnClickListener(x).
In your button XML, give it the android:onClick property, and assign it a string you like, for example,
android:onClick="clickOne"
In the activity the sets this xml as its content view, create a method named clickOne with a View parameter.
public void clickOne(View view)
Whatever you place on this method will be executed when you click the button.

regarding showing different view on each item click in android application

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

Categories

Resources