I have a ListView populated with the names of US states from a database. To transition to a new screen, I want to select a state in that ListView and then hit a button that transitions to a screen of cities in that state.
I think I know how to populate the ListView, but I do not know what the inner button workings should look like. Plus, I do not know how to use to carry the state info through the transition to use it to list cities.
Does this code have anything to do with this?
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
blah.setCurrentListID(arg3);
Let me know. Thanks.
First, assuming you use an adapter class that extends ArrayAdapter with states, like:
public class StatesItemAdapter extends ArrayAdapter<UsState>
Then you have a List like ArrayList as the data source for the ArrayAdapter right? Then,
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
UsState state = statesList.get(position); // on ArrayList
// or try statesList.get(listView.getSelectedItemPosition());
Intent citiesIntent = new Intent(getApplicationContext(), CitiesActivity.class);
citiesIntent.putExtra("state_name", state.getName()); // Name string originating from database record
startActivity(citiesIntent);
}
And then in the Cities Activity, in onCreate you must get the name of the state and use it as a criteria in your database query to get a Cursor of city results:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle bundleExtras = intent.getExtras();
String selectedState = bundleExtras.getString("state_name");
// Do database query on cities relating to state
.......
Related
In my activity I have two listviews with some data. On first listview selected row will be highlighted on click and on click on 2nd listview new activity starts.
I want to send the highlighted row data of first listview and clicked row of 2nd listview data on next activity. How can Iachieve that?
supposing your lists are named list1 & list2, add to your list2 onItemClickListener that should looks something like this :
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
String highlightedItem = (String)list1.getSelectedItem();
String clickedItem = (String)list2.getItemAtPosition(position);
Intent intent = new Intent(FirstAcivity.this, SecondActivity.class);
intent.putExtra("highlightedItem", highlightedItem);
intent.putExtra("clickedItem", clickedItem);
startActivity(intent);
}
and then in your Second Activity you can receive the items like this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
if (intent != null){
String highlightedItem = intent.getStringExtra("highlightedItem");
String clickedItem = intent.getStringExtra("clickedItem");
}
}
I have assumed your list items are of type String but you can apply the same logic for any other type.
In case your want to send object not just primitives you need to make your objects implements Serializable or Parcelable interface.
i am displaying only three details from my database in 1 row of the listview after the user clicks on this list item all the details should be made visible in another activity in a list view.i tried but m getting a blank activity to open instead of a list..
ListViewDetails.java
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
int appno=cursor.getInt(cursor.getColumnIndexOrThrow("appln_no"));
Intent objintent=new Intent(getApplicationContext(),DisplayDetails.class);
objintent.putExtra("countryCode", countryCode);
startActivity(objintent);
}
});
here m passing an appno parameter to the next intent so that details related to this appno are displayed in DisplayDetails.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listdisplay);
Intent intentobj=getIntent();
int appno=intentobj.getIntExtra("appln_no", 0);
WayDataBase way=new WayDataBase(getApplicationContext());
ArrayList<String> listvalues=way.getListDetails(appno);
if(listvalues.size()!=0)
{
ListView lv=getListView();
ListAdapter adapter=new ArrayAdapter<String>(DisplayDetails.this, R.layout.view_animal_entry, R.id.animalName, listvalues);
lv.setAdapter(adapter);
}
}
but the screen is just balnk..
whats the issue??? please help! thanks!
Shiv,
You have fetched the values in variable "appno" but set values from variable "countryCode" instead of "appno".
In your DisplayDetails.java, you are trying to fetch it from the the variable "appln_no" which is incorrect.
If i look at your code then it seems that you want to pass appno value to another activity
so should keep it like this:
ListViewDetails.java
objintent.putExtra("countryCode", appno);
DisplayDetails.java
int appno=intentobj.getIntExtra("appln_no", "countryCode");
I am developing an application in which when user clicks on a item of a listview, a new page appears and in this page only the selected items are displayed. I mean for a particular list item only previously saved items are displayed.
In this new page i have 3 checkboxes. So when user clicks on a particular list item the new page display the checkbox with previous saved state for that particular item.
Now I dont know how to do that.
thanx in advance!!
This is my list onItemClick :-
OnItemClickListener ocl= new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
String name= list.get(arg2)[2];
Toast.makeText(Profile.this, "session of.." + name, Toast.LENGTH_LONG).show();
Settings.setName(name);
Intent intent= new Intent(Profile.this, ProfileConfig.class);
startActivity(intent);
}
};
lvChildProfile.setOnItemClickListener(ocl);
Now i want that when clicking the saved instance of checkbox appear.
You can keep the state within your objects itself.
Model implements Serializable {
boolean isOptionOneChecked
boolean isOptionTwoChecked
boolean isOptionThreeChecked
int id
...
// Constructor
// Getters & setters
}
This means your Adapater will have a list of objects from the type Model. How you want to display these in your ListView, can be done with a custom Adapter but that's not the real question.
When you click on an Item in your list, you can retrieve the object (like you already did) with
Model lModel = list.get(position);
In order to display the Model in a different Activity, you can send it along with your Intent as such:
Intent intent= new Intent(Profile.this, ProfileConfig.class);
intent.putExtra("MySelectedObject", lModel);
startActivity(intent);
Because our class implements the Serializable interface, we can retrieve the object in our new Activity as followed:
getIntent().getSerializableExtra("MySelectedObject");
You will have all the information about this object so you can create you layout accordingly.
I have this ListView setup:
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long id) {
//
Intent i = new Intent(getApplicationContext(), Rate.class);
startActivity(i);
}
});
setListAdapter(new ArrayAdapter<String>(Items.this, R.layout.list,
items));
It is a standard ListView as you see. Each row has a single TextView populated with the List "items". I am pulling that in from a MySQL Database and outputting through a JSON loop.
I simply want to be able to click on the row, with its single name, and take that name and pass it into the Rate.class activity as a String. How can I do this in code?
Add putExtra to your intent (see below). I am assuming you have the list 'items' as the member of the activity, the arg2 argument is basically the index of the item clicked in the list, which you can use to get the name from the 'items' list.
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long id) {
//
Intent i = new Intent(getApplicationContext(), Rate.class);
i.putExtra("name", items.get(arg2));
startActivity(i);
}
You could create a specific array adapter instance instead of just creating one to pass to the function. Then in the OnItemClick() function, int arg2 is the index in the array adapter that the user selected. You can get the view from the array adapter and getText() from the text view.
One of the things passed to onItemClick is the view that was clicked:
Cast view to the appropriate type and call getText() on it; for example:
final String text = ((TextView)view).getText();
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