Automatic click on ListView if has only one item - android

I am trying to search for some data in Mysql database, then display it Listview
When more than 1 result is found, clicking on any one of them will take you to its respective details page.
But.. I am looking to go to details page directly if there is only one result in list view. Put simply, how would I call onItemClick on the list view automatically when there is only one item in the list.
My Listview's onClickItemListener looks like this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// when a student is selected then the following methods are called
DetailsFragment.setLUNCH_TYPE(searchResult.get(position).lunchtype);
Log.d("Lunch type selected", ""+DetailsFragment.getLUNCH_TYPE());
((HomeActivity) getActivity()).startStudentSale(searchResult
.get(position).name.toString(),searchResult
.get(position).isStudent,searchResult.get(position).id);
// it was comming null here when running this this need to find out why
// ((HomeActivity)getActivity()).setmSaleType(SALE_TYPE.SEARCHED_STUDENT);
}

When you know the count of the resultSet, say cnt and if it is 1, i.e.
if(cnt==1){
Intent intent=new Intent(getApplicationContext(),DetailActivity.class);
startActivity(intent);
}

Related

Saving additional context with a view

I have GridView which shows a list of thumbnails. After clicking on one of them I'd like to navigate to another activity with detailed view of a corresponding "thing".
How should I store some additional data/identifiers such that on click I can pass it to the detailed activity? By default I only get position and id. I'd like to somehow store my custom identifier which I use to query external services for details.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(this, MovieDetails.class);
intent.putExtra(MY_ID, <some_extra_ID>);
startActivity(intent);
}
});
EDIT:
For example, I have a dynamic list of dishes with only photos of that dish (using GridView). When user clicks on one of them I want to show a detailed view with a recipe and comments.
For that I need to pass name of that dish to load information in the detailed view.
Actually it is as good as a click listener can get as it passes the view, position and id. However you can improve the data you get by defining a unique item id inside your adapter.
If you are using an ArrayAdapter or SimpleAdapter the id is usually the same as position but for a CursorAdapter or SimpleCursorAdapter the id returns the row id of the table.
Now you can extend you adapter and return the desired id inside the getItemId method:
public class MyAdapter extends ArrayAdapter<MyItem>{
...
#Override
public long getItemId(int position) {
return myItems.get(position).myUniqueId;
}
}

Getting a particular detail from XML file in android

I am working on a CRM app in android, in which, I am showing details of all contacts in a list view. Now, my requirement is when I click on a particular item in the list, it should only display the details about selected contact, such as, name, address, email, etc.. The data is coming from XML file, which I am parsing using SAX Parser. How can I query a XML to get selected data?
You are filling the ListView using Adapter right? Now you can get the item at the selected view inside the ListView and pass this item to an Activity.
E.g. inside your Adatper class implement the onItemClickListener:
public void onItemClick(AdapterView<?> a, View v, int position, long l) {
// Remembers the selected Index
Data item =getItem(position);
Intent intent = new Intent(getApplicationContext(), DetailedActivity.class);
intent.put("object",item);
startActivity(intent);
}
Note: the item "Data" class should implement the Parsable interface so it can be passed to the Activity in your DetailedActivity onCreate method get that object and update the UI based on its Values.

How can I reload a single activity with new data each time the activity is started?

I have a list view with over 100 items. Each of these items show description when clicked on them. The problem is that I don't want over a hundred activities to display the description of each item instead I want to implement a single activity which refreshes the whole view and sets it to the new data depending upon the item clicked.How can I achieve this?
Thank you.
Create a new activity that only displays the details of your selected item. This activity is created for each item but you would open it, press the back button open it again with different content. Have a look at Intent and their extras as how to pass the data to your details activity.
try like this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i=new Intent(yourActivity.this,next.class);
String s = this.getListAdapter().getItem(position);
//query the database for the row with this string.. and get the necessary information
i.putExtra("name",s1);
i.putExtra("id",s2);
startActivity(i);
}

How to trigger onListItemClick in a ListFragment

In the tablet layout of my app, I have three ListFragments and one regular Fragment, let's call them Make, Model, Size, and Details. Initially the Make list is populated, then based on the Make selection, the Model list is populated; when a Model is selected, the Size list is populated; when a Size is selected, Details are shown. Each of these events (list item selection) is handled through the onListItemClick handler.
On startup, I want to populate the Make list, select the first Make in the list and have it go through the onListItemClick handler to populate the Model list (and so on so that all lists are populated and the details are shown - this should also be the behavior when any selection is made in any of the lists - select the first item in the next list until we get to showing the details). Note that I have control of the DB and for every Make there will always be at least one Model, for each Make/Model at least one Size, for each Make/Model/Size exactly one Detail.
So, I want to select the first item in the list and have it trigger the onListItemClick handler. I've tried the following (with appropriate bounds checking, etc), but it doesn't work.
getListView().setItemChecked(0, true);
The only changes to an "out of the box" ListFragment are to set the CacheColorHint as such
getListView().setCacheColorHint(R.color.GhostWhite);
where GhostWhite is set in the styles.xml as
<color name="GhostWhite">#88FFFFFF</color>
Any ideas?
Thanks in advance.
To select the first item in the list, first get focus from touch, then select the first item, then trigger the onclick handler
int position = 0;
getListView().requestFocusFromTouch();
getListView().setSelection(position);
getListView().performItemClick(getListView().getAdapter().getView(position, null, null), position, position);
Try setSelection(int position)
http://developer.android.com/reference/android/widget/ListView.html#setSelection(int)
1.) In the OnActivityCreated of your list fragment create an interface like so
public interface ListItemSelectedListener {
public void onListItemSelected(int index);
}
2.) Implement the interface in your activity
3.) Create private ListItemSelectedListener selectedListener in your list fragment
4.) Set selectedListener.onListItemSelected(0) in the OnActivityCreated.
This is how it worked for me using the Android Support Package - should be similar on HC
Try adding this,, it works in my project:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent = new Intent();
intent.setClass(getActivity(), someActivity.class);
startActivity(intent);
}
in The Fragment class in onActivityCreated(Bundle savedInstanceState) method, you can add the following:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//if the parent is in the landscape, make the first item selected
if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
try {
if (mItems.size() > 0) {
//notify the activity that an item is selected
mListener.onMasterItemSelected(mItems.get(0));
}
} catch (Exception e) {
Log.e("exception", e.toString());
}
}
}

How to show another List View 'B' after clicking List View 'A'

I'd like to show another List View 'B' after clicking a item of List View 'A'. I use onListItemClick event in Android 1.6 project.
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(this,
"You have selected " + lv_arr[position],
Toast.LENGTH_SHORT).show();
}
how to code it?
If you want to stay on the same Activity and layout you could use a ViewSwitcher, which is designed for flipping between two views.
However I would strongly suggest that the click triggers a new local Activity via an Intent. This will have a new Layout containing your second ListView. This is because users will expect that having clicked and had the display significantly change, that pressing the back button will take them back to the original location in the app. As a general rule, any user action that changes the conceptual location in the application should be accompanied by an Activity change.
I could see the List View by adding
<activity android:name=".WhiteListView"/>
in AndroidMainfest.xml.
How about try ExpandableListView. When you click on the groupview it expands to show childviews. It has a nice BaseExpandableListAdapter.
For example I call a new activity using Intents, with packed values added this way...
#Override
public void onListItemClick( ListView parent, View v, int position, long id) {
Intent lancon = new Intent(this, viewContact.class);
//lancon.putExtra("id", id);
//or
c.moveToPosition(position);
id = c.getInt(0);
c.close();
lancon.putExtra("id", id);
this.startActivity(lancon);
finish();
}
Then in the other class onCreate method I call:
this._id = this.getIntent().getLongExtra("id", 0);

Categories

Resources