如何根据索引位置从模型列表中检索数据并存储在TextView中。

huangapple 未分类评论42阅读模式
英文:

How to retrive data's from model list based on index postion and store in textview

问题

我是 Firebase 的新手

我正试图从 Firebase 中检索一些特定的数据。我有一个名为 "universities" 的节点,该节点有许多唯一标识符作为节点,每个节点中都有更多的数据。我想从每个节点中检索名称。

如何根据索引位置从模型列表中检索数据并存储在TextView中。

我想使用 OnChildEventListener() 将所有名称存储到一个列表中...

List<University> universityList = new ArrayList<>();myRef=FirebaseDatabase.getInstance().getReference().child("universities").child(userid);
myRef.addChildEventListener(new ChildEventListener() { 
	@Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
		University university = dataSnapshot.getValue(University.class); 
		universityList.add(university); 
		Log.i(TAG,"add university name = " + university.name); 
	} 
}

通过上述代码,我可以将所有名称从 Firebase 存储到列表中。

现在,我想根据索引位置从列表中获取这些名称,并将其存储在 TextView 中。这是我的问题。

英文:

Im new to Firebase

I am trying to retrieve some specific data from a Firebase. I have a node universities and that node has many unique identifier as node which has so more data into it. I want to retrieve name from each node.

如何根据索引位置从模型列表中检索数据并存储在TextView中。

And i want to store that all name's into a list using OnChildEventListener()...

List&lt;University&gt; universityList = new ArrayList&lt;&gt;();myRef=FirebaseDatabase.getInstance().getReference().child(&quot;universities&quot;).child(userid);
myRef.addChildEventListener(new ChildEventListener() { 
	@Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
		University university = dataSnapshot.getValue(University.class); 
		universityList.add(university); 
		Log.i(TAG,&quot;add university name = &quot; + university.name); 
	} 
}

By this above code i can able to store that all names from firebase to list.

Now I want to get that name's from the list based on index position and store it in textview. This was my problem.

答案1

得分: 0

public class UniversityAdapter extends RecyclerView.Adapter<UniversityAdapter.MyViewHolder> {
 
    private List<University> universityList;
 
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
 
        public MyViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name);
        }
    }
 
    public UniversityAdapter(List<University> universityList) {
        this.universityList = universityList;
    }
 
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.university_list_item, parent, false);
 
        return new MyViewHolder(itemView);
    }
 
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        University university = universityList.get(position);
        holder.name.setText(university.getName());
    }
 
    @Override
    public int getItemCount() {
        return universityList.size();
    }
}
英文:

You can make simple Adapter and just pass the list you received in it.

And access element position in onBindViewHolder()

then you can set your TextView



public class UniversityAdapter extends RecyclerView.Adapter&lt;UniversityAdapter.MyViewHolder&gt; {
 
    private List&lt;University&gt; universityList;
 
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public name;
 
        public MyViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name);
        }
    }
 
 
    public MoviesAdapter(List&lt;University&gt; universityList) {
        this.universityList = universityList;
    }
 
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.university_list_item, parent, false);
 
        return new MyViewHolder(itemView);
    }
 
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        University university = universityList.get(position);
        holder.name.setText(university.getName());
        
    }
 
    @Override
    public int getItemCount() {
        return universityList.size();
    }
}

</details>



huangapple
  • 本文由 发表于 2020年4月4日 23:12:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/61030182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定