0
First 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 moved my database to EXTERNAL, and am using MYSQL as a database.
The problem is this:
After switching to EXTERNAL database mine ListView
custom does not display the first result of the list.
That is, I have an Adapter called "adpJogos" that is populating the list of ListView
with variables taken from an object, however, when it only contains one object to ListView
, the ListView
comes empty as if there were no items, however, after adding another object to the Adapter
, the ListView
displays the 2 objects at once.
Follow my code to create the Adapter and set it on 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);
}
}
Follow my code from the "Jogos repository" which is responsible for popular 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;
}
}
Follow the code of the custom Listview items:
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 anything else, like the code on dao.preencherListView
just ask, but it’s just a simple SELECT coluna FROM tabela
. Any questions too just ask.
Thank you
Edited: Follow the dao code.fillListView:
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 the:
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; }
What’s in the file in your
adapter
adpJogos
?– viana
Nothing, it is not a file. It is only an Arrayadapter that is used only in the command: "adpJogos = repositorioJogos.buscaJogos(this, id);" .. and is sent to the "Games repository" to be populated and then this adpJogos is added in Listview. However, another adpJogos with Listview form is created in the "Jogos repository", and the codes for it are in the third citation, in the Listview name class.
– Omnislade
And this dao.fillListView(id)? How is it mounted? Let me see if I can help you.
– viana
Ready friend. .. I edited the question and put the code "dao.fillListView" and the eclipse code to access the database. Thanks for trying to help.
– Omnislade
Well, you perform the
query
and returns 1 result, but does not display inlistview
? Right?!– viana
This query searches the database for the values corresponding to the "id" that was requested and stores it in the form of a "comment" object. However, there may be more than one result with the same "id", so it has this loop to take all the results that have the same "id" and put in an array. The problem is: when there is only one object stored in the array, the listview becomes empty, plus when you add another object with the same "id" (i.e., it has more than one object within the array), it shows normal listview. That is, either listview shows 0 items or shows 2+. It does not show when it has 1 item only.
– Omnislade
In short: My comment table has 3 items:"id","comment" and "user". However, there may be more than one "id" equal. Therefore, I create this array called "list" and place all the objects that are found inside it.So for example:If I have in table 1 row only with the same id (example: 5), it should show in Listview an item. If I have 2 lines with id 5 (like in the example) I should show 2 items and so on (so there’s the loop). However, Listview only shows 0 items or 2+ items, it does not show when it has only 1 item (if the table has only 1 row with id 5).
– Omnislade
Try calling refresh pro Adapter from listview after loading the data: adpJogos.notifyDataSetChanged();
– Giuliana Bezerra
I tried, but nothing has changed. It seems that Listview does not even display item 1, I have tried everything I can imagine and nothing. It must be something in the programming that I missed or whatever.
– Omnislade