我正在尝试在我的应用中实现搜索功能,它可以部分工作,当它无法找到应用程序崩溃的项目时,会给我以下错误:
12-19 14:35:44.019 29694-29694/W/System.err﹕ at br.com.adapter.DespesaViewAdapter.getCount(DespesaViewAdapter.java:75)
12-19 14:35:44.020 29694-29694/W/System.err﹕ at br.com.representemais.DespesaActivity$1.onQueryTextChange(DespesaActivity.java:241)
以上几行是:
75:return despesa.size(); 241:lv.setAdapter(ad);
这是我的全部代码:
public class DespesaViewAdapter extends BaseAdapter {
private Repositorio mRepositorio;
private SearchView.OnQueryTextListener query;
private Runnable frag;
private DespesaActivity desp;
private static LayoutInflater inflater=null;
private List<DespesasDaViagemModel> despesa = new ArrayList<DespesasDaViagemModel>();
public DespesaViewAdapter(Context context, Runnable fragmentDespesa, List<DespesasDaViagemModel> despesas) {
this.inflater = LayoutInflater.from( context );
this.despesa = despesas;
mRepositorio = new Repositorio(context);
frag = fragmentDespesa;
}
public DespesaViewAdapter(Context contextx, SearchView.OnQueryTextListener onQueryTextListener, List<DespesasDaViagemModel> despesa) {
this.inflater = LayoutInflater.from( contextx );
this.despesa = despesa;
query = onQueryTextListener;
}
public int getCount() {
return despesa.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;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.despesa_activity, null);
holder=new ViewHolder();
holder.id = (TextView)vi.findViewById(R.id.despesaId);
holder.data = (TextView)vi.findViewById(R.id.dataDespesa);
holder.categoria = (TextView)vi.findViewById(R.id.despesaCategoria);
holder.valor = (TextView)vi.findViewById(R.id.valorDespesa);
View corCategoria = (View) vi.findViewById(R.id.corCategoria);
corCategoria.setBackgroundResource(R.drawable.corner_categoria);
GradientDrawable drawable = (GradientDrawable) corCategoria.getBackground();
String corFormatada = (despesa.get(position).getCor());
drawable.setColor(Color.parseColor(corFormatada));
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
}
DespesasDaViagemModel item = new DespesasDaViagemModel();
item = despesa.get(position);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = df.parse(item.getData());
} catch (ParseException e) {
e.printStackTrace();
}
df.applyPattern("dd/MM/yyyy");
String dataCriado = df.format(date);
holder.id.setText(String.valueOf(item.getId()));
holder.data.setText(dataCriado);
holder.categoria.setText(item.getCategoria());
holder.valor.setText("R$" + item.getValor());
return vi;
}
public void setData(List<DespesasDaViagemModel> despesas){
this.despesa.addAll(despesas);
this.notifyDataSetChanged();
}
public class ViewHolder
{
TextView id;
TextView data;
TextView categoria;
TextView valor;
}
}
public class DespesaActivity extends ActionBarActivity {
private String rm_IdViagem;
private List<DespesasDaViagemModel> despesa = new ArrayList<DespesasDaViagemModel>();
private boolean searchCheck;
private SearchView searchView;
private ProgressBar progressBar;
private ListView lv;
private LinearLayout footerLinearLayout;
private DespesaViewAdapter ad;
private DespesaViewAdapter ads;
private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.despesa);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
SecurePreferences mSessao = new SecurePreferences(DespesaActivity.this, "sessao");
mSessao.put("menuAtual", "Despesas");
lv = (ListView) findViewById(R.id.listaDespesa);
Bundle parametros = getIntent().getExtras();
if (parametros != null) {
rm_IdViagem = parametros.getString("id_viagem");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint(this.getString(R.string.search));
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);
if (rm_IdViagem != null) {
new DownloadTask().execute();
}
menu.findItem(Menus.PROCURAR).setVisible(true);
searchCheck = false;
return super.onCreateOptionsMenu(menu);
}
public class DownloadTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(DespesaActivity.this);
// setup your dialog here
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Carregando Despesas");
dialog.setMessage("Aguarde...");
dialog.setCancelable(false);
dialog.show();
}
protected Void doInBackground(Void... params) {
despesa = new ArrayList<DespesasDaViagemModel>();
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
Repositorio mRepositorio = new Repositorio(DespesaActivity.this);
List lista = mRepositorio.getDespesasViagensPorId("", Integer.valueOf(rm_IdViagem));
despesa = lista;
ad = new DespesaViewAdapter(DespesaActivity.this, this, despesa);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
}
});
lv.setAdapter(ad);
TextView emptyText = (TextView) findViewById(R.id.SemResultado);
lv.setEmptyView(emptyText);
}
});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
private SearchView.OnQueryTextListener OnQuerySearchView = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String search) {
// TODO Auto-generated method stub
if (searchCheck) {
// implement your search here
despesa = new ArrayList<DespesasDaViagemModel>();
try {
Repositorio mRepositorio = new Repositorio(DespesaActivity.this);
List listaDeDespesas = mRepositorio.getDespesasViagensPorId(search, Integer.valueOf(rm_IdViagem));
despesa = listaDeDespesas;
ad = new DespesaViewAdapter(DespesaActivity.this, this, despesa);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setAdapter(ad);
TextView emptyText = (TextView) findViewById(R.id.SemResultado);
lv.setEmptyView(emptyText);
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
};
private static final int REFRESH = 1;
private static final int SEARCH = 2;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menus.PROCURAR:
searchCheck = true;
break;
case android.R.id.home:
super.onBackPressed();
overridePendingTransition(R.anim.animation_back, R.anim.animation_back_leave);
case REFRESH:
return true;
case SEARCH:
return true;
case R.id.menu_filtro:
this.fnFiltrar();
return true;
case R.id.menu_atualizar:
/* dispara os repositorios a sincronizar */
new Sincronizar(DespesaActivity.this, DespesaActivity.this).execute(0);
return true;
}
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation_back, R.anim.animation_back_leave);
finish();
}
private void fnFiltrar() {
Intent intent = new Intent(this, FiltrarActivity.class);
startActivity(intent);
}
}
答案 0 :(得分:2)
我猜你在传递null代替数组列表。只是尝试在适配器的getCount()方法中打印列表。
和doInBackground()
List lista = mRepositorio.getDespesasViagensPorId("", Integer.valueOf(rm_IdViagem));
despesa.addAll(lista);
或
public int getCount() {
if(despesa == null)
{
return 0;
}
return despesa.size();
}
答案 1 :(得分:1)
您应该只能在方法中进行Null检查。像这样:
public int getCount() {
if(despesa != null || despesa.size() > 0)
return despesa.size();
// otherwise we've got no data
return 0;
}
如果您的列表为空或不包含任何项目,将覆盖您。