如何解决如何从片段中的两个数组字符串创建自定义列表视图?
我在 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_product 和 price_of_product 可以在 xml 上与 product_name 和 product_price 连接。
解决方法
变化:
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),R.layout.shop_row,R.id.product_name,name_of_product);
到
ArrayAdapter<String> adapter = new ArrayAdapter(getActivity(),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;
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。