Hey everyone I've looked for hours trying to find a solution to this, my goal is to have a Listview when it opens well open another activity. Well actually I got it to be able to open another activity when its click but how do I get it so that each list item will open its own activity? I am terribly sorry if this question is already answered but the links I found doesn't really describe what the code is doing [Yes i am a newbie :)]
this is the code im using
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.newfile, countries));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Intent myIntent = new Intent(view.getContext(), Html_file.class);
startActivityForResult(myIntent, 0);
}
});
}
}
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), Html_file1.class);
startActivityForResult(myIntent, 0);
}
if(position == 2) {
//code specific to 2nd list item
Intent myIntent = new Intent(view.getContext(), Html_file2.class);
startActivityForResult(myIntent, 0);
}
}
});
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position ) {
case 0: Intent newActivity = new Intent(this, i1.class);
startActivity(newActivity);
break;
case 1: Intent newActivity = new Intent(this, i2.class);
startActivity(newActivity);
break;
case 2: Intent newActivity = new Intent(this, i3.class);
startActivity(newActivity);
break;
case 3: Intent newActivity = new Intent(this, i4.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, i5.class);
startActivity(newActivity);
break;
}
}
If you have some limited number of list you can use switch case here on position
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Intent myIntent = new Intent(view.getContext(), Html_file.class);
startActivityForResult(myIntent, 0);
}
});
If you know which activity is to be opened when different list items are clicked, then just assign id or tag to the list items.
In the callback of onItemClick, you have a parameter View,
use it to get id or tag to differentiate them and call respective Activity.
// Add ArrayList and ArrayAdapter:
final ArrayList<String> listItems = new ArrayList<String>();
listItems.add("image_one");
listItems.add("image_two");
listItems.add("image_three");
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listItems);
myListView.setAdapter(myArrayAdapter);
// Add ArrayList of Classes:
final ArrayList<Class> intents = new ArrayList<Class>();
intents.add(image_one.class);
intents.add(image_two.class);
intents.add(image_three.class);
// Click on list item to open Class from ArrayList of Classes:
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Intent listIntent = new Intent(getApplicationContext(),
intents.get(position));
startActivity(listIntent);
}
});
SEE IMAGE OF CLASS NAMES HERE
Related
I want to get the item on list view to edit Text in another activity.
When clicked on list view item, I want to transfer the item in another activity in edit Text.
You have to make onItemClickListner of listview like that.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
}
});
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
And finally set Value to editText like this
editText.setText(value);
Hope this will help you.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("text", text want to transfer);
startActivity(intent);
}
});
You can make use of SharedPreferences. And when you pass the content of the ListView to the next activity, you can use editText.setText("Your Text").
You can also pass your data through intents from which you are calling your new activity.
create onClick method like this.
ListView list = (ListView) findViewById(R.id.newsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
NewsItem item = (NewsItem) adapter.getItem(position);
Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class);
intent.putExtra(KEY, item.getHeadline());
startActivity(intent);
}
});
In next activity
Intent intent = getIntent();
headline = intent.getStringExtra(KEY);
have a look here
I have an error when i click on item in activity they all show me the same string which his name: "name_0"
the activity which is open when we click:
public void handle() {
TextView var3 = (TextView) this.findViewById(R.id.name);
TextView var2 = (TextView) this.findViewById(R.id.meaning);
TextView var5 = (TextView) this.findViewById(R.id.proof);
Intent var4 = this.getIntent();
int var1 = var4.getIntExtra("name", 0);
switch (var1) {
case 0:
var3.setText(R.string.name_0);
var2.setText(R.string.meaning_0);
var5.setText(R.string.proof_0);
break;
case 1:
var3.setText(R.string.name_1);
var2.setText(R.string.meaning_1);
var5.setText(R.string.proof_1);
break;
case 2:
var3.setText(R.string.name_2);
var2.setText(R.string.meaning_2);
var5.setText(R.string.proof_2);
break;
}
}
and the activity who open the item onclick:
String[] values = { "test", "list", "ok", "bon" };
protected void onCreate(Bundle var1) {
super.onCreate(var1);
this.setContentView(R.layout.activity_main);
this.listView = (ListView) this.findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_item,
R.id.textView_list, values));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Intent Start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class);
startActivity(Start);
}
});
The problem is that you are not sending the position of the clicked item
Intent start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class);
start.putExtra("name", position);
startActivity(start);
That should work.
You always got 0 because you set 0 here (var4.getIntExtra("name", 0);) as the default value when the "name" key is missing.
You never set the 'name' extra on your Intent when you create it in the setOnItemClickListener, so when you try and retrieve the int in the 'handle()' method, you will always get 0.
You have to set the extra on the intent before you do 'startActivity'.
Intent startIntent = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class);
startIntent.putExtra("name", position);
startActivity(startIntent);
In order to get the text from the item you need to get the value from the view you get from the itemclicklistener
i.e
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
String text = ((TextView)view.findViewById(R.id.name)).getText();
Intent Start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class).putExtra("name",text);
startActivity(Start);
}
});
I have a custom List view and I been trying to get to next screen if user clicks an item. It doesn't seem to work. I tried if else, switch. It works if I don't use either but that way, every item will go to the same screen. Any help would be appericiated.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options_main);
OptionMainHeader option_list[] = new OptionMainHeader[]
{
new OptionMainHeader(R.drawable.clock_icon_48, "Helo"),
new OptionMainHeader(R.drawable.weather_cloudy, "Apple"),
new OptionMainHeader(R.drawable.weather_cloudy, "Mango"),
new OptionMainHeader(R.drawable.weather_cloudy, "Banana"),
};
OptionMainHeader1 adapter = new OptionMainHeader1(this, R.layout.option_main_header1, option_list);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.option_main_header, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int position = listView1.getSelectedItemPosition();
String selectedFromList =(String) (listView1.getItemAtPosition(position));
if (selectedFromList == "Helo"){
Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
startActivity(CurrentWaitTime);
} else if (selectedFromList == "Apple"){
Intent Appointments = new Intent(getApplicationContext(), no.class);
startActivity(Appointments);
} else if (selectedFromList == "Mango"){
Intent Logout = new Intent(getApplicationContext(), go.class);
startActivity(Logout);
} else if (selectedFromList == "Banana"){
Intent Exit = new Intent(getApplicationContext(), low.class);
startActivity(Exit);
}
}
});
}
Try this
ListView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView textView = (TextView) arg1;
String strText = textView.getText().toString();
if ("Helo".equals(strText)){
Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
startActivity(CurrentWaitTime);
} else if ("Apple".equals(strText){
Intent Appointments = new Intent(getApplicationContext(), no.class);
startActivity(Appointments);
} etc, etc
You can't compare Strings with "==". What you need to do is use the method String.equals(String). Something like:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedFromList = option_list[position].getTHATSTRING();
if (selectedFromList.equals("Helo")){
Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class);
startActivity(CurrentWaitTime);
} else if (selectedFromLis.equals("Apple")){
Intent Appointments = new Intent(getApplicationContext(), no.class);
startActivity(Appointments);
} else if (selectedFromList.equals("Mango")){
Intent Logout = new Intent(getApplicationContext(), go.class);
startActivity(Logout);
} else if (selectedFromList.equals("Banana")){
Intent Exit = new Intent(getApplicationContext(), low.class);
startActivity(Exit);
}
}
Try this, it will help you.
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
//global variable
idItem = (int) id;
switch (idItem) {
case 0:
// This is your first Item
// Add your Intent and startActivity
break;
case 1:
// Add your Intent and startActivity
break;
case 2:
// Add your Intent and startActivity
break;
case 3:
// Add your Intent and startActivity
break;
}
}
});
You get over id the item you clicked. Then you can go to the right activity you want for this item.
Hope it helps.
I have a listview ,I want that every button open different Activity.
listview there are many options,Each option will lead to another activity.
I didn't know how to do this.
Thanks.
Java
public class AndroidListViewActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] adobe_products = getResources().getStringArray(
R.array.adobe_products);
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
R.id.label, adobe_products));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String product = ((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), mavo.class);
i.putExtra("product", product);
startActivity(i);
} });}}
listen_item XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
I'm just guessing since you didn't say what went wrong with your code. Maybe the ArrayAdapter is using more than simply a TextView for the layout of each list item, so you can't simply pull the TextView out the way you're doing it.
Try this instead for getting the product string:
String product = adobe_products[position];
You will have to make String[] adobe_products final.
EDIT:
Based on what I think you're asking, try this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Class<? extends Activity> activityToStart = null;
switch (position){
case 0:
activityToStart = MyProduct0Activity.class;
break;
case 1:
activityToStart = MyProduct1Activity.class;
break;
//etc.
}
Intent i = new Intent(getApplicationContext(), activityToStart);
startActivity(i);
} });
Okie.. So here is what i understand from your comments. You have a listview and each item in the listview is a textview. And when you click on each item, you need to go to different screen / Activity.
If it is the case...
replace this code
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String product = ((TextView) view).getText().toString();
Intent i = new Intent(getApplicationContext(), mavo.class);
i.putExtra("product", product);
startActivity(i);
}
with this..
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Switch(position){
case 0:
String product = ((TextView) view).getText().toString();
Intent i = new Intent(AndroidListViewActivity.this, FIRST_SCREEN.class);
i.putExtra("product", product);
startActivity(i);
break;
case 1:
String product = ((TextView) view).getText().toString();
Intent i = new Intent(AndroidListViewActivity.this, SECOND_SCREEN.class);
i.putExtra("product", product);
startActivity(i);
break;
// Repeat the same for all screens
...
...
...
}
}
Note ::: I have not tested this code. This is just to give you an idea on how to do it.
Hope this helps..
I searched stackoverflow but couldnt find a answer(sure there is one somewhere) but I am trying to add a activity for each listview input. I can manage it with one intent but how do I give each listview input a seperate activity. So to be clear, I want to make every item have a serperate activity.
Currently use this code to initiate a activity but want individual activities for each item.
public class ListviewActivityActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.list_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, adobe_products));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), BMICalculatorActivity.class);
startActivity(i);
}
});
}
Have tried adding this but it failed to work
private static final int ACTIVITY_0 = 0;
private static final int ACTIVITY_1 = 1;
private static final int ACTIVITY_2 = 2;
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent();
// Set up different intents based on the item clicked:
switch (position)
{
case ACTIVITY_0:
intent.setClass(this, listview.activty.BMICalculatorActivity.class);
break;
case ACTIVITY_1:
intent.setClass(this, listview.activty.BodyLog.class);
break;
I know its probally simple to sort so any help would be amazing. Thanks
I think you want something like this :
Intent i = null;
switch (position)
{
case ACTIVITY_0:
i = new Intent(getApplicationContext(), BMICalculatorActivity.class);
break;
case ACTIVITY_1:
i = new Intent(getApplicationContext(), BodyLog.class);
break;
}
if(i != null)
{
startActivity(i);
}
Try something like this:
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i;
// selected item
String product = (String) lv.getItem(position);
if(product.equals("photoshop"))
i = new Intent(ListviewActivity.this, Photoshop.class);
else if (product.equals("final cut pro"))
i = new Intent(ListviewActivity.this, FinalCutPro.class);
else
i = new Intent(ListviewActivity.this, Generic.class);
startActivity(i);
}
});
Another option would be to make a POJO that held the name of the listview entry and the class to call. And add an array of those instead of an array of Strings. This will require using an ArrayAdapter of some kind. Then when you get the Item, you simply call startActivity(new Intent(listviewActivity.this, myPojo.classToCall); You should be able to find a good example of that somewhere on SO...