我是新来的,也是android的新手……所以我开始为android开发我的第一个应用程序……从自定义适配器中加载数据……当尝试从我的数据库中检索数据时,rawquery我使用的适用于 android 2.2。高于 2.2 的游标不检索任何内容。如果我更改查询的原始查询...它仅适用于 4.0 并且对于光标以下的版本是空的。我注意到问题出在光标上。有谁知道为什么会这样?
来自主类的方法
protected void selecionardados() {
// EditText to string
String busca = campo_busca.getText().toString();
//Open database class
Dados_banco consulta = new Dados_banco(this.getActivity());
consulta.open();
//Create a list
List<Dados_banco.ItemLista> itens = consulta.consultaMusica(busca);
CustomAdapter adapter = new CustomAdapter(this.getActivity(),itens);
listagem.setAdapter(adapter);
数据库方法
public List<ItemLista> consultaMusica(String NovaPesquisa){
String r = NovaPesquisa;
List<ItemLista> itens = new ArrayList<Dados_banco.ItemLista>();
Cursor resul = bancodados.query(Tabela_musica, new String[] {"_id", "Musica","Cantor","letra_musica","cat_oracao","cat_missa"}, null, null, null, null, null, null);
for (resul.moveToFirst();!resul.isAfterLast();resul.moveToNext()) {
Idlista = Integer.parseInt(resul.getString(resul.getColumnIndex("_id")));
BdMusica = resul.getString(resul.getColumnIndex("Musica"));
BdCantor = resul.getString(resul.getColumnIndex("Cantor"));
BdLetra = resul.getString(resul.getColumnIndex("letra_musica"));
BdOracao = resul.getString(resul.getColumnIndex("cat_oracao"));
BdMissa = resul.getString(resul.getColumnIndex("cat_missa"));
itens.add(new ItemLista(){{
id = Idlista;
musica = BdMusica;
cantor = BdCantor;
letra = BdLetra;
oracao = BdOracao;
missa = BdMissa;
});
} resul.close();
return itens;
自定义适配器类
public class CustomAdapter extends BaseAdapter{
LayoutInflater inflater;
List<Dados_banco.ItemLista> itens;
public CustomAdapter(Activity a, List<Dados_banco.ItemLista> itens) {
super();
this.itens = itens;
this.inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return itens.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
Dados_banco.ItemLista item = itens.get(position);
View vi = convertView;
if(convertView==null){
vi = inflater.inflate(R.layout.linha_listview, null);
TextView id = (TextView)vi.findViewById(R.linhaLista.id);
TextView musica = (TextView)vi.findViewById(R.linhaLista.musica);
TextView artista = (TextView)vi.findViewById(R.linhaLista.cantor);
ImageView oracao = (ImageView)vi.findViewById(R.linhaLista.cat_oracao);
ImageView missa = (ImageView)vi.findViewById(R.linhaLista.cat_missa);
id.setText(String.valueOf(item.id));
musica.setText(item.musica);
artista.setText(item.cantor);
String imgOra = item.oracao;
String imgMissa = item.missa;
if (imgOra.equals(null)) {
oracao.setImageResource(R.drawable.blank);
}else if(imgOra.equals("louvor")) {
oracao.setImageResource(R.drawable.louvor);
}else if(imgOra.equals("adoracao")) {
oracao.setImageResource(R.drawable.adoracao);
}
if (imgMissa.equals(null)) {
missa.setImageResource(R.drawable.blank);
}else if(imgOra.equals("louvor")) {
missa.setImageResource(R.drawable.louvor);
}else if(imgOra.equals("adoracao")) {
missa.setImageResource(R.drawable.adoracao);
}
}return vi;
}
}