listview in a fragment jumping to other activities - java

Should there be an alternative for it?
Thank you for reading.

Try this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch(position)
case 0:
Intent myIntent = new Intent(getActivity(), OneActivity.class);
getActivity().startActivity(myIntent);
break;
case 1:
Intent myIntent = new Intent(getActivity(), OtherActivity.class);
getActivity().startActivity(myIntent);
break;
}
});

Related

How to add click to specific item in listView

I did ListView with some items, now I want when I click on specific item to send me to 'next' activity.
I write this code:
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
Intent intent = new Intent(DrugaStranica.this, MainActivity.class);
startActivity(intent);
}
});
But it is work for all item some, I want specific item to send me to specific activity.
Please use below code it may be helpful to solve your issue.
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
Intent intent;
switch (pos) {
case 0:
intent = new Intent(DrugaStranica.this, TestOneActivity.class);
break;
case 1:
intent = new Intent(DrugaStranica.this, TestTwoActivity.class);
break;
case 2:
intent = new Intent(DrugaStranica.this, TestThreeActivity.class);
break;
case 3:
intent = new Intent(DrugaStranica.this, TestFourActivity.class);
break;
default:
intent = new Intent(DrugaStranica.this, MainActivity.class);
break;
}
startActivity(intent);
}
});

changing layout and activity in spinner

First of all, I am fairly new at this, so bear with me.
I am trying to make a spinner where if you select an item, the layout and the activity will change, but I cant even implement the simplest of spinners.
so far the only code I've got is the one below, but no matter what I put in it doesn't do anything, so I must just not understand it, please be very specific in your answers. thank you. the first answer got me some of the way, but the "spinner spin..... spin.setAdapter(aa)" part is not accepted under OnCreate
String[] generations = { "Gen2", "Gen3", "Gen4", "Gen5", "Gen6","Gen7" };
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,generations);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
Intent intent2 = new Intent(this, gen2.class);
startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(this, Gen3.class);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(this, gen4.class);
startActivity(intent4);
break;
case 4:
Intent intent5 = new Intent(this, gen5.class);
startActivity(intent5);
break;
case 5:
Intent intent6 = new Intent(this, MainActivity.class);
startActivity(intent6);
break;
case 6:
Intent intent7 = new Intent(this, gen7.class);
startActivity(intent7);
break;
}
}
Here is a example, hope it helps you understand it better:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
switch (position){
// Check what position was selected
case 1:
//start activity depending on position
Intent intent = new Intent(this, ActivityOne.class);
startActivity(intent);
break;
case 2:
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
break;
case 3:
Intent intent = new Intent(this, ActivityThree.class);
startActivity(intent);
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

How to start Activity with Intent in ListView.setOnItemClickListener?

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

List view opens the same activity

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

Getting item position in ListView then use the if / else clause or switch statement

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.

Categories

Resources