尝试在列表视图中显示已过滤的列表时出现问题。 我在过滤后打印出数据并且它实际上是正确的,但它不会显示在我的ListView上,请问我做错了什么?我的代码在
之下import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.media.Image;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.BaseAdapter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
public class SchoolListAdapter extends BaseAdapter implements Filterable {
private Activity activity;
ArrayList<HashMap<String, String>> data;
ArrayList<HashMap<String, String>> displayedData;
private static LayoutInflater inflater=null;
public SchoolListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
displayedData = d;
inflater = LayoutInflater.from(a);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null) {
vi = inflater.inflate(R.layout.school_details_list, null);
}
ImageView schoolLogo = (ImageView) vi.findViewById(R.id.schoolLogo);
TextView lblSchoolId = (TextView)vi.findViewById(R.id.lblSchoolId);
TextView lblSchoolName = (TextView)vi.findViewById(R.id.lblSchoolName);
HashMap<String, String> school = data.get(position);
// Setting all values in listview
lblSchoolId.setText(school.get("id"));
lblSchoolName.setText(school.get("name"));
String logo = school.get("logo");
byte []img = Base64.decode(logo,Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(img, 0, img.length);
schoolLogo.setImageBitmap(bmp);
return vi;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
if(results.count == 0){
notifyDataSetInvalidated();
}else{
displayedData = (ArrayList<HashMap<String, String>>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<HashMap<String, String>> FilteredArrList = new ArrayList();
if (data == null) {
data = new ArrayList(displayedData); // saves the original data in data
}
/********
*
* If constraint(CharSequence that is received) is null returns the data(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = data.size();
results.values = data;
} else {
constraint = constraint.toString().toUpperCase();
HashMap <String, String>schools = new HashMap();
for (int i = 0; i < data.size(); i++) {
String dat = data.get(i).get("name");
if(dat.toString().matches(".*"+constraint+".*")){
schools.put("id",data.get(i).get("id"));
schools.put("name",data.get(i).get("name"));
schools.put("logo",data.get(i).get("logo"));
FilteredArrList.add(schools);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class SelectSchoolActivity extends ListActivity implements AdapterView.OnItemClickListener {
private ProgressDialog pDialog;
private ListView lv;
private SharedPreferences sharedPreferences;
public static String SCHOOLING_PREFERENCES = "SCHOOL_PREF";
private ArrayList<HashMap<String, String>> schoolList;
private EditText txtSearch;
private SchoolListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_school);
sharedPreferences = getSharedPreferences(SCHOOLING_PREFERENCES, Context.MODE_PRIVATE);
txtSearch = (EditText)findViewById(R.id.txtSearch);
schoolList = new ArrayList();
if(sharedPreferences.contains("school")){
Intent intent = new Intent(SelectSchoolActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}else{
new GetSchoolList(SelectSchoolActivity.this).execute();
lv = getListView();
lv.setOnItemClickListener(this);
// Add Text Change Listener to EditText
txtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Call back the Adapter with current character to Filter
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String school = ((TextView)view.findViewById(R.id.lblSchoolId)).getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("school", school);
editor.commit();
Intent intent = new Intent(SelectSchoolActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
private class GetSchoolList extends AsyncTask<Void, Void, Void> {
private Activity context;
public GetSchoolList(Activity context){
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
String url = "http://192.168.34.1:8085/SchoolWebService/rest/Student/school_list";
// Making a request to url and getting response
String details = sh.makeServiceCall(url, ServiceHandler.GET);
if (details != null) {
try {
JSONArray results = new JSONArray(details);
for(int i = 0; i<results.length(); i++) {
JSONObject c = results.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String logo = c.getString("logo");
// tmp hashmap for single feeDetails
HashMap <String, String>school = new HashMap();
school.put("id", id);
school.put("name", name);
school.put("logo", logo);
schoolList.add(school);
}
} catch (Exception e) {
Log.e("Exception", Log.getStackTraceString(e));
}
} else {
//snackbar.setText("Connection Failed!").show();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing()) {
pDialog.dismiss();
}
adapter = new SchoolListAdapter(context, schoolList);
setListAdapter(adapter);
}
}
}
答案 0 :(得分:0)
@medhdj在我的代码中指出并出错,因此我能够找到并解决问题。 请参阅他的评论