2017-10-16 42 views
1

我想继续在同一个地方观看我的物品,当我将新物品添加到列表中时,我该怎么办?当我添加一个新项目时,我该如何保存在我的listView项目中?

我正在使用firebase,但这不是问题所在。

在这里,我要的项目看

here, the item that i want to see

这里,发送我失望

here, the new item that sends me down

//这样添加我的项目在新项目MainClass

if (o instanceof Post) { 
     posts.add((Post) o); 
     listaFeed.notifyDataSetChanged(); 
    } 




public class ListaFeed extends BaseAdapter { 

private ArrayList<Post> posts; 
private Context context; 

public ListaFeed(ArrayList<Post> posts, Context context) { 
    this.posts = posts; 
    this.context = context; 
} 

@Override 
public int getCount() { 

    return posts.size(); 
} 

@Override 
public Object getItem(int position) { 


    return posts.get(position-getCount()-1); 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 

{ 
    if (convertView==null){ 
     convertView = LayoutInflater.from(this.context).inflate(R.layout.card_feed, null);} 

    Post post = (Post) getItem(position); 
    ImageView foto = convertView.findViewById(R.id.iv_foto); 
    TextView titulo = convertView.findViewById(R.id.tv_titulo); 
    TextView autor = convertView.findViewById(R.id.tv_autor); 
    TextView modo = convertView.findViewById(R.id.tv_modo); 

    Picasso.with(context).load(post.getFoto()).into(foto); 
    titulo.setText(post.getTitulo()); 
    autor.setText(post.getAutor()); 
    modo.setText(post.getModo()); 



    return convertView; 
} 

}

+0

在arraylist中的第0个位置添加新项目将解决这个 – Shruti

回答

0

如果你想保留旧的项目在第一个位置(这是我如何理解问题),那么你只需要添加你的新项目在正确的位置。索引应该是前一项的1 +位置。

此外,我会建议您使用RecyclerView而不是ListView更好的性能。使用notifyItemInserted()而不是notifyDataSetChanged(),因为在插入单个项目时不需要重新加载和重绘所有内容。

+0

Thaks !!!我使用了RecyclerView,并将其添加到位置0,就像您告诉我的那样,它的工作原理<3 –

+0

很高兴能帮到您! :)你能接受答案吗?谢谢! –

0

如果您正在使用数组,然后修复您的特定项目位置在第0个索引并添加具有相应索引的所有项目。

+0

,但该项目将被添加到下面,我需要它但不会让我失望,或者有可能我只显示10个项目,例如我可以如果我继续上升或下降,看更多?我想要像facebook或instagram –

+0

固定第一项然后它可能会帮助 –

相关问题