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

时间:2016-03-08 18:49:45

标签: android autocompletetextview

我的问题是,只要用户从AutoCompleteTextView中选择一个项目,我的代码就不会做出相应的反应 flag是一个变量,只要选择了每个AutoCompleteTextView中的一个项目,该变量就会设置为一个值。如果它设置为1,则表示它是正确的,它应该进入主要活动。否则,点击按钮onClick调用方法callMainActivity时会显示祝酒词 没有错误。 Gradle构建成功,但点击该按钮(如上所述)什么都不做 代码:

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;
        }
    });

// 这是新更新的部分

           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);
}

}

0 个答案:

没有答案