简体   繁体   English

测试用户是否从 AutoCompleteTextView 中选择了一个项目

[英]Test if a user has selected an item from AutoCompleteTextView

My problem is that my code does not react accordingly whenever an user selects an item from an AutoCompleteTextView.我的问题是,每当用户从 AutoCompleteTextView 中选择一个项目时,我的代码不会做出相应的反应。
flag is a variable which is set to a value whenever one item from each AutoCompleteTextView has been selected. flag是一个变量,每当从每个AutoCompleteTextView中选择一个项目时,该变量就会设置为一个值。 If it's set to 1, then it means it's right and it should proceed to main activity.如果它设置为 1,则表示它是正确的,它应该继续进行主要活动。 Otherwise, a toast is displayed on click of button whose onClick calls the method callMainActivity .否则,单击onClick调用方法callMainActivity的按钮时会显示吐司。
There are no errors.没有错误。 Gradle build is successful, but clicking on that button (mentioned above) does nothing at all. Gradle 构建成功,但单击该按钮(如上所述)根本没有任何作用。
Code:代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

public class Location extends AppCompatActivity {
    private static int flag=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);
    int city = android.R.layout.simple_dropdown_item_1line;
    int area = android.R.layout.simple_dropdown_item_1line;
    int store = android.R.layout.simple_dropdown_item_1line;

    String []city_array = getResources().getStringArray(R.array.City);
    String []area_array= getResources().getStringArray(R.array.Area);
    String []store_array= getResources().getStringArray(R.array.Store);

    List<String> city_list= Arrays.asList(city_array);
    List<String> area_list= Arrays.asList(area_array);
    List<String> store_list= Arrays.asList(store_array);

    ArrayAdapter<String> adapter_city = new ArrayAdapter(this,city, city_list);
    ArrayAdapter<String> adapter_area = new ArrayAdapter(this, area, area_list);
    ArrayAdapter<String> adapter_store = new ArrayAdapter(this, store, store_list);

    final AutoCompleteTextView autocompleteView_city =
            (AutoCompleteTextView) findViewById(R.id.City);
    final AutoCompleteTextView autocompleteView_area =
            (AutoCompleteTextView) findViewById(R.id.Area);
    final AutoCompleteTextView autocompleteView_store =
            (AutoCompleteTextView) findViewById(R.id.Store);

    autocompleteView_area.setAdapter(adapter_area);
    autocompleteView_city.setAdapter(adapter_city);
    autocompleteView_store.setAdapter(adapter_store);
    autocompleteView_area.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg0) {
            autocompleteView_area.showDropDown();
            if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
                flag=1;
            else
                flag=0;

        }
    });
    autocompleteView_city.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg0) {
            autocompleteView_city.showDropDown();
            if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
                flag=1;
            else
                flag=0;
        }
    });
    autocompleteView_store.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View arg0) {
            autocompleteView_store.showDropDown();
            if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
                flag=1;
            else
                flag=0;
        }
    });

// This is the newly updated part //这是新更新的部分

           autocompleteView_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
            //... your stuff
            if(autocompleteView_area.getListSelection()>0) {
                flag = 1;
                System.out.println(flag + "flag at area");
            }else
                flag=0;


        }
    });

}

public void callMainActivity(View view){
    if(flag==1) {
        Intent in = new Intent(getBaseContext(), MainActivity.class);
        startActivity(in);
    }
    else
        Toast.makeText(getBaseContext(),"Please select all fields properly",Toast.LENGTH_LONG);
}

} }

The reason you are not seeing the Toast or changing activities, is because you are never calling callMainActivity(View view) in your code.您没有看到Toast或更改活动的原因是因为您从未在代码中调用callMainActivity(View view) Add this line to the end of all your OnClickListeners : callMainActivity(arg0) -- if this does not work, put some log statements in your OnClickListeners to check if they are triggering or not.将此行添加到所有OnClickListenerscallMainActivity(arg0) -- 如果这不起作用,请在OnClickListeners放入一些日志语句以检查它们是否正在触发。

Also, if you want to trigger the call when an item from your AutoCompleteTextView result list is selected, you should use an AdapterView.OnItemClickedListener instead.此外,如果要在选择AutoCompleteTextView结果列表中的项目时触发调用,则应改用AdapterView.OnItemClickedListener This will notify you when an item is selected from the AutoCompleteTextView list, or when nothing is selected and then you can react accordingly.这将在从AutoCompleteTextView列表中选择一个项目时通知您,或者当您没有选择任何内容时,您可以做出相应的反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 AutoCompleteTextView 所选项目创建 Toast? - How to create Toast from AutoCompleteTextView selected item? Android-从自动完成文本视图中识别选定的项目 - Android - identify selected item from an autocompletetextview 如果未选择 AutoCompleteTextView 中的任何项目,则显示错误 - Displaying Error if no item from a AutoCompleteTextView is selected AutoCompleteTextView未填充所选项目 - AutoCompleteTextView not populating with selected item 选择项目后无法从android AutoCompleteTextView获取用户输入的文本 - Can't get user entered text from android AutoCompleteTextView after item selected 如何从AutoCompleteTextView获取所选项目的数组索引? - How to get array index of selected item from AutoCompleteTextView? 检查从AutoCompleteTextView中选择的项目在RecyclerView中已经存在 - Check the Item selected from the AutoCompleteTextView already exist in the RecyclerView 使用从AutoCompleteTextView中选择的项目的相应数据设置隐藏字段的值 - Set value of hidden field with corresponding data of item selected from AutoCompleteTextView 如何使用自定义适配器从 AutoCompleteTextView 获取所选项目 - How to get the selected item from a AutoCompleteTextView with a custom Adapter AutoCompleteTextView检测用户编辑的列表中的选定条目 - AutoCompleteTextView detect when selected entry from list edited by user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM