-1

image1Fetch EditText Wert in Dialogfeld Zusätzlich wird benötigt zusätzlich für jene Benutzer eingegebenen Werte

image2

Hallo, ich versuche, Dialogfeld zu bekommen, während die „ADD“ klicken ....

Davor möchte ich Ihnen eine kleine Erklärung geben, was ich erreichen muss. Hier habe ich listView-Elemente. Die Liste enthält Elementname und editText. Benutzer kann Werte in diesen editText eingeben. Angenommen, ein Benutzer gibt drei Werte im Bearbeitungstext ein (z. B. Tomoto 30, Suppe 40, Biriyani 50). Außerdem gibt es einen "HINZUFÜGEN" Knopf, der ein Popup mit einem Dialogfeld auf demselben Bildschirm anzeigt, wenn Sie klicken. Innerhalb des Dialogfelds muss der Name des Elements und der vom Benutzer eingegebene Wert angezeigt werden. Diese Summe wird auch für das obige Beispiel benötigt (Tomoto 30, Soup 40, Biriyani 50..Total 120). Wenn jemand helfen kann, es wird leicht sein, meine Anwendung zu übermitteln und erreichen .... Vielen Dank im Voraus

=========== 
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` 

Antwort

0

ich Sie mit einigen Erklärungen und wenigen Zeilen Code helfen, aber es ist an Ihnen, Forschung fortzusetzen und schreibe Code.

Zuerst eine Klasse AmountDialog erstellen, um DialogFragment zu erweitern. Überschreiben Sie die OnCreateDialog Methode wie folgt:

@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(); 

In 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"); 
        } 
       }); 

Diese würden die grundlegenden Schritte sein. Vergessen Sie nicht, das Layout für den Dialog zu erstellen. Und suchen Sie, wie Sie die aus dem Dialog extrahierten Werte über Aktivitäten bearbeiten können.