具有Firefield数据库中更多字段和自定义外观数据的ListView

时间:2019-06-27 01:17:22

标签: java android firebase-realtime-database android-listview

我想使列表视图具有4条信息,但是我只能用2个字段来完成。在那里,我看到了一个视频,该人对该列表进行了自定义。我复制了它,我真的必须按照我想要的方式保留它。现在唯一的问题是我无法将数据放入其中。我不知道错误是什么,即使在文档中也找不到任何解释。

有人有什么想法吗?

我的数据库:

Database

这些是我要显示的数据,但低于返回给我的数据。

Return

 vagas = new Vagas();
        list = new ArrayList<Vagas>();
        adapter = new ArrayAdapter<Vagas>(getActivity(), layout.list_view, id.funcao, list);

        db = FirebaseDatabase.getInstance();
        ref = db.getReference("vagas");

        listView = getView().findViewById(id.listViewId);

        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()){
                    vagas = ds.getValue(Vagas.class);
                    list.add(vagas);
                }
                listView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

list_view.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:paddingStart="12dp"
    android:paddingEnd="12dp"enter code here
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/funcao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Developer"
            android:textStyle="bold"
            />
        <TextView
            android:id="@+id/descricao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:text="A software engineer is a person who applies the principles of software engineering to the design, development, maintenance, testing, and evaluation of this. So that only."/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="2dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Salário: "
            android:textSize="16sp"
            android:textStyle="bold"
            />
        <TextView
            android:id="@+id/salario"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1248,00 R$"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Localização: "
            android:textSize="16sp"
            android:textStyle="bold"
            />
        <TextView
            android:id="@+id/local"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Campinas-SP"
            />
    </LinearLayout>
</LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

在使用自定义视图类型时,您需要创建自己的自定义适配器扩展数组适配器并为列表项的自定义视图添加膨胀。

public class VegasAdapter extends ArrayAdapter<Vagas> {
    public VegasAdapter(Context context, ArrayList<Vagas> users) {
       super(context, 0, users);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // Get the data item for this position
       Vagas vagas = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_, parent, false);
       }
       // Lookup view for data population
       TextView tv1 = (TextView) convertView.findViewById(R.id.funcao);
       TextView tv1 = (TextView) convertView.findViewById(R.id.descricao);

// get data and binding with textview
// Below lines are just sample based on you class if you need full code post your Vagas.java code
       tvName.setText(vagas.getFincao());
       tvHome.setText(vagas.getDescripcao());


       // Return the completed view to render on screen
       return convertView;
   }
}


然后像这样设置自定义适配器,并从firebase中获取数据。

   List<Vagas>   list = new ArrayList<Vagas>();
   VegasAdapter     adapter = new VegasAdapter<Vagas>(getActivity(),list);