您好我是android的新手 我在ListView中添加了一个搜索按钮,但是我得到了一个错误,你可以帮我解决这个错误。
修改
错误cannot resolve symbol adapter
adapter是getuser方法中使用的List适配器
这是我的java代码
UserFragment.java
public class UsersFragment extends Fragment implements ListView.OnItemClickListener {
private String JSON_STRING;
private ImageButton btn1;
public static final String URL = "http://10.0.3.2/GetPHP/getuser.php";
public static final String TAG_JSON_ARRAY = "result";
// List view
private ListView listView;
// Search EditText
EditText inputSearch;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_users, container, false);
listView = (ListView) rootview.findViewById(R.id.listView);
inputSearch = (EditText) rootview.findViewById(R.id.inputSearch);
listView.setOnItemClickListener(this);
btn1=(ImageButton) rootview.findViewById(R.id.btn1);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
(SimpleAdapter) UsersFragment .this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
getJSON();
return rootview;
}
private void getuser(){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
HashMap<String,String> users= new HashMap<>();
users.put("name",name);
users.put("ref",ref);
list.add(users);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row2,
new String[]{"name","ref"},
new int[]{R.id.nom, R.id.email2});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
getuser();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), Viewuser.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("ref").toString();
intent.putExtra("ref", tid);
startActivity(intent);
}
提前致谢
答案 0 :(得分:1)
答案 1 :(得分:0)
使adapter
成为全局变量将解决您的问题:
更改您的代码:
public class UsersFragment extends Fragment implements ListView.OnItemClickListener {
private String JSON_STRING;
private ImageButton btn1;
//Make the adapter GLOBAL
ListAdapter adapter;
public static final String URL = "http://10.0.3.2/GetPHP/getuser.php";
public static final String TAG_JSON_ARRAY = "result";
// List view
private ListView listView;
// Search EditText
EditText inputSearch;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_users, container, false);
listView = (ListView) rootview.findViewById(R.id.listView);
inputSearch = (EditText) rootview.findViewById(R.id.inputSearch);
listView.setOnItemClickListener(this);
btn1=(ImageButton) rootview.findViewById(R.id.btn1);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
getJSON();
return rootview;
}
private void getuser(){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try
{
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
HashMap<String,String> users= new HashMap<>();
users.put("name",name);
users.put("ref",ref);
list.add(users);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new SimpleAdapter(
getActivity(), list, R.layout.list_row2,
new String[]{"name","ref"},
new int[]{R.id.nom, R.id.email2});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(getActivity(), "Téléchargement", "Veuillez patientez...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
getuser();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
说明:在您的代码中,您使用的
adpater
是本地 这意味着您不能使用相同的方法在onTextChanged()中设置过滤器。 这就是您无法解析适配器的原因。