尝试使用ArrayList创建自定义ArrayAdapter

时间:2017-10-30 07:33:34

标签: java android listview arraylist

我试图使用不同的变量获取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>

这是适配器,我想要主要活动中的ArrayLists但是它不能正常工作

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时它无法正常工作

1 个答案:

答案 0 :(得分:1)

来自主要活动的

适配器调用应该是,

CusatomeAdapter adapter;        
adapter = new CusatomeAdapter(mActivity,mList1 ,mList2);        
mListview.setAdapter(adapter);    

这是你的适配器calss

public class CusatomeAdapter extends BaseAdapter {
    public ArrayList<String> mList1;
    public ArrayList<String> mList2;
    Activity activity;

    public CusatomeAdapter(Activity activity,ArrayList<String> mList1 , ArrayList<String> mList2){
        super();
        this.activity = activity;
        this.mList1 = mList1;
        this.mList2 = mList2;
    }

    @Override
    public int getCount() {
        return mList1.size();   //give the size of list here
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_detail, null);
            holder = new ViewHolder();
            holder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);

            //same way define another text view and set their value ..

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_detail.setText(mList1.get(position).toString());
        return convertView;
    }

    private class ViewHolder {
        TextView tv_detail;
    }
}