내 앱에 맞춤 목록 뷰가 있습니다. HTTP 요청을 사용하여 JSON 형식의 데이터를 가져옵니다. 리스트 뷰에 대한 전체 데이터 (이미지 제외)를로드하는 asynctask가 있습니다.Android : lazyloading listview에서 검색을 구현할 수 없습니다.
이 asyntask의 onpostexecute()에서 listview에 대한 이미지를 가져 오기 위해 다른 asynctask를 호출합니다.
가 이제 문제는 이미지가 배경에로드하기로하는 textchangedlistener를 사용하여 목록보기에 검색 기능을 구현하기 위해 노력하고, 나는 종종 널 포인터 예외로 실행합니다.
아무도 나에게 이것을 없애는 방법을 제안 할 수 있습니까? 현재 내가하고있는 일은 이미지가로드 될 때까지 searchbox edittext를 표시하지 않을 것입니다. 그러나 이로 인해 사용자는 특히 긴 목록의 경우 많이 기다리게됩니다.
전체 코드가 너무 길기 때문에 게시 할 수 없습니다. 그래서 여기에 간단한 개요를 붙이고 있습니다. 네가 다른 것을 필요로하면 알려줘. 나는 그렇게 할 것이다. 이 방법은 UI 스레드에서 작동하기 때문에
public class abcd extends Activity {
ListView todlistview;
List<HashMap<String, Object>> cnt = null;
ArrayList<HashMap<String, Object>> searchResults;/
.
.
/** AsyncTask to parse json data and load ListView */
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
@Override
protected SimpleAdapter doInBackground(String... strJson) {
.
.
.
adapter = new SimpleAdapter(getBaseContext(), cnt, com.example.promoapp.R.layout.textviewfield, from, to);
return adapter;
}
@Override
protected void onPostExecute(SimpleAdapter adapter) {
searchResults=new ArrayList<HashMap<String, Object>> (cnt);
// Setting adapter for the listview
todlistview.setAdapter(adapter);
for(int i=0;i<adapter.getCount();i++){
HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
.
.
ImageLoaderTask imageLoaderTask = new ImageLoaderTask();
// Starting ImageLoaderTask to download and populate image in the listview
imageLoaderTask.execute(hm);
}
}
}
/** AsyncTask to download and load an image in ListView */
private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
Dialog dil;
@Override
protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
InputStream iStream=null;
.
// Create a hashmap object to store image path and its position in the listview
// Storing the path to the temporary image file
// Storing the position of the image in the listview
// Returning the HashMap object containing the image path and position
}
@Override
protected void onPostExecute(HashMap<String, Object> result) {
}
}
private TextWatcher filterTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String searchString=Search.getText().toString();
int textLength=searchString.length();
searchResults.clear();
for(int i=0;i<cnt.size();i++)
{
String placeName=cnt.get(i).get("country").toString();
if(textLength<=placeName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(placeName.substring(0,textLength))){
//Toast.makeText(getApplicationContext(),placeName,1).show();
searchResults.add(cnt.get(i));
}
}
}
String[] from = { "country","flag","details"};
int[] to = { com.example.promoapp.R.id.tv_country,com.example.promoapp.R.id.iv_flag,com.example.promoapp.R.id.tv_country_details};
adapter = new SimpleAdapter(getBaseContext(), searchResults, com.example.promoapp.R.layout.textviewfield, from, to);
todlistview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
};
}
귀하의 logcat 오류를 추가하십시오! – Janmejoy
비어있는 객체에 cnt List 및 HashMaps를 구체적으로 초기화하지 않는 한 값 객체'Object'는 null입니다. –
불행히도이 질문을 며칠 전 게시 할 계획이었습니다. 나는 코드에서 다른 많은 변화를 만들었고, 다른 에러를 얻었고 지금은 로그캣을 가지고 있지 않습니다. 그러나 내가 가진 오류는 null 포인터 예외였습니다. 색인 x는 y 크기로 요청했습니다. 오류를 복제하고 logcat을 게시하려고 시도합니다. – ambit