Question: Create Delete Dialog on selected piechart value?

Question

Create Delete Dialog on selected piechart value?

Answers 1
Added at 2016-12-29 14:12
Tags
Question

I created this android app using the MPAndroidChart Library, which allows the user to create a quick PieChart using an ArrayList (I added some screenshots, for clarification).

My problem is that I want to allow the user to delete an entry from the piechart (and the array list), the pie chart should then be rendered without the removed entry.

Is there any way I can achieve this by allowing the user to long press an item, which opens some sort of yes/no dialog?

Screenshots gallery

Code:

package com.example.quickpie;



public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final List<PieEntry> entries = new ArrayList<>();

    final EditText editText = (EditText) findViewById(R.id.editText);
    final EditText editText2 = (EditText) findViewById(R.id.editText2);
    final EditText editText3 = (EditText) findViewById(R.id.editText3);
    final Button button = (Button) findViewById(R.id.button);
    final Button button2 = (Button) findViewById(R.id.button2);
    final TextView textView = (TextView) findViewById(R.id.textView);
    final TextView textView2 = (TextView) findViewById(R.id.textView2);
    final TextView textView3 = (TextView) findViewById(R.id.textView3);

    editText.requestFocus();

    button.setOnClickListener(new View.OnClickListener() {
                                   public void onClick (View v) {

                                       //if(editText.getFreezesText())

                                       if (editText.getText().toString().matches("")) {
                                           Toast.makeText(getBaseContext(), "You did not enter a Valid Item ID", Toast.LENGTH_LONG).show();
                                           return;
                                       }

                                       else if (editText2.getText().toString().matches("")) {
                                           Toast.makeText(getBaseContext(), "You did not enter a Valid Quantitiy", Toast.LENGTH_LONG).show();
                                           return;

                                       }

                                       else {












                                       String nomen = (editText.getText().toString());
                                       float number = Float.parseFloat(editText2.getText().toString());



                                           final PieChart piechart = (PieChart) findViewById(R.id.piechart);
                                           entries.add(new PieEntry(number, nomen));

                                           editText.setText("");
                                           editText2.setText("");
                                           textView3.setSingleLine(false);
                                           textView3.append("\n" + nomen + " " + number);
                                           editText.requestFocus();


                                           PieDataSet set = new PieDataSet(entries, "");


                                           //colors

                                           getResources().getColor(R.color.violp);
                                           getResources().getColor(R.color.bluep);
                                           getResources().getColor(R.color.redp);
                                           getResources().getColor(R.color.greenp);
                                           getResources().getColor(R.color.yellowp);
                                           getResources().getColor(R.color.orangep);
                                           getResources().getColor(R.color.lightbluep);
                                           getResources().getColor(R.color.purplep);
                                           getResources().getColor(R.color.darkredp);


                                           set.setColors(new int[]{R.color.bluep, R.color.greenp, R.color.violp, R.color.redp, R.color.yellowp, R.color.orangep, R.color.lightbluep, R.color.purplep, R.color.darkredp}, getApplicationContext());
                                           PieData data = new PieData(set);
                                           piechart.setData(data);


                                           Description description = new Description();
                                           description.setText(getResources().getString(R.string.besch));
                                           piechart.setDescription(description);

                                           Toast.makeText(getBaseContext(), (R.string.loading),
                                                   Toast.LENGTH_SHORT).show();


                                           String mitte = (editText3.getText().toString());
                                           piechart.setCenterText(mitte);


                                           piechart.setCenterTextSize(30);
                                           piechart.setNoDataText(String.valueOf(R.string.nodata));
                                           piechart.notifyDataSetChanged();

                                           piechart.animateX(2000, Easing.EasingOption.EaseInExpo);
                                           piechart.animateY(2000, Easing.EasingOption.EaseInExpo);

                                           piechart.invalidate();
                                           editText3.setVisibility(View.VISIBLE);

                                           editText3.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                                               @Override
                                               public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                                                   if (actionId == EditorInfo.IME_ACTION_DONE) {

                                                       String mitte = (editText3.getText().toString());
                                                       piechart.setCenterText(mitte);
                                                       editText3.setText("");
                                                       editText.requestFocus(); //editText3.setVisibility(View.INVISIBLE);

                                                       InputMethodManager imm = (InputMethodManager) getSystemService(Main.INPUT_METHOD_SERVICE);
                                                       imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);


                                                       //do something
                                                   }
                                                   return false;
                                               }
                                           });

                                           editText3.setOnTouchListener(new View.OnTouchListener() {
                                               @Override
                                               public boolean onTouch(View v, MotionEvent event) {
                                                   if (MotionEvent.ACTION_UP == event.getAction()) {
                                                       piechart.setCenterText("");
                                                       textView3.setHint(R.string.hint1);

                                                   }

                                                   return false; // return is important...
                                               }
                                           });


                                           button.setOnLongClickListener(new View.OnLongClickListener() {

                                               @Override
                                               public boolean onLongClick(View v) {

                                                   Toast.makeText(getBaseContext(), R.string.longpresshint, Toast.LENGTH_LONG).show();

                                                   return true;
                                               }
                                           });



















                                           final Button button2 = (Button) findViewById(R.id.button2);
                                           button2.setOnClickListener(new View.OnClickListener() {


                                               @Override
                                               public void onClick(View v) {
                                                   String mitte = (editText3.getText().toString());
                                                   piechart.setCenterText(mitte);
                                                   //piechart.setVisibility(View.VISIBLE);
                                                   //editText.setVisibility(View.INVISIBLE);
                                                   //textView2.setVisibility(View.INVISIBLE);
                                                   //textView3.setVisibility(View.INVISIBLE);
                                                   //editText3.setVisibility(View.INVISIBLE);
                                                   // editText2.setVisibility(View.INVISIBLE);
                                                   //button.setVisibility(View.INVISIBLE);
                                                   //button2.setVisibility(View.INVISIBLE);

                                                   Toast.makeText(getBaseContext(), (R.string.loading),
                                                           Toast.LENGTH_SHORT).show();


                                                   piechart.animateX(2000, Easing.EasingOption.EaseInExpo);
                                                   piechart.animateY(2000, Easing.EasingOption.EaseInExpo);

                                                   InputMethodManager imm = (InputMethodManager) getSystemService(Main.INPUT_METHOD_SERVICE);
                                                   imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);


                                               }


                                           });
                                       }}
    }
    )
    ;
}

@Override
public void onBackPressed(){
    super.onBackPressed();
    Intent intent = getIntent();
    finish();
    startActivity(intent);


}



}
Answers to

Create Delete Dialog on selected piechart value?

nr: #1 dodano: 2016-12-29 14:12

Please try this

    button.setOnClickListener(new View.OnClickListener() {
                                   public void onClick (View v) {

                                       //if(editText.getFreezesText())

                                       if (editText.getText().toString().matches("")) {
                                           Toast.makeText(getBaseContext(), "You did not enter a Valid Item ID", Toast.LENGTH_LONG).show();
                                           return;
                                       }

                                       else if (editText2.getText().toString().matches("")) {
                                           Toast.makeText(getBaseContext(), "You did not enter a Valid Quantitiy", Toast.LENGTH_LONG).show();
                                           return;

                                       }

                                       else {
piechart.invalidate();

}
Source Show
◀ Wstecz