【问题标题】:How to create custom listview from two array strings in fragment?如何从片段中的两个数组字符串创建自定义列表视图?
【发布时间】:2021-08-19 01:02:21
【问题描述】:

我在 Youtube 和 Google 上搜索其他问题,但没有得到最好的结果。我需要在 XML 上将两个 ArrayList 连接到 TextView,我确实尝试使用两个 arrayadapter,但结果只影响最后一个阵列适配器。也许这里的任何人都可以帮助我处理这个案子。

这是我的 ShopFragment.java

    public class ShopFragment extends Fragment implements AdapterView.OnItemClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ListView listView = (ListView)view.findViewById(R.id.product_list_view);

        ArrayList<String> name_of_product = new ArrayList<>();
        name_of_product.add("Tomat");
        name_of_product.add("Cabe");
        name_of_product.add("Wortel");
        name_of_product.add("Bayam");
        name_of_product.add("Sawi");
        name_of_product.add("Kangkung");

        ArrayList<String> price_of_product = new ArrayList<>();
        price_of_product.add("4000");
        price_of_product.add("5000");
        price_of_product.add("8000");
        price_of_product.add("6000");
        price_of_product.add("6000");
        price_of_product.add("6000");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), R.layout.shop_row, R.id.product_name, name_of_product);


        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position==0) {
            Toast.makeText(getActivity(), "Tomat", Toast.LENGTH_SHORT).show();
        }
        if(position==1) {
            Toast.makeText(getActivity(), "Cabe", Toast.LENGTH_SHORT).show();
        }
        if(position==2) {
            Toast.makeText(getActivity(), "Wortel", Toast.LENGTH_SHORT).show();
        }
        if(position==3) {
            Toast.makeText(getActivity(), "Bayam", Toast.LENGTH_SHORT).show();
        }
        if(position==4) {
            Toast.makeText(getActivity(), "Sawi", Toast.LENGTH_SHORT).show();
        }
        if(position==5) {
            Toast.makeText(getActivity(), "Kangung", Toast.LENGTH_SHORT).show();
        }
    }
}

还有这个shop_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Bayam"
            android:textSize="25dp"
            android:fontFamily="sans-serif-medium"
            android:gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:textColor="@color/cyan_900"
            android:id="@+id/product_name"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Rp."
            android:textSize="25dp"
            android:fontFamily="monospace"
            android:gravity="center_vertical"
            android:textColor="@color/cyan_900"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.4">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="5000"
            android:textSize="25dp"
            android:fontFamily="monospace"
            android:gravity="center_vertical"
            android:textColor="@color/cyan_900"
            android:id="@+id/product_price"/>

    </LinearLayout>

</LinearLayout>

我只希望 name_of_productprice_of_product 可以在 xml 上连接 product_nameproduct_price

【问题讨论】:

    标签: android listview arraylist android-arrayadapter


    【解决方案1】:

    变化:

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), R.layout.shop_row, R.id.product_name, name_of_product);
    

            ArrayAdapter<String> adapter = new ArrayAdapter(getActivity(), R.layout.shop_row, name_of_product) {
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                convertView = super.getView(position, convertView, parent);
                TextView tvName = convertView.findViewById(R.id.product_name);
                TextView tvPrice = convertView.findViewById(R.id.product_price);
                tvName.setText(name_of_product.get(position));
                tvPrice.setText(price_of_product.get(position));
                return convertView;
            }
        };
    

    【讨论】:

    • 打开那个片段还是崩溃
    猜你喜欢
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多