2017-10-16 1 views
1

새 항목을 목록에 추가 할 때 같은 위치에서 내 항목을 계속보고 싶습니다. 어떻게해야합니까?새 항목을 추가 할 때 내 목록보기 항목에 어떻게 보관할 수 있습니까?

나는 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 + 위치 여야합니다.

더 나은 성능을 위해 ListView 대신 RecyclerView를 사용하는 것이 좋습니다. notifyDataSetChanged() 대신 notifyItemInserted()를 사용하십시오. 하나의 항목을 삽입 할 때 모든 것을 다시로드하고 다시 그릴 필요가 없으므로 notifyDataSetChanged()를 사용하십시오.

+0

탁신! RecyclerView를 사용하고 위치 0에 항목을 추가했습니다. 내게 말한 것처럼 작동했습니다. <3 –

+0

도움이 될 수있어서 다행입니다! :) 대답을 받아 들일 수 있겠습니까? 감사! –

0

배열을 사용하는 경우 0 번째 색인에서 특정 항목 위치를 수정하고 해당 색인이있는 모든 항목을 추가하십시오.

+0

이 해결되지만 아래 항목이 추가됩니다. 필요하지만 예를 들어 보내지 않거나 예를 들어 10 개 항목 만 표시 할 가능성이 있습니다. 내가 계속 올라가거나 내려 가면 더 보아라. 나는 페이스 북이나 인스 타 그램과 같은 것을 원한다. –

+0

첫 번째 항목이 고정되면 도움이 될 것이다. –

관련 문제