gpt4 book ai didi

android - 在 Arraylist 的 ListView 的列表项上显示值

转载 作者:行者123 更新时间:2023-11-30 02:01:19 26 4
gpt4 key购买 nike

两个 fragment ,

  • 第一个 fragment 将产品名称和价格的数组列表发送给第二个 fragment ,
  • 第二个 fragment 正确获取所有值

问题

我想在 TextView 上显示数据。怎么可能?

下面是我的代码

第一个 fragment

alists=new ArrayList<String>();

System.out.println("ARRAYLIST><><><><><"+alists);

GenericUtility.setStringToSharedPrefsForKey("selected_prodname", user_name, getActivity());
GenericUtility.setStringToSharedPrefsForKey("selected_prodprc", dollars+prod_price, getActivity());

btn_add_to_cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

cartincrement();


productnams = GenericUtility.getStringFromSharedPrefsForKey("selected_prodname", getActivity());
System.out.println("Seleced user name=-=-=-=-=-=-==-=" + productnams);
productprc = GenericUtility.getStringFromSharedPrefsForKey("selected_prodprc", getActivity());
System.out.println("Seleced prodprice=-=-=-=-=-=-==-=" + productprc);

alists.add(productnams);
alists.add(productprc);

}
});


relcart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Add_to_cart tf = new Add_to_cart();
Bundle bundle = new Bundle();

bundle.putStringArrayList("prodnam", alists);
// bundle.putString("prodprce", productprc);
tf.setArguments(bundle);

android.support.v4.app.FragmentManager fm = getFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, tf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
});

}

第二个 fragment

public class Add_to_cart extends Fragment {

private Button continue_shopping;
private Button checkout;
ListView list;
private TextView _decrease,mBTIncrement,_value;
private CustomListAdapter adapter;
private ArrayList<String> alst;
private String bname;

public Add_to_cart(){}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

final View rootView = inflater.inflate(R.layout.list_view_addtocart, container, false);

alst=new ArrayList<String>();
Bundle bundle = this.getArguments();
alst = bundle.getStringArrayList("prodnam");

System.out.println("NAM--"+alst);

/* for(int i=0;i<alst.size();i++)
{
bname=alst.get(i);
}*/

continue_shopping=(Button)rootView.findViewById(R.id.btn_continueshoppping);
checkout=(Button)rootView.findViewById(R.id.btn_chckout);
adapter = new CustomListAdapter(getActivity(),alst);
list=(ListView)rootView.findViewById(R.id.list_addtocart);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();



list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub


}
});

continue_shopping.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

HomeFragment tf = new HomeFragment();
android.support.v4.app.FragmentManager fm = getFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, tf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

}
});

checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Checkout tf = new Checkout();
android.support.v4.app.FragmentManager fm = getFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, tf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

}
});

return rootView;
}

public class CustomListAdapter extends BaseAdapter {

private Context context;
private ArrayList<String> listData;
//private AQuery aQuery;

String dollars="\u0024";


public CustomListAdapter(Context context,ArrayList<String> listData) {
this.context = context;
this.listData=listData;
// aQuery = new AQuery(this.context);
}

@Override
public int getCount() {
return listData.size();
}

@Override
public Object getItem(int position) {
return listData.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
holder.txtproname = (TextView) convertView.findViewById(R.id.proname_addtocart);
holder.txtprofilecast = (TextView) convertView.findViewById(R.id.proprice_addtocart);

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

holder.txtproname.setText(listData.get(position));

holder.txtprofilecast.setText(dollars+listData.get(position));

//holder.txtprofilecast.setText(dollars+listData.get(position));



// aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

return convertView;
}
class ViewHolder{
ImageView propic;
TextView txtproname;
TextView txtprofilecast;

}

最佳答案

您的问题是您的项目列表实际上是应该的两倍长。如果您的列表中有 4 个项目,您的数组中将有 8 个条目(每个项目有 2 个条目,一个名称和一个价格)。

您不应将名称和价格添加到单个数组,而应创建 2 个并行数组。第一个包含名称列表,第二个包含价格列表。将两个数组传递给第二个 fragment 。

关于android - 在 Arraylist 的 ListView 的列表项上显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31424300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com