i hava got the code below. i want to start an activity when i click on a single item on the list. however when i do nothing happens. I alsow want every item to refer to the same intent calld "com.whiskey.app.view" and send a id variable that was given by the sql query. I browsed trough the code several times but i cant seem t get it to work.
public class MainScreen extends Activity implements OnItemClickListener{
public ListView whiskeylist;
public String[] DataArryWhiskey;
....
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig whiskeyrows = new DBConfig(this);
whiskeyrows.open();
DataArryWhiskey = whiskeyrows.getDataInArray();
whiskeyrows.close();
whiskeylist = (ListView)findViewById(R.id.listofWhiskey);
whiskeylist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , DataArryWhiskey));
// End db view of whiskey
}// end onCreate
// catch itemclick event from the main list.
public void onItemClick(AdapterView av, View v, int position, long l)
{
// TODO Auto-generated method stub
String[] listitem_data = DataArryWhiskey[position].split(","); // break passed sting into a array comma seperated
Bundle passingitems = new Bundle();
passingitems.putString("whiskey_id", listitem_data[0]);
Intent currentintent = new Intent("com.wiskey.app.view");
currentintent.putExtras(passingitems);
startActivity(currentintent);
}
Although the above answers work, I think that the problem in your current implementation is that you don't call:
whiskeylist.setOnItemClickListener(this);
I think this should do the work!
If your activity only cotains this ListView, you should use a ListActivity.
These are made specifically for activities that only contain lists.
One of the methods for ListActivities is onListItemClick. That one is specifically for clicking on items in lists, as the name says.
The reason your code doesn't work is because onItemClick isn't generally used for clicking in Lists, but for clicking other objects in Activities.
Try changing your code based on the samples here: ListActivity
Derive your class from ListActivity and remove the implements OnItemClickListener
Put below code in onCreate
setListAdapter(whiskeylist);
And then have this as your onItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String[] listitem_data = DataArryWhiskey[position].split(","); // break passed sting into a array comma seperated
...your code....
startActivity(currentintent);
}
Also refer to:
ListActivity
ListView
You have not added listener for click actions, try adding:
whiskeylist.setOnItemClickListener(this);
at the end of onCreate
You might also write anonymous OnItemClickListener as in here:
http://developer.android.com/resources/tutorials/views/hello-listview.html
Related
I've started learning android programming and I'm stuck, I'm not sure how to work with the listadapter,(the app should be a personal trainer, you get 3 activities where you choose like type of training, home or gym and intensity, based on what you choose it should show you the exercises) I've set a list and upon clicking on one of the two choices, the path to the next activity is created with an intent as seen in the code, however, the activity name is different than what is shown as text in the list. I feel my logic is failing here but can't find a way to do this other than if elses.
(and maybe it would be easier to just do this with buttons or anyway views of some sort and onclicklisteners)
2nd thing, can I move the position of the list and the text inside the list (to the middle for example) if it is programmed this way in java? Like in xml with padding or something? Thanks
public class WorkoutPlace extends ListActivity {
String classes[] = { "WORKOUTHOME" , "WORKOUTGYM" };
// these should be "HOME Workout", "GYM Workout"
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(WorkoutPlace.this,
android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String Home_Gym = classes[position];
try{
Class ourClass = Class.forName("totaltrainer.com." + Home_Gym);
Intent ourIntent = new Intent(WorkoutPlace.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
Define a parallel array that contain the actual classes, like this:
String classes[] = { "HOME Workout" , "GYM Workout" };
Class actualClasses[] = { totaltrainer.com.WORKOUTHOME.class,
totaltrainer.com.WORKOUTGYM.class };
In onListItemClick() do:
Intent ourIntent = new Intent(WorkoutPlace.this, actualClasses[position]);
startActivity(ourIntent);
Note: You really should change the names of your classes so they aren't all UPPERCASE. Use WorkoutGym instead of WORKOUTGYM.
I have to fetch the data from the database and populate it into the ListView. But everytime I am running it, it adds the data into the previous data...
for example..
1st run:-
A
B
C
2nd run:-
A
B
C
A
B
C
and so on..
Here is my code I am using where I am using NotifyDatasetChanged()
List<Tag> allTags = db.getAllTags();
for (Tag tag : allTags) {
lv1=(ListView)findViewById(R.id.lv1);
arraylist=new ArrayAdapter<Tag>(this,
android.R.layout.simple_list_item_1, allTags);
lv1.setAdapter(arraylist);
Log.d("Tag Name", tag.getTagName());
lv1.setOnItemClickListener(new OnItemClickListener() {
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_SHORT).show();
arraylist.notifyDataSetChanged();
}
});
}
I have searched many links, applied their answers in my code, But nothing is changed.
You are creating your adapter using allTags, so whenever you are changing the value of allTags by adding or removing more elements and then immediately calling notifyDataSetChange() it will reflect the changes in the adapter. What I can see here in your code is that inside OnItemClickListener you are not modifying your allTags hence calling notifyDataSetChanged is not effective.
I am kind of new to Android development. Could you please tell me, how to navigate to another screen using onListItemClick. I have 200 items in a ListView. If i click an item from the ListView, it has to navigate to another screen which should show the details of the clicked item.
To get what you want you can do something like this...
1>create another activity with certains controls in which you can place your values...
2>on itemclick() of listview...
a>create intent and set values to pass to new activity
b>start new activity with this intent.
3>in oncreate() of new activity
a>retrive the values from intent
b>populate your controls using the values...
I am assuming you are getting the list and detail of actor from database.
I have used the same walk through to display recipe detail of selected recipe in my app.
public class ActorList extends Activity {
ListView myActorList=null;
public final static String selectedActor_ID="will Keep Actor ID";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_actor_list);
//initialize controls
myActorList.setAdapter(adapter);
myActorList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v,
int position, long id) {
// TODO Auto-generated method stub
Intent i= new Intent(ActorList.this,ActorDetail.class);
// you have to pass the actor id to next activity
// you can get this actor id from argument of type "long"
i.putExtra(selectedActor_ID, String.valueOf(id));
startActivity(i);
}
});
}
}
//Other Activity To show Detail..
//get the ID of Selected Actor on other activity say "ActorDetail"
public class ActorDetail extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_actor_detail);
ActorID=getIntent().getStringExtra(ActorList.selectedActor_ID);
// now as you have the id here for that particular actor
//fetch the detail of that selected actor thru id and bind it to your layout.
}
}
You need to register the other activity in Manifest file also like this
<activity
android:name=".otherscreenActivity"
</activity>
What is the best way to add an OnClickListener to a ListActivity that's Endless?
I am using cwac-endless adapter for my endless list.
The problem I am encountering is only my first batch of data that populates the list get's the click listener. The new data that get's fetched doesn't get the onclick listener.
I've tried adding a onclick listener in the following manner.
I used ListActivity onListItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String url = prodList.get(position).getProdUrl();
Log.i("URL",url);
Intent webViewIntent = new Intent(ProductListActivity.this, ProductWebView.class);
webViewIntent.putExtra("URL", url);
startActivity(webViewIntent);
}
UPDATE Rookie mistake, the above code works fine, the problem was that I was referencing the original list that was passed in instead of referencing the list in my custom ArrayAdapter.
I have a ListActivity that displays a list of search results I grab from a webservice off the internet. I parse the XML I receive into an ArrayList<MyObjects> which I then bind to a ListView using my own adapter (as in MyObjectAdapter extends ArrayAdapter<MyObject>).
So then I want the user to be able to click on one of the items in the list. Each item has an identifier which will then be put into an intent and sent to a new activity (which then triggers a new webservice request based on this, downloads the rest of the data). But I don't know how to get this one property of the MyObject that was selected.
Here's the onListItemClick() method as it stands:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String myObjectId;
// I want to get this out of the selected MyObject class here
Intent i = new Intent(this, ViewObject.class);
i.putExtra("identifier_key", myObjectId);
startActivityForResult(i, ACTIVITY_VIEW);
}
If you are using ArrayAdapter you must initialize that adapter with an array, right? So, the better you can do is to use the postition that you get from onListItemClick and take the object from the original array.
For instance:
// somewhere you have this
ArrayList<MyObjects> theItemsYouUsedToInitializeTheArrayAdapter;
// and inside onListItemClick....
String myObjectId = theItemsYouUsedToInitializeTheArrayAdapter.get(position).getObjectId();
try this getIntent().getExtras().getString(key);