java - 无法在自定义 View 中显示两个项目

标签 java android firebase listview firebase-realtime-database

我想在自定义 ListView 中显示项目,但我似乎无法让它工作。 Activity 就崩溃了。我的目的是在 ListView 中显示这两个项目。我能够从 firebase 检索必要的数据并将其存储到单独的数组列表中,但无法在 ListView 中显示它们。附上下面所有必要的代码。

这是我的自定义适配器类


public class CustomAdapter extends ArrayAdapter<String>{

    private final Activity context;
    private final ArrayList main;
    private final ArrayList sub;
    public CustomAdapter(Activity context,
                         ArrayList main, ArrayList sub) {
        super(context, R.layout.colortext, main);
        this.context = context;
        this.main = main;
        this.sub = sub;

    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.colortext, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.textmain);
        TextView txtSub=rowView.findViewById(R.id.address);
        txtTitle.setText((Integer) main.get(position));
        txtSub.setText((Integer) sub.get(position));
        return rowView;
    }
}

自定义xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView1">
</ListView>
    <TextView
        android:id="@+id/textmain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:paddingLeft="6dip"
        android:paddingRight="10dip"
        android:textSize="23dp"
        android:textColor="#01B9F5"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <CheckBox
        android:id="@+id/checkBox"
        android:clickable="true"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:paddingLeft="6dip"
        android:paddingRight="10dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:focusable="false"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:id="@+id/address"
        android:layout_below="@+id/checkBox"
        android:textColor="@color/white"/>
</RelativeLayout>

主要 Activity

mDatabase.child("users").child(mUserId).child("locs").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds:dataSnapshot.getChildren())
                    {
                        main.add(ds.child("title").getValue(String.class));
                        String g = main.toString();
                        sub.add(ds.child("addr").getValue(String.class));
                        String s = sub.toString();
                        ListView list = findViewById(R.id.listView1);


                        CustomAdapter adapter2 = new CustomAdapter(Location.this, main, sub);
                        list.setAdapter(adapter2);
                    }

                }

最佳答案

试试这个。在您的适配器中添加这些方法以在数组列表中添加项目。

public class CustomAdapter extends ArrayAdapter<String>{
     private final ArrayList mainList;
     private final ArrayList subList;

    public void addMain(Main main){
      mainList.add(main);
      notifyDataSetChanged();
    }

    public void addSub(Sub sub){
      subList.add(sub);
      notifyDataSetChanged();
    }

    // manage your data in onBind
    @Override
     public void onBindViewHolder(final ViewHolder holder, int 
     position) {
        if(position < mainList.size() ){
            // display mainlist related data here
            // after adding items in mainlist, this will executed.

        }
        if(position > mainList.size()-1) {
            // display sublist related data here
        }
     }
}

需要先添加mainList,才能满足onbind中的条件。 您可以将数据添加到适配器,如下所示。

// For example this is that path of your main lists
  DatabaseReference ref = database.getReference().child("main"); 
  ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            // I assume you have a custom object. In this case let's just say `Main`
            Main main = dataSnapshot.getValue(Main.class);
            adapter.addMain(notification);
        }
        // ... other override methods

关于java - 无法在自定义 View 中显示两个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55761851/

相关文章:

java - 本地PC无法读取同一网络中其他PC的套接字

java - 使用反射调用 Enum 子类类型的 valueOf() 时出现 NoSuchMethodError?

android - 使用较低版本和v7更新分级时出错

java - 如何使用嵌套的 for/each 循环来创建具有不同功能的对象?

Android环境下Java日期解析特定字符串格式

android - GCM 重复消息

java - 即使没有网络或 gps,Android 应用程序的纬度和经度值也会出现

android - Firebase:处理请求时出现未知错误。再试一次。

Firebase 动态链接在 Chrome iOS 中的行为异常

javascript - Firestore - 听特定的字段变化?