First of all thank you for trying to help, come on!
I own a project made in Android Studio where it contains a custom ListView that I used along with my INTERNAL database. After the project has evolved, I've moved my database to EXTERNAL, and I'm using MYSQL as a database.
The problem is as follows:
After switching to EXTERNAL database my custom%% of% does not display the first result of the list.
That is, I have an adapter called "adpGames" that is populating the list of ListView
with variables taken from an object, but when it only contains one object to popular ListView
, ListView
comes empty as if you do not have any items, however, after adding another object in ListView
, Adapter
displays the 2 objects at once.
Follow my code to create the adapter and set it to ListView
:
public class ActApp extends AppCompatActivity {
private ListView lstComentarios;
private ArrayAdapter < Comentario > adpJogos;
@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_cad2_app_comentarios);
lstComentarios = (ListView) findViewById(R.id.lstComentarios);
adpJogos = repositorioJogos.buscaJogos(this, id);
lstComentarios.setAdapter(adpJogos);
}
}
Here is my "RepositoryGames" code that is responsible for popularizing the ListView with items taken from an Object:
public class RepositorioJogos {
private SQLiteDatabase conn;
public RepositorioJogos(SQLiteDatabase conn) {
this.conn = conn;
}
public ListView buscaJogos(Context context, int id) {
ListView adpJogos = new ListView(context, R.layout.listview);
UsuarioDAO dao = new UsuarioDAO();
ArrayList < Comentario > usr = dao.preencherListView(id);
if (usr != null) {
for (int i = 0; i < usr.size(); i++) {
Comentario comentario = new Comentario();
comentario.setComentarioJogo(usr.get(i).getComentarioJogo());
comentario.setUsuario(usr.get(i).getUsuario());
adpJogos.add(comentario);
}
return adpJogos;
}
return adpJogos;
}
}
The code for custom ListView items follows:
public class ListView extends ArrayAdapter < Comentario > {
private int resource = 0;
private LayoutInflater inflater;
public ListView(Context context, int resource) {
super(context, resource);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
view = inflater.inflate(resource, parent, false);
viewHolder.txtusuario = (TextView) view.findViewById(R.id.txtusuario);
viewHolder.txtcomentario = (TextView) view.findViewById(R.id.txtcomentario);
view.setTag(viewHolder);
convertView = view;
} else {
viewHolder = (ViewHolder) convertView.getTag();
view = convertView;
}
Comentario comentario = getItem(position);
viewHolder.txtcomentario.setText(comentario.getComentarioJogo());
viewHolder.txtusuario.setText(comentario.getUsuario());
return view;
}
static class ViewHolder {
TextView txtusuario;
TextView txtcomentario;
}
}
If you need something else, such as the code that is in ListView
, just ask, it's just a simple dao.preencherListView
. Any questions also just ask.
Thank you
Edited: Here is the code for dao.preencherListView:
public ArrayList fillListView (int id) { ArrayList list = new ArrayList ();
SoapObject buscarUsuarios = new SoapObject(NAMESPACE, PREENCHERLISTVIEW); buscarUsuarios.addProperty("id", id); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(buscarUsuarios); envelope.implicitTypes = true; HttpTransportSE http = new HttpTransportSE(URL); try{ http.call("urn" + PREENCHERLISTVIEW, envelope); Vector<SoapObject> resposta = (Vector<SoapObject>) envelope.getResponse(); for (SoapObject soapobject : resposta) { Comentario usr = new Comentario(); usr.setId( Integer.parseInt(soapobject.getProperty("id").toString())); usr.setComentarioJogo(soapobject.getProperty("comentarioJogo").toString()); usr.setUsuario(soapobject.getProperty("usuario").toString()); lista.add(usr); } } catch (Exception e) { e.printStackTrace(); return null;
EDITED: I forgot the code to access the database, which dao accesses:
public ArrayList fillListView (int id) { ArrayList list = new ArrayList ();
try{ Connection conn = ConectaMySql.obtemConexao(); String queryInserir = "SELECT * FROM comentario WHERE JOGOID = ?"; PreparedStatement ppStm = conn.prepareStatement(queryInserir); ppStm.setInt(1, id); ResultSet rSet = ppStm.executeQuery(); while(rSet.next()){ Comentario usr = new Comentario(); usr.setId(rSet.getInt(1)); usr.setComentarioJogo(rSet.getString(3)); usr.setUsuario(rSet.getString(5)); lista.add(usr); } conn.close(); } catch (Exception e){ e.printStackTrace(); } return lista; }