Getting a particular detail from XML file in android - 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.

Related

Pass selected item data from ListView from current activity to next activity

I have a Custom ListView of products. Each row of listview contains product name, image, price and description. I want to send the listview's selected item's title, price, description and image be passed from current ListView activity to next activity where it is to be displayed. How do I pass data of selected list item in intent?
This is part of code from my java file which displays the list:
private ArrayList<ThemeTourModel> GetThemeTourResults(){
ArrayList<ThemeTourModel> results = new ArrayList<ThemeTourModel>();
ThemeTourModel item_details = new ThemeTourModel();
item_details.settourname("Solo Woman Travellers");
item_details.settourDescription("Women searching the ultimate liberation may discover it in exploring the tourist destinations of world on their own. More freedom and security while traveling for leisurely getaways and adventures.");
item_details.setImageNumber(1);
item_details.setPrice("Rs.6999 - Rs.8999");
results.add(item_details);
return results;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
if(position==0){
Intent i0 = new Intent(this,EnquireActivity.class);
startActivity(i0);
}
You can put the primitive data through the Intent to another activity by intent.putExtra(name,value) and get the values through getIntent().getExtra() on the another Activity. You can find the reference here: http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle)
The other way is to let your item object implement Parcelable to exchange between activities, check this out: Android: How to implement Parcelable to my objects?.
Hope this helps

Automatic click on ListView if has only one item

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);
}

how to get the saved items for a particular listitem in android?

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.

Get specific item property on onListItemClick() using 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);

Pass custom list item content to new activity when list item is clicked from a listview

First of all I want to know if this is possible. Essentially I have a listview that is being populated programatically with a custom list item view and a custom adapter. This works like a charm but the problem is that I have to sort this list in a specific way once it is populated. The values that are being put into the list are stored in String arrays. The problem is that when I sort the listview it changes the index of where I thought the values would be stored. I want to start a new activity with certain values found in the list view. Is this possible? I could take a string from the list item and can look it up in my arrays storing the values when that item is clicked. I can then start a new intent with extras so that I can pass this new data to the new activity. So, is it possible to extract a string from a list item?
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3){
Object o=lv.getItemAtPosition(position);
UtilityClass u=(UtilityClass)o;
String s=u.getMyData();
Intent edit = new Intent(context, AnotherActivity.class);
edit.putExtra("your_identifier", Integer.toString(position));
startActivity(intent);
}});
If u have implemented onItemClickListner / onItemSelectListner then you can get a callback onItemClicked()/ onItemSelected() from there using the Adapter get the item from selected position. and the same can be sent to another activity using a bundle/extra

Categories

Resources