I have successfully call activity on an item. but the problem is I already add a bracket but It still calls the two activity even on other item on the list view.
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position==0);
{
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
if(position==1);
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
});
}
}
Never add a semicolon after an if statement. This
if(position==0);
{
// some stuff
}
is equivalent to
if(position==0)
{
// do nothing
}
{
// some stuff
}
which means that you always do "some stuff" irrespective of the value of position.
this
if(position==1);
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
should be
if(position==1){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
New:
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
if(position==1){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
}
});
BTW you u notice you are calling the same activity
Related
Im developing and App and I need to pass int data from a fragment to Another Activity. The problem its that it returns 0 or false.
First I start ActivityForResult putting a Bundle.
Then I pass a boolean and it works fine but when I try to put the int into the bundle from the fragment and comeback to onActivityResult it retuns 0 or false.
Can someone help please? I have tried with putExtras too and still false
public void newcampeon(View view) {
Intent i = new Intent(getApplicationContext(), Campeones.class);
b = new Bundle();
Boolean vengodraft = true;
b.putBoolean("vengodraft",vengodraft);
i.putExtras(b);
startActivityForResult(i,01);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int res = b.getInt("idcampeon");
aban1.setImageResource(res);
Toast.makeText(getApplicationContext(),String.valueOf(res),
Toast.LENGTH_SHORT).show();
}
------ Fragment on Another Acivity
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_top_lane, container, false);
b = getArguments();
if(b!=null){
Boolean vengodraft = b.getBoolean("vengodraft");
if(vengodraft==true){
final ImageView aatrox = v.findViewById(R.id.aatrox);
aatrox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = aatrox.getId();
getActivity().setResult(Activity.RESULT_OK);
b.putInt("idcampeon",id);
getActivity().finish();
}
});
}
}
return v;
It should return the current id of the Img
Finally I got what its wrong. Its very simple.
I just put correctly the sintax here. Just make a new intent and pass the arguments .
#Override
public void onClick(View v) {
String nombrecampeon = String.valueOf(aatrox.getTag());
Intent i = new Intent();
i.putExtra("nombrecampeon",nombrecampeon);
getActivity().setResult(Activity.RESULT_OK,i);
getActivity().finish();
}
});
And on ActivityResult
String uri = "#drawable/"+data.getExtras().getString("nombrecampeon");
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Drawable res = getResources().getDrawable(imageResource);
aban1.setImageDrawable(res);
Anyway, thanks all for the help :)
So I have a Spinner and I wan't user to select one item from it and then display his selection in another activity.. but Button won't even do anything.. I'm guessing error is in intent.putExtra of int position == 0.. can somebody help me out?
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
startActivity(intent);
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
This is the second activity
String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
Thank you!!
Here's the modified code of first activity:
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Toast.makeText(PrehranaInputMain.this, R.string.cilj_prehrana_nista_odabrano, Toast.LENGTH_SHORT).show();
} else if (position == 1){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getSelectedItem().toString());
startActivity(intent);
} else if (position == 2){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJeMrsavljenje", cilj.getSelectedItem().toString());
startActivity(intent);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
and second activity
String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
String prehrana2 = intent.getStringExtra("ciljJeMrsavljenje");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
// SEEMS LIKE SOME IF STATEMENT NEEDED HERE?
//ciljPrehranaRezultat.setText(prehrana2);
your listener to the drop down is not set until the user clicks the button, which means until you click the button at least once, selecting an item from the drop down doesn't do anything.
have you tried to select an item from the spinner after you click the button?
if you want to go to the other activity when the user clicks the button, keep only the startActivity(intent) in the onClick() method and move the onItemSelected() method up a level, move the intent as a global variable.
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intent);
}
});
also, even in that case, it appears to me that you are only jumping to the activity when the user selects item at position "0". can we assume this is not a blank space and your spinner only has 2 entries in it?
Change the following lines:
if (position == 0){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", (CharSequence)cilj.getItemIdAtPosition(0));
startActivity(intent);
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
To:
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemAtPosition(position));
startActivity(intent);
I want to start a new activity when clicking an item in my Listview. But when clicking an item, nothing happens. Nothing is in Logcat message. Both activities are declared in AndroidManifest.
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(view.getContext(), SecondActivity.class);
myIntent.putExtra("test", "hello");
startActivity(myIntent);
}
});
First of all, you need to set ItemClick listener in your listView object.
ListView yourListView.setOnItemClickListener
Then you need to pass activity's context in Intent
Intent myIntent = new Intent (view.getContext()ThisActivityName.this, SecondActivity.class);
Code snippet :
ListView yourListView = (ListView) findViewById(R.id.listviewid);
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(ThisActivityName.this, SecondActivity.class);
myIntent.putExtra("test", "hello");
startActivity(myIntent);
}
});
Hope it helps ツ
Try this for your ListView
final ListView list = (ListView) findViewById(R.id.list);
list.setItemsCanFocus(false);
Also, make sure that for CheckBox inside list item set focusable false
android:focusable="false"
android:focusableInTouchMode="false"
Source: setOnItemClickListener() not working on custom ListView # Android
try to use this code,
ListView listView=(ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(ActivityName.this, SecondActivity.class);
myIntent.putExtra("test", "hello");
startActivity(myIntent);
}
});
For Multiple option in ListView You can use follwing code.
lv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String s = lv_1.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
switch (position) {
case 0:
Intent newActivity = new Intent(MainActivity.this, Android.class);
startActivity(newActivity);
break;
case 1:
Intent newActivity1 = new Intent(MainActivity.this, Iphone.class);
startActivity(newActivity1);
break;
case 2:
Intent newActivity2 = new Intent(MainActivity.this, Window.class);
startActivity(newActivity2);
break;
}
}
;
});
I am using a spinner as navigation for my app, and need to know how to reset it when the user uses the back button. Currently when a user selects a page and goes back, the spinner is on the previously selected bar, and not the current page. Here is my current code.
public void spinnerNavigation(){
Spinner mySpinner = (Spinner) findViewById( R.id.spinner1);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {
if (i == 0) {
// current page
} else if (i == 1) { // Second item
Intent myIntent = new Intent(getBaseContext(), LearnActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
} else if (i == 2) { // Third item
Intent myIntent = new Intent(getBaseContext(), QuizActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
} else if (i == 3) { // Fourth item
Intent myIntent = new Intent(getBaseContext(), ForumActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// Do nothing
}
});
}
If you want to reset it to the first item:
mySpinner.setSelection(0);
_____________________
I have a listView, I'm wondering how to get the position of the item selected in the ListView so that I can use this code under the OnItemClick method.
string selected = listView.getItemPosition().toString()
So then I can use the if / else clause or switch to say:
if (selected.positionEquals("0")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
I hope I made sense, please note, the listView.getItemPosition().toString() and selected.positionEquals was something I made up so you can get an idea of what I want.
Thanks!
EDIT
I just saw this from another question, I searched before asking this, but this just popped out. Do you think it will work in my case?
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, 0);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, 0);
}
}
});
in one of the parameter of onItemClick there is a position value (third argument) of the clicked item. you dont need to get it progromatically, just use that value to know
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { //check third argument it automatically get the selected item POSITION
// TODO Auto-generated method stub
Intent i;
switch (position)
{
case 0: // first one of the list
i = new Intent(this, anActivity.class);
startActivity(i);
break;
case 1: // second one of the list.
i = new Intent(this, anActivity2.class);
startActivity(i);
break;
// and so on...
}
}
Try this code:
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
});
here "int position" will gives the selected item index value.
please see the code below :-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index,
long arg3) {
if (index == 0)) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
}
});
Using Below code Toast has print the position of selected item :-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),"Position of Selected Item is :-"+position,Toast.LENGTH_LONG).show();
}
});
yes working for you this way
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, position);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, position);
}
}
});
Also switch to will work for you,
and if you want compare using String like
string selected = listView.getItemPosition().toString()
then do like
if (selected.equalsIgnoreCase("0")){
// do work here
}else if(selected.equalsIgnoreCase("1")){
// do work here
}
end so on.