我是Android应用程序开发的初学者,在stackoverflow上可能有类似的问题,但我无法找到这个问题的确切解决方案。我在SO上尝试了很多链接。帮助感谢。如果需要上传任何其他文件,请告诉我。
这是listview输出 Listview output
我非常确定这些项目是否已插入列表视图中,因为当我将项目保存为0时,我得到以下输出。Toast.makeText to display item0 of listview l1 因此将数据插入listview没有问题。问题可能在于显示
// fragment_cart layout - where the listview is stored.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listview_cart"
/>
</LinearLayout>
Cartlist_layout - listview的结构
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
// addtocart_layout
<?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"
android:orientation="vertical">
<TextView
android:text="Total : Rs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView10"
android:textSize="20sp"
android:textStyle="normal|bold"
android:layout_marginTop="16dp"
android:layout_below="@+id/textView9"
android:layout_alignStart="@+id/textView9" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="4"
android:id="@+id/editText6"
android:text="1"
android:layout_alignBaseline="@+id/textView9"
android:layout_alignBottom="@+id/textView9"
android:layout_alignStart="@+id/textView11" />
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView11"
android:textStyle="normal|bold"
android:textSize="20sp"
android:layout_alignBaseline="@+id/textView10"
android:layout_alignBottom="@+id/textView10"
android:layout_alignParentEnd="true"
android:layout_marginEnd="142dp" />
<TextView
android:text="Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView9"
android:textSize="20sp"
android:textStyle="normal|bold"
android:layout_marginTop="36dp"
android:layout_below="@+id/textView7"
android:layout_toStartOf="@+id/textView11"
android:layout_marginEnd="45dp" />
<TextView
android:text="ItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView7"
android:textStyle="normal|bold"
android:textSize="20sp"
android:layout_marginTop="95dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView10"
android:layout_marginStart="12dp" />
</RelativeLayout>
CartDialog - java文件
public class CartDialog extends DialogFragment {
String item;
TextView iname,t11;
EditText qty;
LayoutInflater inflater;
View view;
NumberPicker numberPicker;
public static int sprice[]={40,50,60,25,30,30,25,25,20,20,20,15,15};
public String names[]=new String[]{"Item1","Item2","Item3"};
public ListView l1;
int i=0;
List itemName;
LinearLayout rl;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater=getActivity().getLayoutInflater();
itemName=new ArrayList();
view=inflater.inflate(R.layout.addtocart_layout,null);
rl = (LinearLayout)view.inflate(this.getContext(), R.layout.fragment_cart,null);
//#s1 This code is for some other functionality.
iname=(TextView)view.findViewById(R.id.textView7);
iname.setText(MyMenu.item1);
int id=Integer.parseInt(MyMenu.childid);
qty=(EditText)view.findViewById(R.id.editText6);
t11=(TextView)view.findViewById(R.id.textView11);
t11.setText(Integer.toString(Integer.parseInt(qty.getText().toString())*sprice[id]));
qty.addTextChangedListener(
new TextWatcher() {
int id=Integer.parseInt(MyMenu.childid);
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String qty_value=qty.getText().toString();
if(!qty_value.equals(""))
t11.setText(Integer.toString(Integer.parseInt(qty_value)*sprice[id]));
else
t11.setText("");
}
}
);
#s1 ends here
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setView(view).setPositiveButton("Add to Cart", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onAddToCart();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
public void onAddToCart(){
//PLAUSIBLE PROBLEM HERE - i think, Something must be wrong here
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this.getContext(),R.layout.cartlist_layout,names);
l1=(ListView)rl.findViewById(R.id.listview_cart);
l1.setAdapter(adapter1);
Toast.makeText(this.getContext(),"Added to Cart:"+l1.getItemAtPosition(0),Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
当你夸大你的fragment_cart布局时,你没有将它附加到任何东西,这就是为什么你看不到你的结果。
从您显示的图像来看,fragment_cart布局似乎实际上是活动的一部分。
你可以做这样的事情
主要活动
public class MainActivity extends Activity {
public Fragment mCartFragment;
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...
mCartFragment = new CartFragment();
// Attach this fragment to your main activity
}
}
购物车碎片
public class CartFragment extends Fragment {
View mView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle s) {
// inflate fragment_cart here
mView = inflater.inflate(
R.layout.fragment_cart,
container,
false
);
return mView;
}
}
在你的addToCart()方法中(假设MainActivity负责启动CartDialog):
public void addToCart(){
MainActivity mainActivity = (MainActivity)getActivity();
View fragmentCartView = mainActivity.mCartFragment.getView();
if(fragmentCartView != null){
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this.getContext(),R.layout.cartlist_layout,names);
l1 = fragmentCartView.findViewById(R.id.listview_cart);
l1.setAdapter(adapter1);
Toast.makeText(this.getContext(),"Added to Cart:"+l1.getItemAtPosition(0),Toast.LENGTH_SHORT).show();
}
}
现在,我不太喜欢这个解决方案,因为你将CartDialog与主要活动相结合,但我并不想过多地改变你的代码。
答案 1 :(得分:0)
尝试在您的方法中添加此内容:
adapter1.notifyDataSetChanged();