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
Related
I have 3 spinners for choosing class, division and subject. These 3 are dependent on each other. I want to enable division's spinner only after selecting a class, and enable subject's spinner after selecting both class and division. and i want to check whether spinners is enable or not when i click on the spinner.
I have done enabling and disabling spinners by using setEnabled like below:
spDivision.setEnabled(true);
spDivision.setEnabled(false);
And, it is working for me.
I have used onitemSelected listener
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
ClassDivData classDivData = (ClassDivData) parent.getItemAtPosition(position);
if(parent == spClass)
{
clsId = classDivData.getId();
// Showing selected spinner item
if(classDivData.getId()>0)
Toast.makeText(parent.getContext(), "Selected: " + classDivData.getId(), Toast.LENGTH_LONG).show();
}
else if(parent == spDivision)
{
divId =classDivData.getId();
// Showing selected spinner item
if (classDivData.getId()>0)
Toast.makeText(parent.getContext(), "Selected: " + classDivData.getId(), Toast.LENGTH_LONG).show();
}
else if(parent == spSubject)
{
subId =classDivData.getId();
// Showing selected spinner item
if (classDivData.getId()>0)
Toast.makeText(parent.getContext(), "Selected: " + classDivData.getId(), Toast.LENGTH_LONG).show();
}
enablingSpinners(); // to enable or disable spinners
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
function enableSpinners() is used to enable or disable spinners
private void enableSpinners() {
if(clsId >-1)
{
spDivision.setEnabled(true);
}
else
{
spDivision.setEnabled(false);
}
if(clsId >-1 && divId >-1)
{
spSubject.setEnabled(true);
}
else {
spSubject.setEnabled(false);
}
}
Note: -1 is the default value of spinners
My problem is i couldn't get the click of the spinner. My need is when i click on the spinner i want to know whether that spinner is enabled or not. Only then i could show the user that he have to select class first for selecting a division.
I have tried setOnClickListener with the spinner.
spDivision.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!spDivision.isEnabled())
{
Toast.makeText(AddHomeWorkActivity.this, "First choose a class", Toast.LENGTH_SHORT).show();
}
}
});
But i got error like below:
Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
And i tried that too. But got error like below:
Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
I have searched a lot for the solution to my problem and so many results. But i didn't get an actual solution to my problem
You should use setOnItemSelectedListener,
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
To know whether your spinner is enabled, use
if(spinner.isEnabled())
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 custom listview with three items. One of them is like "Add this to the DB" and when I click to it it inserts something to the DB.
What I want it to do is after doing the insert, change the text to "Delete this from the DB" and also the onClick method to call a method to delete that record instead a method to insert.
Is this possible?
Here is my code:
final String[] opcs = new String[]{"Resultados", "ClasificaciĆ³n", text_fav};
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.list_menutipo_item, opcs);
m_list.setAdapter(aa);
m_list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent newActivity = null;
switch( position )
{
case 0: ...
case 2: if (isConnected(m_context))
{
aƱadirFavorito();
}
break;
}
}
});
It's definitely possible. The view parameter to the onItemClick callback is the view on which you clicked, you can simply change that view's content. i.e. view.setText("Delete this from the DB").
Also you will want to distinguish whether the next click is "Add this to the DB" or "Delete this from the DB", doing a string comparison here like if("Delete this from the DB".equals(view.getText())) might not be of good practice, you can set a flag in the view like view.setTag(true) to indicate that the current view's content is "Delete this from the DB". and later you can use view.getTag() to get back the flag to do the comparison.
Boolean flag = (Boolean)view.getTag();
if(flag == null || !flag) {
view.setText("Delete this from the DB");
flag = true;
//... code to insert a record to DB
} else {
view.setText("Insert this to the DB");
flag = false;
//... code to delete a record from DB
}
view.setTag(flag);
I am using simple_list_item_multiple_choice with list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); to create a list of check boxes populated from a database query. I am then using onListItemClick to handle the clicking of the list, again that is working fine. What I can find no way of doing (after 5 days) is writing an if statement based on whether or not the check box attached to the list item is checked. What I need is the equivalent of the example below which works perfectly for a check box where I can use the android:onClick element to fire the method below.
public void onCheckboxClicked(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
This is critical to my app so any advice would be greatly appreciated. Below is the simpleCusrorAdapter is am using:
Cursor cursor3 = db.rawQuery("SELECT _id, symname FROM tblsymptoms WHERE _id IN ("+sympresult+") ", null);
adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_multiple_choice,
cursor3,
new String[] {"symname","_id"},
new int[] {android.R.id.text1});
setListAdapter(adapter);
ListView list=getListView();
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
I have solved the problem after finding this very useful blog item
I changed my onListItemClick to the following and it works like a dream:
public void onListItemClick(ListView parent, View view, int position, long id) {
CheckedTextView check = (CheckedTextView)view;
check.setChecked(!check.isChecked());
boolean click = !check.isChecked();
check.setChecked(click);
if (click) {
Toast.makeText(this, "Not Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Selected", Toast.LENGTH_SHORT).show();
}
}
If I understand correctly, each row in your list has a checkbox. When an item in the list is selected you want to be able to tell if the corresponding checkbox is checked?
Try to use the setTag(...) method on each list item View object. Then when onListItemClick() method is called you can call getTag(...) on the view (which will return your checkbox). I assume that you are using a custom Adapter to populate the list. While populating you want to call:
setTag( CHECKBOX_KEY, checkbox );
For example:
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckBox cb = (CheckBox)v.getTag( CHECKBOX_KEY );
boolean isChecked = false;
if( null != cb ) {
isChecked = cb.isChecked();
}
// .. do whatever you have to here...
}
Hope this helps...
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?