我想在自定义列表视图中显示项目,但似乎无法正常工作。活动只是崩溃。我的目的是在列表视图中显示两个项目。我能够从Firebase检索并将必要的数据存储到单独的arraylist中,但是无法在listview中显示它们。在下面附加所有必需的代码。
这是我的customadapter类
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>
主要活动
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);
}
}
答案 0 :(得分:0)
尝试一下。在您的适配器中添加这些方法,以在阵列列表中添加项目。
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