I've an issue with onItemclicklistener.
I'm getting real time data from firebase
What I want is, getting real time orders and when I click any specific order, it will go to another activity where specific details of that specific order should be shown.
How I get this?
Switch Statements will not work in this scenario.
How I get some id that will match in database, so that I can get that particular details.
I tried this method
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String name=(String)adapterView.getItemAtPosition(i);
Intent i=new Intent(order.this,details.class);
i.putExtra("CATEGORY", name);
startActivity(i);
}
});
and in details class
Intent i = new Intent();
String name=i.getStringExtra("CATEGORY");
databaseReference=FirebaseDatabase.getInstance().getReference(Constans.CATEGORIES).child(name);
But it is crashing.
Assuming that the ListView contains the order names and the item on which the user click is the name of the order, to get that name of the order you need to use this code inside onItemClick() method:
String orderName = (String) adapterView.getItemAtPosition(position);
Hope it helps.
You can pass the item_id with firebase notification and store it in the listview adapter.
Later you can pass this id to the new activity by putting in extras.
Related
I am kind of new to android studio, I have an array of strings for my list and when i click on an item from that list it takes me to the correct page. However, there are 100 items on the list and it's not logical to create 100 java files for all of them to set the text on that page.
My question is: is there a way to set the text to the correct item of list just after or before clicking that item?
Assuming you have in your FirstActivity a ListView please use the following code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String item = (String) adapterView.getItemAtPosition(position);
Intent intent = new Intent(YourActivity.this, SecondActivity.class);
intent.putExtra("item", item);
startActivity(intent);
}
});
To get the item in your SecondActivity please use this code:
String item = (String) getIntent().getExtras().get("item");
yourTextView.setText(item);
Hope it helps.
I have a long list of number and i want to open dialer with same number which is clicked if i use
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123"));
startActivity(intent);
then I have to write code for every number I want to know how to use single code for every number please help me
ArrayList Phone_Numbers=new ArrayList();
First of all put all the number in the arraylist
then create a listview and set that arraylist to it.
Populating a ListView using an ArrayList?
then seta setonitemclicklisterner on that list for that have a look at below link
http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90
Explanation for below code
when you will click on the item on list below listener will be invoked and on that by using int position you can get the specific Phone Number.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// in itemValue you will have the phone number.
String itemValue = Phone_Numbers.get(position);
//now call the function to open the dialer with that specified number
open_Phone_Dialer(itemValue);
}
});
public void open_Phone_Dialer(String phone_Number) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phone_Number));
startActivity(intent);
}
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.
I have a ListView which is created with SimpleCursorAdapter.
The list represents merchants. When someone clicks on a merchant i want to view full details of this particular merchant.
on this list (lv1) im setting a listener
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
//Cursor merchant = (Cursor) adapter.getItem(pos);
Intent merchant = new Intent(v.getContext(), MerchantView.class);
merchant.putExtra("merchantPosition", pos);
startActivity(merchant);
}
});
How should I pass the data to merchant view in most optimal way?
I have static reference to adapter so I guess I could use somehow getItem call (just as in commented out line) and then pass it as putExtra to Merchant. If that is the way to do it how should I use getItem (I tried couple of times but failed to extract data that i want).
P.S Adapter is making sql query earlier to database with columns - ID,NAME,DESCRIPTION,STATUS
Thanks!
What I do in my Apps, is override the SimpleCursorAdapter.setViewBinder() to set the Tag of Views inside the ListView with the ID from the DB and pass this ID to the intent in the setOnItemClickListener(). Check this question which is a similar case to what you want to do
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