I have a listview of store building locations. Each row contains address, phone, hours, etc. I added a button so that when pressed the dialer intent can be called.
My method is called when a button is pressed, but I can't figure out how to get the row #, which I need.
Here's what I have. How do I get the listview row # that this button child is in?
public void myPhoneClickHandler(View v)
{
int key_RowId =0;
Toast displayToast = Toast.makeText(branchPicker.this,
"You pressed:" + key_RowId, Toast.LENGTH_LONG);
displayToast.show();
}
<Button
android:text="Call"
android:id="#+id/bBranchCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:onClick="myPhoneClickHandler">
Thanks Gopal, it worked
Solution:
public void myPhoneClickHandler(View v)
{
ListView lv = getListView();
if (lv == null)
return;
final int position = lv.getPositionForView(v);
if (position < 0)
return;
Toast displayToast = Toast.makeText(branchPicker.this,
"You pressed:" + position, Toast.LENGTH_LONG);
displayToast.show();
}
You can call setTag on Button and in onClick call getTag on the Button View. Link about Set Tag
Used setTag and getTag in one of my project here is Link. Search for onClick method, Hope this helps in resolving your issue.
well, i guess you can do lvItems.getSelectedItem(), but why don't you use an onItemClickListener and attach that to the listview?
Related
I've got a card-view that each card has a button on it. I'm trying to get the position of the card that holds the button when the users clicks the button.
I have an adapter that extends from RecyclerView.Adapter and implements the following method...
public void onBindViewHolder(MovieImageViewHolder movieImageViewHolder, int i) {
Movie movieItem = mMovieList.get(i);
movieImageViewHolder.details.setTag(i);
movieImageViewHolder.details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
position = (Integer) v.getTag();
}
});
}
This works sucessfully, when I press a button on card 1, position equals 1 and when I press a button on card 4, position equals 4.
The question is, how do I get this value (position) out of the adapter and back into the Activity so I can use it? or do I even need to do that? I'm trying to create a new intent but don't think that should be done from the adapter?
I attempted to create a getter for the variable position, and call the primary button action with
public void buttonClick(View view) {
int position = adapter.getPosition();
Log.d(TAG, "Button Clicked::- " + String.valueOf(position));
}
Using
android:onClick="buttonClick"
In the XML, but nothing happens when clicking the button, I believe this is because the OnClick method in the adapter is fired instead?
EDIT:
Seems I've made some progress. Worked out that I can set the tag in the adapter
movieImageViewHolder.btn_details.setTag(i);
Then get that tag back in my onClick method with .getTag();
Only thing now is the tag seems to be zero for the first element, then zero for the second element, then starts at one for the third... Any reason why this would be?
This was actually very simple and a very common "issue" people face. I was set down the path that I couldn't create an activity from an adapter where infact that is exactly what I should have been trying to do.
movieImageViewHolder.details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
Integer taggedPosition = (Integer) v.getTag();
Intent intent = new Intent(mContext, ViewMovieDetailsActivity.class);
intent.putExtra(SearchMovie.MOVIE_TRANSFER, getMovie(taggedPosition));
v.getContext().startActivity(intent);
}
});
Correctly starts the correct activity.
i got a problem with my listview. basically consists of a list generated from a search in database, but when loading the list, only detects the first checkbox as checked or unchecked, ignoring the rest.
the code is this:
public void AnyadirProdLista() {
// TODO Auto-generated method stub
ListView lista = (ListView) view.findViewById(R.id.ListaProds);
CheckBox check = (CheckBox) view.findViewById(R.id.checkLista);
int contador = lista.getCount();
for (int i = 0; i < contador; i++){
if (check.isChecked()){
Toast.makeText(getActivity(), "a" + i, Toast.LENGTH_LONG).show();
}
}
}
when i do this and click the button that activate this method, only if the first check box of the list ( the 0 position item in the list ) makes work the toast and do it all the loop, but the other checkbox do nothing.
I do focusable false the checkbox in the view, and don't know nothing more to do, if someone can help me i will be very thankful.
CheckBox check = (CheckBox) view.findViewById(R.id.checkLista);
you get one check box and you only check the one you got above
if (check.isChecked()){
Toast.makeText(getActivity(), "a" + i, Toast.LENGTH_LONG).show();
}
while you iterate throw your list you should overwrite "check" with an other checkbox
I have a ListViewwith BaseAdapter which its items are LinearLayout. Inside LinearLayout, there're a TextView and a RadioGroup.
I wrote a OnItemClickListener for listview. However, when I clicked the item inside ListView. Nothing response!
I want to know how could I find out which view was being Clicked. Is there any method like Log.setShowClickingView(true)?
First you need to have an event in your Xml that is something like this
android:onClick="myCheckMethod"
Remember you need to declare /write this statement for your button and radio button.
Now In your Java file (Class) declare the method and match it with the view and you will know which view was clicked
public void myCheckMethod(View v) {
// does something very interesting
String Caller = "";
if (R.id.DI == v.getId()) {
Caller = "DI";
} else if (R.id.btnExp == v.getId()) {
Caller = "Exp";
}
Log.i(Tag,"View Clicked was:"+Caller);
}
Edit User requested to know how to know the ID
getResources().getResourceEntryName(int resid);
or
getResources().getResourceName(int resid);
You'll prefer the first one I guess.
When using OnItemClickListener, one of the things returned is the position within the listView of the item that was clicked:
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getBaseContext(), "Item clicked: " + position, Toast.LENGTH_LONG).show();
}
});
In the above example, "int position" represents the item that was clicked. A toast message is displayed with the index of the item that was clicked.
I have a activity which has a button and 2 autocomplete widget. for the button i am using
addProductButton.setOnClickListener(this);
and for the 2 autocomplete widget i am using
supplierTextView.setOnItemClickListener(this);
now when i select the first autocomplete it runs this code but does not go into the if loop also when i click a item in the 2nd autocomplete it runs the same code but without going into the else
#Override
public void onItemClick(AdapterView<?> adapter, View view, int pos, long rowId) {
// TODO -
String supName = supplierTextView.getText().toString();
String proName = productTextView.getText().toString();
System.out.println("Name selected "+ view.getId());
if(view == supplierTextView)
{
Log.d("Supplier Name selected", supName);
}
else if(view == productTextView)
{
Log.d("Product Name selected", proName);
}
loadProducts(supName);
handleProductSuccess(filteredProduct);
}
On this statement.
if(view == supplierTextView)
{
Log.d("Supplier Name selected", supName);
}
else if(view == productTextView)
{
Log.d("Product Name selected", proName);
}
the type of the view is TextView so nothing happened.
If you want to know which of the 2 AutoCompleteTextView is triggered.
call this statement
supplierTextView.setOnClickListener(listener);
and
productTextView.setOnClickListener(listener);
and in onClick()
public void onClick(View view)
{
if(view == supplierTextView)
{
Log.d("Supplier Name selected", supName);
}
else if(view == productTextView)
{
Log.d("Product Name selected", proName);
}
}
don't forget to implement View.OnClickListener
The OnItemClickListener is used to listen for clicks on autocomplete text field items.
That is, when you click an autocomplete suggestion, you will receive the View corresponding to that suggestion along with its index and not the AutocompleteTextView itself.
Use the OnClickListener on your AutocompleteTextViews if you want to be notified when the views themselves are clicked.
If you want to react to changes in text content of your text field you can use the TextWatched listener interface. See TextView API for more information on that.
If you want to give click event to auto complete view then change setOnItemClickListener to setOnClickListener. Put if else condition in onClick method. It will work
I have a list, this list is basically TableRows in scrollview which I have inflated with layout inflater, i.e. it depends at runtime, how many rows are being added. Now I have a button also attached in each row. This button is either a delete button or an edit button. Before programming I was thinking that I will just get the row id and call my delete function for that row id. But now when I program I see that I do not find a way to get the row id, because the button is just the same button always and how will button come to know with what row exactly it is linked to. I am basically doing database programming where I have taken lots of user input and displayed it in a table row inside a scroll view. But I don't know how do I get those rows. As of now I am not using any array list or array adapter. Do I need to use them either to solve my this problem ?
Please help.
I am entering the my code here
if(dbExists)
{
myName = (TextView)findViewById(R.id.myName);
db.open();
Cursor c = db.getAllTitles();
long NoOfRows = c.getCount(); //here I am gettin 30 as entered in database
while(NoOfRows >= 1)
{
c.moveToFirst();
//.........Inflate here name and number........
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflated = inflater.inflate(R.layout.name, myTableLayout);
TextView userName = (TextView)findViewById(R.id.myName);
userName.setText(c.getString(1));//here I am settng the user entered name
Button delButton = (Button)rowView.findViewById(R.id.Delete_Name);
delButton.setTag(RowId--);
delButton.setOnClickListener(this);
c.moveToNext();
NoOfRows--;
}
db.close();
}
#Override
public void onClick(View v) {
Long rowId = (Long)v.getTag();
if (rowId != null)
{
Toast.makeText(this, "rows get Tag() " + rowId, Toast.LENGTH_LONG).show();
//db.deleteTitle(rowId);
}
}
You can use setTag/getTag on a View so set and get special information for that view.
Using this you would call button.setTag(rowId) and then retrieve it later using getTag in your onClick method.
Here is a detailed answer with a code example.