【发布时间】:2018-04-11 03:01:54
【问题描述】:
我正在尝试使用从数据库中获取的不同变量获取 ArrayList,以在自定义列表视图中显示
这是主要的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View the balance sheet"
android:textSize="29sp"
android:id="@+id/view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView">
</ListView>
</RelativeLayout>
这是自定义布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/description_names"
android:text="description"
android:layout_marginTop="25sp"
android:layout_marginLeft="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/values_names"
android:text="value"
android:layout_marginTop="25sp"
android:layout_marginLeft="105sp"/>
</RelativeLayout>
这是适配器,我想要主活动中的两个 ArrayList,但它不工作
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(@NonNull Context context, ArrayList<String> mDetails,ArrayList<String> mValues) {
super(context,R.layout.custom_layout ,mDetails);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(getContext());
View customView=inflater.inflate(R.layout.custom_layout,parent,false);
String details=getItem(position);
String values=getItem(position);
TextView description_names=(TextView)customView.findViewById(R.id.description_names);
TextView values_names=(TextView)customView.findViewById(R.id.values_names);
description_names.setText(details);
return customView;
}
}
主要活动
ListView listvIew=(ListView)findViewById(R.id.listView);
final ArrayAdapter<String>array=new CustomAdapter(this,mDetails,mValues);
listvIew.setAdapter(array);
private ArrayList<String>mDetails=new ArrayList<>();
private ArrayList<String>mValues=new ArrayList<>();
V3Ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value=dataSnapshot.child("records").child("values").getValue(String.class);
Log.d(TAG, "onChildAdded: the value is"+value);
mDetails.add(value);
mValues.add(value);
array.notifyDataSetChanged();
}
我从数据库中获取值然后将它们放入 ArrayLists 然后我尝试在自定义列表视图中显示它们但是当我尝试使用第二个 Arraylist 时它不起作用
【问题讨论】:
标签: java android listview arraylist