-1

image1Fetch valeur EditText dans la boîte de dialogue, ajout également requis pour les utilisateurs valeurs saisies

image2

Salut, j'essaie d'obtenir boîte de dialogue en cliquant sur le bouton « Ajouter » ....

Avant cela, je voudrais vous donner une petite explication de ce que je dois accomplir. Ici, j'ai des éléments listView. La liste comprend le nom des éléments et editText. L'utilisateur peut entrer des valeurs dans cet editText. Supposons qu'un utilisateur saisisse trois valeurs d'éléments dans le texte d'édition (par exemple: Tomoto 30, Soup 40, Biriyani 50). En outre, il y a un bouton "ADD" qui affiche une fenêtre contextuelle avec une boîte de dialogue sur le même écran lorsque vous cliquez. À l'intérieur de la boîte de dialogue, il doit être montré le nom de l'article et la valeur entrée par l'utilisateur, ces totaux également nécessaires dans le cas de l'exemple ci-dessus (Tomoto 30, Soup 40, Biriyani 50..Total 120). Si quelqu'un peut aider, il sera facile de transmettre et de réaliser ma demande .... Merci à l'avance

=========== 
my_row.xml 
    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:orientation="vertical" android:padding="6dip"> 
     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="150dp" 
      android:layout_height="100dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="16dp" 
      android:src="@drawable/ic_soup"/> 

     <TextView android:id="@+id/description" 
      android:layout_width="100sp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/imageView" 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium"/> 

     <TextView android:id="@+id/itemNumber" 
      android:layout_width="100sp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/imageView" 
      android:layout_below="@+id/description" 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <EditText android:id="@+id/quantity" 
      android:layout_width="80dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/itemNumber" 
      android:inputType="number" 
      android:layout_marginTop="50dp" 
      android:ems="10" 
      android:hint="0.00" 

      /> 

    </RelativeLayout> 

    =========== 
    activity_main.xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.info.detail.textwatcher.MainActivity"> 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:layout_marginTop="1dp" 
      android:layout_marginBottom="45dp" 
      tools:listitem="@layout/my_row" 
      /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:orientation="horizontal" 
      android:background="#46BB09"> 

      <!-- <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="SEND QUERY" 
       android:textSize="20dp" 
       android:textColor="@android:color/white" 
       android:layout_weight="2" 
       android:layout_marginLeft="30dp"/>--> 
      <Button 
       android:id="@+id/btnAdd" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="ADD" 
       android:layout_weight="1" 
       android:textSize="16dp" 
       android:background="@null" 
       android:textColor="@android:color/white"/> 
     </LinearLayout> 

    </RelativeLayout> 

    public class MainActivity extends AppCompatActivity { 
     private MyCustomAdapter dataAdapter = null; 

     public Button button; 
     public EditText ediText; 
     TextView textView; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      displayListView(); 
      button = (Button) findViewById(R.id.btnAdd); 
      ediText=(EditText) findViewById(R.id.quantity); 
      textView = (TextView) findViewById(R.id.description); 

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

        AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(MainActivity.this); 


        alertdialogbuilder.setTitle("Selected Items"); 


         } 
        }); 
     } 

     private void displayListView() { 

      //Array list of products 
      ArrayList<Product> productList = new ArrayList<Product>(); 
      Product product = new Product(R.drawable.ic_soup, "Tomoto", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_soup, "Soup", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_biriyani, "Biriyani", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_soup, "Chilli", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_soup, "Powder", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_soup, "Apple", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_soup, "Orange", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_soup, "Banana", "desc"); 
      productList.add(product); 
      product = new Product(R.drawable.ic_soup, "Mango", "desc"); 
      productList.add(product); 


      //create an ArrayAdaptar from the String Array 
      dataAdapter = new MyCustomAdapter(this, R.layout.my_row, productList); 
      ListView listView = (ListView) findViewById(R.id.listView1); 
      // Assign adapter to ListView 
      listView.setAdapter(dataAdapter); 
     } 

     private class MyCustomAdapter extends ArrayAdapter<Product> { 

      private ArrayList<Product> productList; 

      public MyCustomAdapter(Context context, int textViewResourceId, 
            ArrayList<Product> productList) { 
       super(context, textViewResourceId, productList); 
       this.productList = new ArrayList<Product>(); 
       this.productList.addAll(productList); 
      } 

      @Override 
      public View getView(int position, View view, ViewGroup parent) { 

       Product product = productList.get(position); 

       if (view == null) { 
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = vi.inflate(R.layout.my_row, null); 
        EditText quantity = (EditText) view.findViewById(R.id.quantity); 
        //attach the TextWatcher listener to the EditText 
        quantity.addTextChangedListener(new MyTextWatcher(view)); 

       } 

       EditText quantity = (EditText) view.findViewById(R.id.quantity); 
       quantity.setTag(product); 
       if (product.getQuantity() != 0) { 
        quantity.setText(String.valueOf(product.getQuantity())); 
       } else { 
        quantity.setText(""); 
       } 
       ImageView itemimage = (ImageView) view.findViewById(R.id.imageView); 
       itemimage.setImageResource(product.getItemimage()); 
       TextView itemNumber = (TextView) view.findViewById(R.id.itemNumber); 
       itemNumber.setText(product.getTitle()); 
       TextView description = (TextView) view.findViewById(R.id.description); 
       description.setText(product.getDescription()); 
       return view; 

      } 

     } 

     private class MyTextWatcher implements TextWatcher { 

      private View view; 

      private MyTextWatcher(View view) { 
       this.view = view; 
      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       //do nothing 
      } 

      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       //do nothing 
      } 

      public void afterTextChanged(Editable s) { 
       String qtyString = s.toString().trim(); 
       int quantity = qtyString.equals("") ? 0 : Integer.valueOf(qtyString); 

       EditText qtyView = (EditText) view.findViewById(R.id.quantity); 
       Product product = (Product) qtyView.getTag(); 
       product.setQuantity(quantity); 

      } 
     } 
    }`enter code here` 

Répondre

0

je vais vous aider avec quelques explications et quelques lignes de code, mais il est à vous de poursuivre la recherche et écrire du code.

D'abord, faites une classe AmountDialog pour étendre DialogFragment. Remplacer la méthode OnCreateDialog comme ceci:

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 
     //for this line you must create the layout for your dialog 
     View v = inflater.inflate(R.layout.dialog, null); 
     //extract view references like this: 
     final TextView amount = (TextView) v.findViewById(R.id.amount);//here will be the id of the total 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Total price") 
       .setView(v) 
       .setPositiveButton(R.string.OkButton, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //do something with the amount: 
         String amountValue = amount.getText().toString(); 
        } 
       }) 
       .setNegativeButton(R.string.CancelButton, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //nothing here, it will dismiss by default 
        } 
       }); 
     return builder.create(); 

En MainActivity:

button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //note: import android.support.v4.app.FragmentManager;!!! 
       FragmentManager fm = getSupportFragmentManager(); 
       AmountDialog alert = new AmountDialog(); 
       alert.show(fm, "AmountDialog"); 
        } 
       }); 

Ceux-ci seraient les étapes de base. Ne pas oublier de créer la mise en page pour la boîte de dialogue. Et cherchez comment manipuler les valeurs extraites du dialogue à travers les activités.