[英]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.将此行添加到所有
OnClickListeners
: callMainActivity(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.