簡體   English   中英

嘗試使用 ArrayList 創建自定義 ArrayAdapter

[英]Trying to create a custom ArrayAdapter with 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 時它不起作用

來自您的主要活動的適配器調用應該是這樣的,

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

這是您的適配器分類

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM