gpt4 book ai didi

android - 单击未在 AutoCompleteBox 中设置的项目时的 AutoCompleteTextView

转载 作者:行者123 更新时间:2023-11-29 02:30:00 24 4
gpt4 key购买 nike

我想在 AutoCompleteTextView 中显示项目。它工作正常,所有下拉项目都显示。但根据我的需要,我不想在单击项目时在自动完成框中设置项目。我怎样才能做到这一点?

public class AutoCompleteViewActvitiy extends Activity {
AutoCompleteTextView autoCompleteTextView;
String[] language;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.support_simple_spinner_dropdown_item);

//after calling this service then you will get resposne ...in post method
new CallServiceForFetchResponseOfCategory().execute();

}


public class CallServiceForFetchResponseOfCategory extends AsyncTask<String, Void, String> {
public static final String REQUEST_METHOD = "GET";
public static final int READ_TIMEOUT = 15000;
public static final int CONNECTION_TIMEOUT = 15000;

@Override
protected String doInBackground(String... params) {
String stringUrl = params[0];
String result;
String inputLine;

try {
URL myUrl = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection)
myUrl.openConnection();
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.connect();
InputStreamReader streamReader = new
InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
while ((inputLine = reader.readLine()) != null) {
stringBuilder.append(inputLine);
}
reader.close();
streamReader.close();
result = stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
result = null;
}

return result;
}

protected void onPostExecute(String result) {
super.onPostExecute(result);
//in response you will get category array ....
//like
then you will set array into this :
language = .......;
then
setResponse();
}

}

private void setResponse() {

ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.select_dialog_item, language);
//Getting the instance of AutoCompleteTextView
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView.setThreshold(1);//will start working from first character
autoCompleteTextView.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
autoCompleteTextView.setTextColor(Color.RED);

autoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
// first srevice again and again call for fetching the result and show in autocomplete

if (autoCompleteTextView.getText().toString().trim().length() > 0) {
new CallServiceForFetchResponseOfCategory().execute();

}

}
});

autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//according to id i will call this service but issue is that when i click on item it will set default in autocomplete text box and
//again afterTextChanged will call then again CallServiceForFetchResponseOfCategory hit, that is the issue
// i dont want call this time CallServiceForFetchResponseOfCategory service when i click on item...
new FetchingCityDataAsynkTask().execute();


}
});
}

//

public class FetchingCityDataAsynkTask extends AsyncTask<String, Void, String> {
public static final String REQUEST_METHOD = "GET";
public static final int READ_TIMEOUT = 15000;
public static final int CONNECTION_TIMEOUT = 15000;

@Override
protected String doInBackground(String... params) {
String stringUrl = params[0];
String result;
String inputLine;

try {
URL myUrl = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection)
myUrl.openConnection();
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.connect();
InputStreamReader streamReader = new
InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
while ((inputLine = reader.readLine()) != null) {
stringBuilder.append(inputLine);
}
reader.close();
streamReader.close();
result = stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
result = null;
}

return result;
}

protected void onPostExecute(String result) {
super.onPostExecute(result);
//setresponse here
}

}
}

最佳答案

这是我在我的一个项目中所做的。我只是将 AutoCompleteTextView 的文本值放到 ""

articlesAutocomplete.setOnItemClickListener(
(detailedArticleAdapterView, childView, position, id) ->{
DetailedArticle selectedArticle = (DetailedArticle)detailedArticleAdapterView.getItemAtPosition(position);
/*logic with selected article*/
articlesAutocomplete.setText("");
}
);

我希望这是你想要实现的:)

编辑:我在您的评论中看到您使用了 TextWatcher,您为什么要使用它?它可能会改变我的解决方案的用处 ^^

关于android - 单击未在 AutoCompleteBox 中设置的项目时的 AutoCompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50056812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com