Adding multiple event to listview - android

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

Related

How to start an activity from a custom ListView?

I have a custom listview in which I can add as many elements as I want. All of these elements must open an activity (which is called "Compile" in my project) with a few edittext stuff to fill. The problem is that I don't actually know what to write in my code to tell the app to open the Compile activity when one of the elements is clicked.
//(Obviously every element must open its Compile activity with its relative informations. For example: element "Pizza" must open the activity Compile in which there are all the infos I previously put about Pizza.)//
Thanks for the support
This is my code for the listview:
public class Sheet extends Activity {
private static final int DIALOG_ALERT = 10;
ListView list;
ImageView addBtn;
EditText input;
String[] items;
MyListAdapter adapter;
AlertDialog alert;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheet);
list = (ListView) findViewById(R.id.list);
addBtn = (ImageView) findViewById(R.id.addPlanBtn);
input = new EditText(this);
items = new String[1];
alert = new AlertDialog.Builder(Sheet.this).create();
alert.setTitle("Activity name: ");
alert.setMessage("Type a name for your activity: ");
alert.setView(input);
alert.setButton("add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString();
items[items.length - 1] = value;
adapter = new MyListAdapter(Sheet.this, items);
list.setAdapter(adapter);
String[] temp = new String[items.length +1];
for(int i = 0; i< items.length; i++)
temp[i] = items[i];
items = temp;
alert.dismiss();
adapter.notifyDataSetChanged();
}
});
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.show();
}
});
}
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Object selectedItem = parent.getItemAtPosition(position);
//Do something with your object. If it's a String and you want to send it to CompileActivity, do it like this:
Intent intent = new Intent(MainActivity.this, CompileActivity.class);
intent.putExtra("some-data-id", (String)selectedItem)
startActivity(intent);
}
}
In CompileActivity, you can use the sent data like so:
String selectItem = getIntent().getStringExtra("some-data-id");
Please keep in mind that Intents send copies of your data across activities. Changing this string does not change the one from the previous activity. It is just a copy.
Intents are basic building blocks for Android, so it is worth learning more about them. Here is a place to start
Add this to your listView's OnItemClickListener:
Intent newActivity = new Intent(MainActivity.this, CompileActivity.class);
//You could pass extra info here using newActivity.putExtra().
// Like newActivity.putExtra("Pizza", "Pizza name");
startActivity(newActivity);
In your list view activity class set "setOnItemClickListener" for your listview. And from there start a new activity.
listview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(view.getContext(),yourActivity.class);
//you can add some extras if you want like this
intent.putExtra("Key",Value);
startActivity(intent);
}
});
You can retrieve the value in called activity like this :
int variable = getIntent().getIntExtra("Key", someDefaultIntValue);
Please note you can replace int here with your datatype.

Add Intent to OnItemClickListener

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

Passing a variable from a listview to another activity

I have been struggling using some tutorial for passing a listview option to a new activity and make it the title (I will do other stuff with it later). I have set up a OnClickListener by what is best to put inside it
ListView listView1 = (ListView) findViewById(R.id.sportslist);
String[] items = { "Archery", "Badminton", "Cricket", "Dodgeball", "Equestrian", "Football", "Golf", "Handball", "Ice Hockey", "Ju Jitsu", "Karate", "Lacrosse", "Mountain Biking", "Netball" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
?????
}
});
Thanks
EDIT:Extra code
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);
For of all you will need to get the item that was selected:
final String selected = items[position];
Or as doctoror drive has suggested
final String selected = (String) parent.getSelectedItem();
Then you will need to pass that string as an extra to your new activity
Intent i = new Intent(getApplicationContext(), MyClass.class);
i.putExtra("name", selected);
startActivity (i);
And then finally in your next activity
Intent in = getIntent();
String name = in.getStringExtra(("name"));//gets name from intent
public void onItemClick(AdapterView parent, View view,int position, long id)
String str = items[position];
Intent in = new Intent(getApplicationContext(), NextClass.class);
in.putExtra("itemkey", str);
startActivity (in);
}
in public void onItemClick(AdapterView<?> parent, View view,int position, long id) add this code.
Intent i = new Intent(getApplicationContext(), NextClass.class);
i.putExtra("selectedItem", items[position]);
startActivity (i);
for getting value in NextClass Activitiy :
String SelectedItem = getIntent().getStringExtra("selectedItem");
Add the following to you Activity where you have listview
following is the variable that will contain the value that you want to pass to other activity
Declare it before Oncreate statement
// Activity_1
public final static String send_to_other_activity="ListViewSelected_ID";
Add the following code to the listView1.setOnItemClickListener
Intent i= new Intent(Recipe_List.this,Recipe_View.class);
i.putExtra(send_to_other_activity, string.valueof(position));
// itz (key-value) pair on the left key thru which u will access it on other place. on the right value that you want to pass
// Iam passing posiion to other activity here
startActivity(i);
Now on other activity extract this value from the key by adding following statement to the oonCreate of other activity
//Activity_2
getdata_from_list =getIntent().getStringExtra(Activity_1.send_to_other_activity);
now you have teh desired value in getdata_from_list

Listview , open new activity onClick

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

AutoCompleteTextView click event on Android

I have successfully implemented my AutoCompleteTextView which is based off an SQLite query and is placed in an array adapter. That's all working beautifully, however I can't get my onclickevent working.
I just want to create an intent to pass the selected value to a new activity. I know how to create an onclicklistener. I am just unsure about how to apply it to the dropdown box of the AutoCompleteTextView.
Nevermind. I've solved it. I was just executing poorly. The code below autocompletes my textview based off a simple SELECT SQLite statement and executes when the user selects the university from the dropdown list.
The onclick event creates a new intent and starts a new activity passing the selection to this activity within the intent.
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.ac_university);
String[] universities = myDbHelper.getAllUnis(db);
// Print out the values to the log
for(int i = 0; i < universities.length; i++)
{
Log.i(this.toString(), universities[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, universities);
textView.setAdapter(adapter);
//textView.setOnItemSelectedListener(this);
textView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(Main.this, Campus.class);
Bundle bundle = new Bundle();
bundle.putString("university_name", arg0.getItemAtPosition(arg2).toString());
bundle.putLong("_id", arg3);
intent.putExtras(bundle);
startActivity(intent);
}
putExtra function can be used for this purpose.
Here is an example...
Form the sending activity:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
ApplicationInfo x = appinstalled.get(pos);
PackageInfo y = appinstall.get(pos);
//Intent i = new Intent(InstalledPackages.this, Information.class);
i = new Intent(InstalledPackages.this, Information.class);
i.putExtra("i",x);
i.putExtra("j", y);
startActivity(i);
}
});
}
On the receiving side:
super.onCreate(savedInstanceState);
Intent myIntent = getIntent();
ApplicationInfo i = (ApplicationInfo)myIntent.getParcelableExtra("i");
PackageInfo j = (PackageInfo)myIntent.getParcelableExtra("j");

Categories

Resources