-1

image1Fetch значение EditText в диалоговом окне, также требуется дополнение для тех пользователей, введенные значения

image2

Привет, я пытаюсь получить диалоговое окно при нажатии на кнопку «Добавить» ....

До этого я хотел бы дать вам небольшое объяснение, чего мне нужно достичь. Здесь у меня есть элементы listView. В список входят имена элементов и editText. Пользователь может вводить значения в этом editText. Предположим, что пользователь вводит три значения элемента в тексте редактирования (например: Tomoto 30, Soup 40, Biriyani 50). Кроме того, есть кнопка «ADD», которая показывает всплывающее окно с диалоговым окном на том же экране при нажатии. Внутри диалогового окна должно быть указано имя элемента и введенное пользователем значение, общее количество также необходимо в случае, если приведенный выше пример (Tomoto 30, Soup 40, Biriyani 50..Total 120). Если кто-то может помочь, это будет легко вперед и достичь своей приложения .... Заранее спасибо

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

ответ

0

Я помогу вам с некоторыми пояснениями и несколько строк кода, но это до вас, чтобы продолжить исследования и написать код.

Прежде всего, сделайте класс AmountDialog для продления DialogFragment. Переопределение OnCreateDialog метод, как это:

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

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

Это было бы основные шаги. Не забудьте создать макет для диалога. И найдите, как манипулировать значениями, извлеченными из диалога через действия.