1

I have setOnItemClickListener for AutoCompleteTextView and in the onItemClick, I have implemented what needs to happen based on the value selected. The program does this by looking at the index value of the selected item ( in this case "int arg2").

Here's the code

 mCommCode = (AutoCompleteTextView) findViewById(R.id.CommCode);
 ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, CommodityCode);

   mCommCode.setAdapter(mArrayAdapter);

   mCommCode.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                double finalCommodityValue = 0;
                double argument = ((double)arg2);

                if(argument < 26){

                finalCommodityValue = argument + 1.00;

                } 

The only trouble is - the value of int arg2 is always 0, as seen while Debugging. It seems there is a disconnect between what is clicked and what is passed to onItemClick.

Any thoughts on how to figure it out?

RmK
  • 1,408
  • 15
  • 28

2 Answers2

0

There might be an error in your code:

You are redefining finalCommodityValue:

finalCommodityValue = 0;
if(argument < 26){

    double finalCommodityValue = argument + 1.00;

} 

Which is a compile-error since you cannot have more than one variable of the same name inside a function.

Now, if the variable finalCommodityValue is defined outside of the function that contains the code you showed here, then you are shadowing it. This means that the finalCommodityValue inside your if (argument < 26) statement is different than the finalCommodityValue you defined outside your whole function. It is, in a sense, hiding the value defined outside of your function.

Also, creating a variable inside the if statement makes it exist only within the scope of the if statement (between {} ). You are not using it for anything in your code.

I assume you didn't have the compile-error since you can debug how your code runs. That means finalCommodityValueis defined as a class field. Thus, What you should do is remove double:

finalCommodityValue = 0;
if(argument < 26){

    finalCommodityValue = argument + 1.00;

} 

Now, finalCommodityValue in your function and within the if statement corresponds to the same finalCommodityValue defined globally in your class.

As far as why arg2 is always 0, I believe your problem is:

mCommCode = (AutoCompleteTextView) findViewById(R.id.CommCode);

You are using an OnItemClickListener in an AutoCompleteTextView. AutoCompleteTextView is a TextView so it only has one item. That is why you are always getting position 0. You need to use a ListView instead.

In you XML file you should have something like this:

<AutoCompleteTextView
    android:id="@+id/tvAutocomplete"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="15"
    android:hint="@string/search_hint" >

    <requestFocus />
</AutoCompleteTextView>

<ListView
    android:id="@+id/lvDataList"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="85" >
</ListView>

Further, create a class variable:

ListView searchList;

And in onCreate():

searchList = (ListView) findViewById(R.id.lvDataList);

Then in the function that contains the code you showed here, put an OnItemClickListener on the ListView and not on the AutoCompleteTextView :

searchList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position,
                long id) {

            Log.d("POSITION", position);

            // .. Rest of your code

        }

    });
nem035
  • 34,790
  • 6
  • 87
  • 99
  • Thanks for pointing out the error. I have edited my question. Any thoughts on why the value int arg2 received is 0 no matter what selection I make ? – RmK Sep 11 '14 at 15:29
  • How do you know arg2 is 0? How did you test it? Did you put a breakpoint and debug it? What is your code trying to achieve? – nem035 Sep 11 '14 at 15:37
  • I really appreciate the time and detail you have given in answering the question, but let me point out a couple of things - 1. finalCommodityValue has no usage once the onClickListener is finished, and hence I haven't declared it as global. 2. There is a reason I used AutoCompleteTextView, which I believe listview won't be able to solve. (The use case here is- in EditText, as and when I type, suggestions need to come up.) – RmK Sep 11 '14 at 16:05
  • I was referring to this example http://stackoverflow.com/questions/8644501/how-to-set-setonclicklistener-for-autocompletetextview – RmK Sep 11 '14 at 16:18
  • if `finalCommodityValue` wasn't a class-global variable then your code, before my suggested fix, would not be able to run. You would have a compile error when saying `finalCommodityValue = 0` without the type `double`. This means you could never even get to debugging run-time values because compiler wouldn't allow you to run. Therefore you would not be able to tell if `arg2` was equal to 0. I am not sure what is wrong with your code but also I am not sure if anything is wrong. You never said what you are trying to achieve and how do you know the position argument is always 0. – nem035 Sep 11 '14 at 16:33
  • To clarify your doubt, I had declared the variable as double inside the onClick listener, in the case of code that is copied just so that people understand it. I didn't want to past the entire code. As far compiler allowing me to run the code, trust my word on this, I have got the debugger pulling out each every data for me. Infact @larrytech has put it down perfectly what just happened.Anyway, thanks for your time. – RmK Sep 11 '14 at 19:15
  • @RmK I'm sure you ran the code, i'm just going off by what I see here. Btw my answer was the same as larrytech's :) I also told you that you need to use a ListView :) Look at the previous edit of my answer. – nem035 Sep 11 '14 at 19:56
0

I think your problem of arg2 being 0 is that there is no other item to be selected since the view object(mCommCode) you are using is a textview which cannot hold a list of individual items to select from. This role is accomplished using a listView Object. i think this one may solve your worries if you decide to use a list view for attaching the data source to:

mCommCode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {
                double finalCommodityValue = 0;
            double argument = ((double) position );

            if(argument < 26){

            finalCommodityValue = argument + 1.00;
            }
        });

You have to use a listView object to get this right.

Akah
  • 1,389
  • 14
  • 19
  • I guess, I don't have other option then !! I was trying to avoid this one. Anyway, let me get going with that. Thanks. – RmK Sep 11 '14 at 19:17