我的目的是让autoCompleteText
以JSON格式从服务器获取值,并且我的文本框在类型字符后一秒发送请求然后显示我的JSON值,并且此autoCompleteText在Fragment中。
在此代码中:
List<ProductGetSet> new_suggestions = null;
try {
new_suggestions = jp.getParseJsonWCF(constraint.toString());
} catch (IOException e) {
e.printStackTrace();
}
new_suggestions没有得到String的值而且直接捕获然后我的List没有得到值但是约束有值
SearchAdapter.java
public class SearchAdapter extends ArrayAdapter<String> {
private Timer searchTimer;
private List<String> suggestions;
public SearchAdapter(Activity context, String nameFilter) {
super(context, android.R.layout.simple_dropdown_item_1line);
suggestions = new ArrayList<>();
}
@Override
public int getCount() {
return suggestions.size();
}
@Override
public String getItem(int position) {
return suggestions.get(position);
}
@Override
public Filter getFilter() {
final Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
FilterResults filterResults = new FilterResults();
final JsonParseProduct jp=new JsonParseProduct();
if (constraint != null) {
// A class that queries a web API, parses the data and
// returns an ArrayList<GoEuroGetSet>
searchTimer = new Timer();
searchTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
searchTimer.cancel();
searchTimer = null;
} catch (Exception e) {
Log.e("tmessages", e.toString());
}
}
}, 200, 300);
List<ProductGetSet> new_suggestions = null;
try {
new_suggestions = jp.getParseJsonWCF(constraint.toString());
} catch (IOException e) {
e.printStackTrace();
}
suggestions.clear();
for (int i=0;i<new_suggestions.size();i++) {
suggestions.add(new_suggestions.get(i).getName());
}
// Now assign the values and count to the FilterResults
// object
filterResults.values = suggestions;
filterResults.count = suggestions.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 1 ) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
}
JsonParseProduct.java
public class JsonParseProduct {
public List<ProductGetSet> getParseJsonWCF(String sName) throws IOException {
List<ProductGetSet> ListData = new ArrayList<ProductGetSet>();
try {
String temp = sName.replace("", "20%");
URL js = new URL("http://webheavens.com/suggestion.php?name="+temp);
URLConnection jc = js.openConnection();
InputStream jStream = jc.getInputStream();
InputStreamReader reader = new InputStreamReader(jStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = bufferedReader.readLine();
JSONObject jsonResponse = new JSONObject(line);
JSONArray jsonArray = jsonResponse.getJSONArray("results");
for (int i=0; i < jsonArray.length();i++){
JSONObject r = jsonArray.getJSONObject(i);
ListData.add(new ProductGetSet(r.getString("id"),r.getString("name")));
}
} catch (JSONException e) {
e.printStackTrace();
}
return ListData;
}
}
ProductFragment .java
public static ProductFragment newInstance(int page, String title) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_product, container, false);
acTextView = (AutoCompleteTextView) view.findViewById(R.id.autoComplete);
this.mView = view;
acTextView.setAdapter(new SearchAdapter(getActivity(), acTextView.getText().toString()));
acTextView.addTextChangedListener(
new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
new SearchAdapter(getActivity(), acTextView.getText().toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
private Timer timer = new Timer();
private final long DELAY = 1000; // milliseconds
@Override
public void afterTextChanged(final Editable s) {
timer.cancel();
timer = new Timer();
timer.schedule(
new TimerTask() {
@Override
public void run() {
// you will probably need to use runOnUiThread(Runnable action) for some specific actions
}
},
DELAY
);
}
}
);
acTextView.setAdapter(new SearchAdapter(getActivity(), acTextView.getText().toString()));
return view;
}
我真的很困惑,请帮助我。