Java ListView重复第一行(Android)
我使用的是android studio,在ListView上有一些问题 listview显示了正确的行数,但元素在每行中都会重复,而不是显示我的ArrayList的每个元素 主活动的XML:Java ListView重复第一行(Android),java,android,listview,Java,Android,Listview,我使用的是android studio,在ListView上有一些问题 listview显示了正确的行数,但元素在每行中都会重复,而不是显示我的ArrayList的每个元素 主活动的XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="mat
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linBotoes"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/linBotoes" >
<Button
android:id="@+id/btnProcurar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:text="Busca" />
<Button
android:id="@+id/btnNovo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:text="Nova" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Alterar"
android:id="@+id/btnAlterar" />
<Button
android:id="@+id/btnExcluir"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:text="Apagar" />
</LinearLayout>
<ListView android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/linBotoes"/>
</RelativeLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postagemDAO = new PostagemDAO(this);
postagemDAO.abrir();
ListView lsView;
lsView = (ListView) findViewById(R.id.list);
PostagemAdapter adaptador = new PostagemAdapter(MainActivity.this, R.layout.activity_ls_postagem,/*minhaPostagem*/postagemDAO.listarPostagem());
lsView.setAdapter(adaptador);
}
适配器代码:
package com.example.eduardo.voiceblog_beta;
import android.content.Context;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import BD.Postagem;
/**
* Created by eduardo on 19/05/15.
*/
public class PostagemAdapter extends ArrayAdapter<Postagem>{
ArrayList<Postagem> arrayPostagem = new ArrayList<Postagem>();
public PostagemAdapter(Context context, int it ,ArrayList<Postagem> postagemAtual) {
super(context, it,postagemAtual);
this.arrayPostagem.addAll(postagemAtual);
}
@Override
public int getCount() {
return arrayPostagem.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
Postagem postagem = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_ls_postagem, parent,false);
}
//ImageView imageView = (ImageView) itemView.findViewById(R.id.imgPostagem);
//imageView.setImageURI(Uri.parse(postagemAtual.getCaminhoFoto()));
TextView txtTitulo = (TextView) convertView.findViewById(R.id.txtTitulo);
txtTitulo.setText(postagem.getTituloPostagem());
TextView txtData = (TextView) convertView.findViewById(R.id.txtData);
txtData.setText(postagem.getDataPostagem());
TextView txtComentario = (TextView) convertView.findViewById(R.id.txtPostagem);
txtComentario.setText(postagem.getComentarioPostagem());
return convertView;
}
}
package com.example.eduardo.voiceblog_beta;
导入android.content.Context;
导入android.view.view;
导入android.view.LayoutInflater;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
进口宝石;
/**
*由爱德华多于2015年5月19日创作。
*/
公共类PostageAdapter扩展了ArrayAdapter{
ArrayList arrayPostagem=新的ArrayList();
公共PostageAdapter(上下文上下文、int-it、ArrayList-postagemAtual){
超级(上下文、it、postagemAtual);
this.arrayPostagem.addAll(postagemAtual);
}
@凌驾
public int getCount(){
返回arrayPostagem.size();
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
Postagem=getItem(位置);
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.activity\u ls\u postagem,parent,false);
}
//ImageView ImageView=(ImageView)itemView.findViewById(R.id.imgPostagem);
//setImageURI(Uri.parse(postagemAtual.getCaminhoFoto());
TextView txtitulo=(TextView)convertView.findViewById(R.id.txtitulo);
setText(postagem.getitulopostagem());
TextView txtData=(TextView)convertView.findViewById(R.id.txtData);
setText(postagem.getDataPostagem());
TextView txtComentario=(TextView)convertView.findViewById(R.id.txtPostagem);
setText(postagem.getComentarioPostagem());
返回视图;
}
}
使用listarPostagem方法的DAO:
public ArrayList<Postagem> listarPostagem () {
ArrayList<Postagem> postagem = new ArrayList<Postagem>();
String[] colunas = new String[] { "DATA_POSTAGEM", "TITULO_POSTAGEM", "COMENTARIO_POSTAGEM", "FOTO_POSTAGEM"};
Cursor cursor = bd.query("TB_POSTAGEM",colunas , null, null, null, null, null);
Postagem post = new Postagem();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
post.setTituloPostagem(cursor.getString(cursor.getColumnIndex("TITULO_POSTAGEM")));
post.setDataPostagem(cursor.getString(cursor.getColumnIndex("DATA_POSTAGEM")));
post.setComentarioPostagem(cursor.getString(cursor.getColumnIndex("COMENTARIO_POSTAGEM")));
post.setCaminhoFoto(cursor.getString(cursor.getColumnIndex("FOTO_POSTAGEM")));
postagem.add(post);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return postagem;
}
public ArrayList listarPostagem(){
ArrayList postagem=新的ArrayList();
String[]colunas=新字符串[]{“DATA_postage”、“TITULO_postage”、“COMENTARIO_postage”、“FOTO_postage”};
Cursor Cursor=bd.query(“TB_POSTAGEM”,colunas,null,null,null,null);
Postagem post=新的Postagem();
cursor.moveToFirst();
而(!cursor.isAfterLast()){
post.setTituloPostagem(cursor.getString(cursor.getColumnIndex(“TITULO_POSTAGEM”));
post.setDataPostagem(cursor.getString(cursor.getColumnIndex(“DATA_POSTAGEM”));
post.setComentarioPostagem(cursor.getString(cursor.getColumnIndex(“COMENTARIO_POSTAGEM”));
post.setCaminhoFoto(cursor.getString(cursor.getColumnIndex(“FOTO_POSTAGEM”));
邮递地址:add(post);;
cursor.moveToNext();
}
//确保关闭光标
cursor.close();
退还邮资;
}
已经感谢您的帮助。问题似乎在于您只使用了一个
Postagem
引用,因此每次循环光标时,您都会修改列表中的每个条目
//Postagem post = new Postagem();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Postagem post = new Postagem(); //add this here
post.setTituloPostagem(cursor.getString(cursor.getColumnIndex("TITULO_POSTAGEM")));
post.setDataPostagem(cursor.getString(cursor.getColumnIndex("DATA_POSTAGEM")));
post.setComentarioPostagem(cursor.getString(cursor.getColumnIndex("COMENTARIO_POSTAGEM")));
post.setCaminhoFoto(cursor.getString(cursor.getColumnIndex("FOTO_POSTAGEM")));
postagem.add(post);
cursor.moveToNext();
}
尝试将呼叫移动到Postagem post=new Postagem()代码>在循环中,以便为列表中的每个项目创建一个新的Postagem
引用
//Postagem post = new Postagem();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Postagem post = new Postagem(); //add this here
post.setTituloPostagem(cursor.getString(cursor.getColumnIndex("TITULO_POSTAGEM")));
post.setDataPostagem(cursor.getString(cursor.getColumnIndex("DATA_POSTAGEM")));
post.setComentarioPostagem(cursor.getString(cursor.getColumnIndex("COMENTARIO_POSTAGEM")));
post.setCaminhoFoto(cursor.getString(cursor.getColumnIndex("FOTO_POSTAGEM")));
postagem.add(post);
cursor.moveToNext();
}
@eduardoUchida当然,没问题!