2014-03-28 54 views
0

我在自定義對話框中遇到了一個小問題。 當用戶在列表視圖中搜索項目時,列表顯示正確的項目,但如果用戶想要再次搜索,則列表視圖將顯示上次搜索的結果。在搜索後恢復對話框中的列表視圖

問題: 如何在第一次搜索後恢復列表視圖?

我與自定義對話框活動

edit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       showDialog(); 

      } 

     }); 

    } 

} 

private void showDialog() { 
    dialog = new Dialog(this); 
    View view = getLayoutInflater().inflate(R.layout.material_list, null); 
    searchList = (ListView) view.findViewById(R.id.searchList); 
    dialog.setTitle("Välj ny artikel"); 

    final MaterialAdapter adapter = new MaterialAdapter(
      InformationActivity.this, materialList); 

    dialog.setContentView(view); 

    searchList.setAdapter(adapter); 

    searchList.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 

      Materials itemMat = materialList.get(position); 

      resultProduct = itemMat.materialName; 
      resultProductNo = itemMat.materialNo; 

      result = resultProduct + " " + resultProductNo; 
      Toast.makeText(getApplicationContext(), result, 
        Toast.LENGTH_SHORT).show(); 

     } 

    }); 

    search = (EditText) dialog.findViewById(R.id.search); 

    search.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      resultText = search.getText().toString() 
        .toLowerCase(Locale.getDefault()); 
      adapter.filter(resultText); 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
    cancel = (Button) dialog.findViewById(R.id.btnCancel); 
    cancel.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 

     } 
    }); 

    ok = (Button) dialog.findViewById(R.id.btnOK); 
    ok.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      product.setText(resultProduct); 
      productNo.setText(resultProductNo); 
      dialog.dismiss(); 

     } 
    }); 

    dialog.show(); 

} 

} 

我的適配器

public class MaterialAdapter extends BaseAdapter { 
LayoutInflater inflater; 
Context context; 
List<Materials> searchItemList = null; 
ArrayList<Materials> materialList = new ArrayList<Materials>(); 

public MaterialAdapter(Context context, List<Materials> searchItemList) { 
    this.context = context; 
    inflater = LayoutInflater.from(context); 
    this.searchItemList = searchItemList; 
    this.materialList = new ArrayList<Materials>(); 
    this.materialList.addAll(searchItemList); 

} 

@Override 
public int getCount() { 
    return searchItemList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return searchItemList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder viewHolder; 
    if (convertView == null) { 

     convertView = inflater.inflate(R.layout.d, null); 
     viewHolder = new ViewHolder(); 
     viewHolder.tvMaterialName = (TextView) convertView 
       .findViewById(R.id.tvMaterialName); 
     viewHolder.tvMaterialNo = (TextView) convertView 
       .findViewById(R.id.tvMaterialNo); 

     convertView.setTag(viewHolder); 

    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 

    } 

    viewHolder.tvMaterialName.setText((searchItemList.get(position)) 
      .getMaterialName()); 
    viewHolder.tvMaterialNo.setText((searchItemList.get(position)) 
      .getMaterialNo()); 

    return convertView; 

} 

public class ViewHolder { 
    TextView tvMaterialName; 
    TextView tvMaterialNo; 

} 

public void filter(String charText) { 
    charText = charText.toLowerCase(Locale.getDefault()); 
    searchItemList.clear(); 
    if (charText.length() == 0) { 
     searchItemList.addAll(materialList); 
    } else { 
     for (Materials wp : materialList) { 
      if (wp.getMaterialName().toLowerCase(Locale.getDefault()) 
        .startsWith(charText)) { 
       searchItemList.add(wp); 
      } 
     } 
    } 
    notifyDataSetChanged(); 

} 

} 

感謝您的幫助:)

回答

0

嘗試改變search.getText()s.toString()

public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     resultText = search.getText().toString() 
       .toLowerCase(Locale.getDefault()); 
     adapter.filter(resultText); 

    } 

public void onTextChanged(CharSequence s, int start, int before, int count) { 
     resultText = s.toString().toLowerCase(Locale.getDefault()); 
     adapter.filter(resultText); 
    } 

一個次要的東西,使materialList = searchItemList;而searchItemList空將與檢索項目(第一次運行會materialList的相同元素。)

P.S你的適配器應落實在這種情況下(implements Filterable

+0

非常感謝你的幫助! 我想到了這一點,但需要一些指導:) – Camo

0
@Override 
     public void afterTextChanged(Editable s) { 
resultText = search.getText().toString() 
        .toLowerCase(Locale.getDefault()); 
      adapter.filter(resultText); 
     } 

試試這個,檢查是否有利於

+0

它沒有工作Filterable填充。仍然是同樣的問題:(但是,無論如何謝謝 – Camo