gpt4 book ai didi

java - fragment 中的多个 AutoCompleteTextView

转载 作者:行者123 更新时间:2023-12-02 03:37:10 26 4
gpt4 key购买 nike

我试图在 fragment 中添加第二个 AutoCompleteTextView(从 Web API 获取对象),但它从未检索到任何内容。

目前正在 API 级别 24 的模拟器上进行测试。我之前曾在另一个 Activity 中尝试过此 AutoCompleteTextView 的代码,并且它按预期工作。

fragment 代码

package com.advatek.timewin.fragments;

import android.app.AlertDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import com.advatek.timewin.MainActivity;
import com.advatek.timewin.R;
import com.advatek.timewin.helper.APICall;
import com.advatek.timewin.helper.APIHelper;
import com.advatek.timewin.helper.AutoSuggestAdapter;
import com.advatek.timewin.helper.JSONHelper;
import com.advatek.timewin.helper.JobSuggestAdapter;
import com.advatek.timewin.models.Employee;
import com.advatek.timewin.models.Function;
import com.advatek.timewin.models.PayPeriod;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link Clock.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link Clock#newInstance} factory method to
* create an instance of this fragment.
*/
public class Clock extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;


private Context context;
Handler handler;
Handler jobHandler;
AppCompatAutoCompleteTextView txtEmpSearch;
AppCompatAutoCompleteTextView txtJobSearch;
AutoSuggestAdapter autoAdapter;
JobSuggestAdapter jobAdapter;
private static final int TRIGGER_AUTO_COMPLETE = 100;
private static final long AUTO_COMPLETE_DELAY = 300;

private static List<Function> functionList;
PayPeriod period;


private TextView txtPayWeek;
Spinner spnJobs;

Button btnSignIn;

private OnFragmentInteractionListener mListener;

public Clock() {
// Required empty public constructor
}

public AppCompatAutoCompleteTextView getTextView(){
return txtEmpSearch;
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Clock.
*/
// TODO: Rename and change types and number of parameters
public static Clock newInstance(String param1, String param2) {
Clock fragment = new Clock();
Bundle args = new Bundle();

args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
context = getContext();
period = new PayPeriod();
functionList = new ArrayList<>();

try{
WeekTask wTask = new WeekTask();
wTask.execute().get();

// JobTask task = new JobTask();
// task.execute();

txtJobSearch = (AppCompatAutoCompleteTextView)getView().findViewById(R.id.txtEmpLookup);

jobAdapter = new JobSuggestAdapter(context, R.layout.support_simple_spinner_dropdown_item);

txtJobSearch.setThreshold(2);

txtJobSearch.setAdapter(jobAdapter);

// Fires when an Employee is selected
txtJobSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
InputMethodManager in = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
Function job = (Function) parent.getItemAtPosition(position);
((MainActivity)getActivity()).setJob(job);

}
});

txtJobSearch.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) {
jobHandler.removeMessages(TRIGGER_AUTO_COMPLETE);
jobHandler.sendEmptyMessageDelayed(TRIGGER_AUTO_COMPLETE, AUTO_COMPLETE_DELAY);
}

@Override
public void afterTextChanged(Editable s) {
if(txtJobSearch.getText().toString().equals("")){

}
}
});

jobHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == TRIGGER_AUTO_COMPLETE) {
if (!TextUtils.isEmpty(txtJobSearch.getText())) {
getJobs(txtJobSearch.getText().toString());
}
}

return false;
}
});
}
catch (Exception ex){
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("Error");
dialog.setMessage(ex.getMessage());
dialog.show();
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_clock, container, false);
txtEmpSearch = view.findViewById(R.id.txtEmpLookup);
txtJobSearch = view.findViewById(R.id.txtJobSearch);
btnSignIn = view.findViewById(R.id.btnSignIn);
return inflater.inflate(R.layout.fragment_clock, container, false);
}


/**
* Gets the Current Pay Period and Sets up the AutoCompleteTextView
*/
private class WeekTask extends AsyncTask<PayPeriod, Void, PayPeriod>{

@Override
protected PayPeriod doInBackground(PayPeriod... payPeriods) {
return new APIHelper().getCurrentPayWeek(JSONHelper.getDateAsInt());
}

@Override
protected void onPostExecute(PayPeriod payPeriod) {
period = payPeriod;
txtPayWeek = getView().findViewById(R.id.txtPayWeekNo);
txtPayWeek.setText(getString(R.string.txtCurrentWeek, payPeriod.getPayWeekNo()));

txtEmpSearch = (AppCompatAutoCompleteTextView) getView().findViewById(R.id.txtEmpLookup);

autoAdapter = new AutoSuggestAdapter(context, R.layout.support_simple_spinner_dropdown_item);

txtEmpSearch.setThreshold(2);

txtEmpSearch.setAdapter(autoAdapter);

// Fires when an Employee is selected
txtEmpSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
InputMethodManager in = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
Employee emp = (Employee) parent.getItemAtPosition(position);
((MainActivity)getActivity()).setEmployee(emp);
}
});

txtEmpSearch.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) {
handler.removeMessages(TRIGGER_AUTO_COMPLETE);
handler.sendEmptyMessageDelayed(TRIGGER_AUTO_COMPLETE, AUTO_COMPLETE_DELAY);
}

@Override
public void afterTextChanged(Editable s) {
if(txtEmpSearch.getText().toString().equals("")){

}
}
});

handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == TRIGGER_AUTO_COMPLETE) {
if (!TextUtils.isEmpty(txtEmpSearch.getText())) {
makeApiCall(txtEmpSearch.getText().toString());
}
}

return false;
}
});


}
}

private void getJobs(String term){
APICall.getJobsByPartial(context, term, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
List<Function> jobs = new ArrayList<>();
try{
JSONArray array = new JSONArray(response);
jobs = new APIHelper().populateFunctionList(array);
jobAdapter.setData(jobs);
jobAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
}
}

最佳答案

上面的代码确实有效,问题在于我如何测试它。我必须使用第一个 AutoCompleteTextView,选择一个建议,然后使用第二个。如果我尝试先使用第二个,它不起作用。

关于java - fragment 中的多个 AutoCompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56853439/

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