嗨,我是Android新手,我已经搜索了一个星期, 我有一个包含的片段
在具有唯一标签的容器中添加和删除相同的片段。在微调器中,活动中会动态显示多个数字,如果我添加或删除相同的片段,textview中显示的值应动态更改。我知道可以通过其标签访问每个实例,并且可以获取活动中的值,但无法找到哪个片段实例已更改。
例如,如果我四次添加相同的片段,则在所有四个片段中更改了微调框,无法确定哪个实例增加或减少并纠正总数
,也无法找到是否添加了图像。
我的片段是
public class Service_details extends Fragment {
private static final String IMAGE_DIRECTORY = "/demonuts";
private int GALLERY = 1, CAMERA = 2;
ProgressDialog pDialog;
TextView textView_service_no, textView_datePicker, textView_amount, textView_optionalDate;
Spinner spinner_services;
EditText editText_dedicated, editText_intention;
ImageView display_photo;
HashMap<String, String> cList = new HashMap<>();
String[] services;
String initial_spinner_value;
String birthday_price;
String deathday_price;
String rosary_price;
String mass_price;
String prayer_price;
String marriageday_price;
OnMessageReadListener messageReadListener;
String service_value = null;
String service_spinner_string=null;
String person_image_string =null;
String editText_dedicated_string=null;
String editText_intention_string=null;
String textView_telecast_date_string=null;
String textView_amount_string =null;
String total_amount_string=null;
String tax_amount_string=null;
String final_total_amount_string=null;
int c;
int count;
String TAG = "Fragment";
Bitmap bitmap;
LinearLayout linearLayout10;
public interface OnMessageReadListener {
void onMessageRead(String message);
}
public Service_details() {
// Required empty public constructor
}
public static Service_details newInstance(int i) {
Service_details service_details = new Service_details();
Bundle bundle = new Bundle();
int c = i;
bundle.putInt("service_count",i);
service_details.setArguments(bundle);
return service_details;
}
Calendar cal;
public String message;
String path;
int temp;
int a;
@SuppressLint("SetTextI18n")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_service_details, container, false);
textView_service_no = view.findViewById(R.id.service_counting);
editText_dedicated = view.findViewById(R.id.editText_dedicated_to);
editText_intention = view.findViewById(R.id.editText_intention);
textView_datePicker = view.findViewById(R.id.date_picker);
spinner_services = view.findViewById(R.id.spinner_services);
textView_amount = view.findViewById(R.id.textview_amount);
display_photo = view.findViewById(R.id.display_photo);
textView_optionalDate = view.findViewById(R.id.textview_exactDate);
cal = Calendar.getInstance();
linearLayout10 = view.findViewById(R.id.linear10);
ButterKnife.bind(this, view);
getJson();
assert getArguments() != null;
count = getArguments().getInt("service_count");
Log.e(TAG, "count value : "+ count);
textView_service_no.setText(valueOf(count));
++count;
a=0;
display_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPictureDialog();
}
});
textView_optionalDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
textView_optionalDate.setText(""+arg3+"/"+(arg2+1)+"/"+arg1);
Toast.makeText(getActivity(), ""+arg3+"/"+(arg2+1)+"/"+arg1, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
});
textView_datePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
textView_datePicker.setText(""+arg3+"/"+(arg2+1)+"/"+arg1);
Toast.makeText(getActivity(), ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
cal.add(Calendar.DAY_OF_WEEK_IN_MONTH, 5);
dialog.getDatePicker().setMinDate(cal.getTimeInMillis());
dialog.show();
}
});
services = new String[]{"Select Service","Birth Day", "Death Day", "Rosary", "Hollymass", "Prayer", "Marriage Day"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, services);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner_services.setAdapter(arrayAdapter);
spinner_services.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spin_value = parent.getItemAtPosition(position).toString();
((Services)getActivity()).setCurrentSpinnerItem(spin_value);
switch (position) {
case 0:
textView_amount.setText(cList.get("empty"));
display_photo.setVisibility(View.GONE);
linearLayout10.setVisibility(View.GONE);
Log.e(TAG,"textView_amount (fragment name)"+ textView_amount.getTag());
String message_temp = textView_amount.getText().toString();
if(!message_temp.equals("")){
a = Integer.parseInt(message_temp);
}
a = a-temp;
message = Integer.toString(a);
Log.e(TAG, "message_temp: "+message_temp);
messageReadListener.onMessageRead(message);
break;
case 1:
textView_amount.setText(cList.get("birthday_price"));
display_photo.setVisibility(View.VISIBLE);
linearLayout10.setVisibility(View.VISIBLE);
message_temp = textView_amount.getText().toString();
if(!message_temp.equals("")){
a = Integer.parseInt(message_temp);
}
a = a-temp;
message = Integer.toString(a);
a= Integer.parseInt(message_temp);
a= a-temp;
message = Integer.toString(a);*/
messageReadListener.onMessageRead(message);
break;
case 2:
textView_amount.setText(cList.get("deathday_price"));
display_photo.setVisibility(View.VISIBLE);
linearLayout10.setVisibility(View.GONE);
message_temp = textView_amount.getText().toString();
if(!message_temp.equals("")){
a = Integer.parseInt(message_temp);
}
a = a-temp;
message = Integer.toString(a);
messageReadListener.onMessageRead(message);
break;
case 3:
textView_amount.setText(cList.get("rosary_price"));
display_photo.setVisibility(View.GONE);
linearLayout10.setVisibility(View.GONE);
message_temp = textView_amount.getText().toString();
if(!message_temp.equals("")){
a = Integer.parseInt(message_temp);
}
a = a-temp;
message = Integer.toString(a);
messageReadListener.onMessageRead(message);
break;
case 4:
textView_amount.setText(cList.get("mass_price"));
display_photo.setVisibility(View.GONE);
linearLayout10.setVisibility(View.GONE);
message_temp = textView_amount.getText().toString();
if(!message_temp.equals("")){
a = Integer.parseInt(message_temp);
}
a = a-temp;
message = Integer.toString(a);
messageReadListener.onMessageRead(message);
break;
case 5:
textView_amount.setText(cList.get("prayer_price"));
display_photo.setVisibility(View.GONE);
linearLayout10.setVisibility(View.GONE);
message_temp = textView_amount.getText().toString();
if(!message_temp.equals("")){
a = Integer.parseInt(message_temp);
}
a = a-temp;
message = Integer.toString(a);
messageReadListener.onMessageRead(message);
break;
case 6:
textView_amount.setText(cList.get("marriageday_price"));
display_photo.setVisibility(View.GONE);
linearLayout10.setVisibility(View.VISIBLE);
message_temp = textView_amount.getText().toString();
if(!message_temp.equals("")){
a = Integer.parseInt(message_temp);
}
a = a-temp;
message = Integer.toString(a);
messageReadListener.onMessageRead(message);
break;
}
// String message = textView_amount.getText().toString();
// messageReadListener.onMessageRead(message);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
private void showPictureDialog(){
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(getActivity());
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {
"Select photo from gallery",
"Capture photo from camera" };
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
}
}
});
pictureDialog.show();
}
public void choosePhotoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}
private void takePhotoFromCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
if (data != null) {
Uri contentURI = data.getData();
Log.e(TAG, "contentURI : " + contentURI);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), contentURI);
String path = saveImage(bitmap);
Toast.makeText(getActivity(), "Image Saved!", Toast.LENGTH_SHORT).show();
display_photo.setImageBitmap(bitmap);
Log.e(TAG, "PATH1" + path);
Log.e(TAG, "BITMAP " + bitmap);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Failed!", Toast.LENGTH_SHORT).show();
}
}
} else if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
display_photo.setImageBitmap(thumbnail);
path = saveImage(thumbnail);
Log.e(TAG, "PATH2" + path);
saveImage(thumbnail);
Toast.makeText(getActivity(), "Image Saved!", Toast.LENGTH_SHORT).show();
}
}
public String saveImage(Bitmap myBitmap) {
Log.e(TAG, "PATH3" + path);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
Log.e(TAG, "wallpaperDirectory" + wallpaperDirectory);
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
}
try {
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(getActivity(),
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
Log.e(TAG, "File Saved::--->" + f.getAbsolutePath());
Log.e(TAG, "PATH4" + path);
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}
/*------------------------------------------------------------------------------------------------------------*/
public void getDetailsFromFragment() {
service_value = textView_service_no.getText().toString();
editText_dedicated_string = editText_dedicated.getText().toString();
editText_intention_string = editText_intention.getText().toString();
textView_telecast_date_string = textView_datePicker.getText().toString();
textView_amount_string = textView_amount.getText().toString();
service_spinner_string = spinner_services.getSelectedItem().toString();
if(bitmap!=null){
person_image_string = bitmap.toString();
}
Log.e(TAG, " SERVICE NO. : " + service_value + " \n" + "SERVICES :" + service_spinner_string + " \n" + "DEDICATED TO : " + editText_dedicated_string + " \n" + " INTENTION : " +
editText_intention_string + " \n" + " TELECASTE DATE : " + textView_telecast_date_string + " \n" + " AMOUNT : " + textView_amount_string + "\n" + "IMAGE : " + person_image_string);
Log.e(TAG, "FINAL PAYMENT DETAILS: " + total_amount_string + " " + tax_amount_string + " " + final_total_amount_string);
messageReadListener.onMessageRead(message);
}
public void changeCount(int a){
c = a;
textView_service_no.setText(valueOf(c));
}
@Override
public void onAttach (Context context){
super.onAttach(context);
Activity activity = (Activity) context;
try {
messageReadListener = (OnMessageReadListener) activity;
} catch (ClassCastException c) {
throw new ClassCastException(activity.toString() + " must override onMessageReadListener...");
}
}
}
My activity is
public class Services extends DashBoard implements Service_details.OnMessageReadListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services);
}
@OnClick(R.id.add_service)
public void addService(){
service_one_amount = Integer.parseInt(message4);
// service_one_amount = message4;
Log.e(TAG, "service_one_amount : "+ service_one_amount);
// service_one_amount = service_one_amount+ ;
if(size<5){
fragTag = "numericField" + numericFragmentCount;
getSupportFragmentManager().beginTransaction().add(R.id.container_frame,Service_details.newInstance(a), fragTag).commit();
++a;
++numericFragmentCount;
Log.e(TAG, "fragment size: "+ size);
}else {
Toast.makeText(this,"Maximum limit reached",Toast.LENGTH_LONG).show();
}
}
@OnClick(R.id.remove_service)
public void removeService(){
size= getSupportFragmentManager().getFragments().size();
if(size>1) {
--numericFragmentCount;
fragTag = "numericField" + numericFragmentCount;
fragment = getSupportFragmentManager().findFragmentById(R.id.container_frame);
if (fragment != null) {
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
--a;
}
Log.e(TAG, "fragment : " + fragment);
}else {
Toast.makeText(this,"Minimum limit reached",Toast.LENGTH_LONG).show();
}
}
@SuppressLint("SetTextI18n")
@Override
public void onMessageRead(String message) {
try{
String identity_frag = "numericField1";
Fragment service_details1 = getSupportFragmentManager().findFragmentByTag("numericField1");
Fragment service_details2 = getSupportFragmentManager().findFragmentByTag("numericField2");
Fragment service_details3 = getSupportFragmentManager().findFragmentByTag("numericField3");
Fragment service_details4 = getSupportFragmentManager().findFragmentByTag("numericField4");
Fragment service_details5 = getSupportFragmentManager().findFragmentByTag("numericField5");
Log.e(TAG,"before adding message4: "+message4);
message1 = Integer.parseInt(message);
service_details1 instanceof Service_details;
Log.e(TAG,"before initial temp: "+ temp);
if(!textView_amount_view.getText().toString().equals("0")){
initial_amount= String.valueOf(message4);
}else {
initial_amount =textView_amount_view.getText().toString();
}
message3 = Integer.parseInt(initial_amount);
message1= message3+message1-temp;
temp = message1;
Log.e(TAG,"after initial temp: "+ temp);
message1 = message1+service_one_amount;
long res = (long) Math.ceil((message1/ 100.0f) * 18);
double res1 = ((message1/ 100f) * 18);
Toast.makeText(getApplicationContext(), "" + res1, Toast.LENGTH_SHORT).show();
int final_amount = (int) (message1 + res);
message4 = Integer.toString(message1);
Log.e(TAG,"after adding message4: "+message4);
textView_amount_view.setText(currency_symbol+" "+ String.valueOf(message4));
textView_tax_amount_view.setText(currency_symbol+" "+ String.valueOf(res)+ decimal_value);
textView_total_amount_view.setText(currency_symbol+" "+ String.valueOf(final_amount)+ decimal_value);
Log.e(TAG,"total amount: "+message4);
Log.e(TAG,"tax amount: "+res);
Log.e(TAG,"amount after tax: "+final_amount);
}catch (NumberFormatException e){
e.printStackTrace();
}
}
}
如果您发现问题很难,请投票以吸引更多人。 在此先感谢。