2016-12-26 16 views
3

减少TextView的数量,我创建了两个按钮,TextView的,在这里如果我点击加号按钮TextView的应该增加,如果我减少,反之亦然。现在我做了一个活动,在点击列表视图项目一个对话框将出现,它有一些复选框,按钮,微调等在那里。我尝试将代码添加到警报框和我的按钮。但它不做工精细,如果我点击加号按钮它给人的最后极限是45变成46,如果我点击下降按钮,它只是降低了一次..如何增加或通过按钮的Android

这里是我的子菜单活动:

public class SubMenu extends AppCompatActivity { 

    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ListView listview; 
    ListViewAdapter adapter; 
    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    static String RANK = "id"; 
    static String COUNTRY = "name"; 

    static String FLAG = "image"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     setContentView(R.layout.activity_sub_menu); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     String SelectedId = getIntent().getStringExtra("id"); 


     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 






     // Get the view from listview_main.xml 

     // Execute DownloadJSON AsyncTask 
     new DownloadJSON().execute(); 
    } 

    // DownloadJSON AsyncTask 
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener { 

     // @Override 
     // protected void onPreExecute() { 
     // super.onPreExecute(); 
     // Create a progressdialog 
     // mProgressDialog = new ProgressDialog(SubMenu.this); 
     // Set progressdialog title 
     // mProgressDialog.setTitle("Categories of Main categories....."); 
     // Set progressdialog message 
     // mProgressDialog.setMessage("Loading..."); 
     // mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     // mProgressDialog.show(); 
     // } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Create an array 
      arraylist = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 
      jsonarray = JsonFunctions 
        .getJSONfromURL("http://cloud.......com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id")); 
      try { 
       // Locate the array name in JSON 
//     jsonarray = jsonobject.getJSONArray("main_menu_items"); 


       for (int i = 0; i < jsonarray.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 

        jsonobject = jsonarray.getJSONObject(i); 
        // Retrive JSON Objects 
        // map.put("id", jsonobject.getString("id")); 
        map.put("name", jsonobject.getString("name")); 

        map.put("image", jsonobject.getString("image")); 
        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 
      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.list1); 
      // Pass the results into ListViewAdapter.java 
      adapter = new ListViewAdapter(SubMenu.this, arraylist); 
      // Set the adapter to the ListView 
      listview.setAdapter(adapter); 
      listview.setOnItemClickListener(this); 
      // Close the progressdialog 
      // mProgressDialog.dismiss(); 
     } 



     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long rowId) { 





      final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create(); 
      final LayoutInflater inflater = getLayoutInflater(); 
      final View dialogView = inflater.inflate(R.layout.popup 
        , null); 
      customDialog.setView(dialogView); 
      // set the custom customDialogimation components - text, image and button 
      final TextView tvDistance = (TextView) dialogView.findViewById(R.id.h2); 

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

       @Override 
       public void onClick(View v) { 

        customDialog.dismiss(); 


       } 
      }); 





      final Button cncl=(Button)dialogView.findViewById(R.id.canc) ; 
      cncl.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        customDialog.dismiss(); 


       } 
      }); 


     Button _decrease = (Button) dialogView.findViewById(R.id.incr); 
      Button _increase = (Button) dialogView.findViewById(R.id.decr); 
      final TextView _value = (TextView) dialogView.findViewById(R.id.value); 




    _decrease.setOnClickListener(new View.OnClickListener() { 

      int currentID = (int) System.currentTimeMillis(); 
      int lastID; 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       removeLastTextView(); 
       lastID--; 
      } 
     }); 

     _increase.setOnClickListener(new View.OnClickListener() { 

      int currentID = (int) System.currentTimeMillis(); 
      int lastID; 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       addNewTextView(); 
       lastID = currentID; 
       currentID++; 
      } 
     }); 
    } 
} 



    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       onBackPressed(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

这里就是我想要做微调这样的形象: 我想希望有微调的图像 enter image description here

+0

是否有应显示在提到TextView的首次任何默认值? –

+0

您已初始化onClick()的值。 int _counter = 45;所以你面临这个错误 – Noorul

+0

不,我没有提到任何默认值valur thr –

回答

2

这种替换下面的代码:

public class SubMenu extends AppCompatActivity { 

    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ListView listview; 
    ListViewAdapter adapter; 
    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    static String RANK = "id"; 
    static String COUNTRY = "name"; 

    static String FLAG = "image"; 
    Integer i = 1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     setContentView(R.layout.activity_sub_menu); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     String SelectedId = getIntent().getStringExtra("id"); 


     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


     // Get the view from listview_main.xml 

     // Execute DownloadJSON AsyncTask 
     new DownloadJSON().execute(); 
    } 

    // DownloadJSON AsyncTask 
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener { 

     // @Override 
     // protected void onPreExecute() { 
     // super.onPreExecute(); 
     // Create a progressdialog 
     // mProgressDialog = new ProgressDialog(SubMenu.this); 
     // Set progressdialog title 
     // mProgressDialog.setTitle("Categories of Main categories....."); 
     // Set progressdialog message 
     // mProgressDialog.setMessage("Loading..."); 
     // mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     // mProgressDialog.show(); 
     // } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Create an array 
      arraylist = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 
      jsonarray = JsonFunctions 
        .getJSONfromURL("http://cloud.......com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id")); 
      try { 
       // Locate the array name in JSON 
//     jsonarray = jsonobject.getJSONArray("main_menu_items"); 


       for (int i = 0; i < jsonarray.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 

        jsonobject = jsonarray.getJSONObject(i); 
        // Retrive JSON Objects 
        // map.put("id", jsonobject.getString("id")); 
        map.put("name", jsonobject.getString("name")); 

        map.put("image", jsonobject.getString("image")); 
        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 
      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.list1); 
      // Pass the results into ListViewAdapter.java 
      adapter = new ListViewAdapter(SubMenu.this, arraylist); 
      // Set the adapter to the ListView 
      listview.setAdapter(adapter); 
      listview.setOnItemClickListener(this); 
      // Close the progressdialog 
      // mProgressDialog.dismiss(); 
     } 


     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long rowId) { 


      final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create(); 
      final LayoutInflater inflater = getLayoutInflater(); 
      final View dialogView = inflater.inflate(R.layout.popup 
        , null); 
      customDialog.setView(dialogView); 
      // set the custom customDialogimation components - text, image and button 
      final TextView tvDistance = (TextView) dialogView.findViewById(R.id.h2); 

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

       @Override 
       public void onClick(View v) { 

        customDialog.dismiss(); 


       } 
      }); 


      final Button cncl = (Button) dialogView.findViewById(R.id.canc); 
      cncl.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        customDialog.dismiss(); 


       } 
      }); 


      Button _decrease = (Button) dialogView.findViewById(R.id.incr); 
      Button _increase = (Button) dialogView.findViewById(R.id.decr); 
      final TextView _value = (TextView) dialogView.findViewById(R.id.value); 
      i = Integer.parseInt(_value.getText().toString()); 


      _decrease.setOnClickListener(new View.OnClickListener() { 


      public void onClick(View v) { 
       String _stringVal; 
       Log.d("src", "Decreasing value..."); 
       if (i > 0) { 
        i = i - 1; 
        _stringVal = String.valueOf(i); 
        _value.setText(_stringVal); 
       } else { 
        Log.d("src", "Value can't be less than 0"); 
       } 

      } 
     }); 

      _increase.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        String _stringVal; 

        Log.d("src", "Increasing value..."); 
        i = i + 1; 
        _stringVal = String.valueOf(i); 
        _value.setText(_stringVal); 
       } 
      }); 
      customDialog.show(); 
     } 


    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       onBackPressed(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 
+0

yess..ur冠军......但有一件事情它不是骗子然后0或-1等等。 –

+0

好像我减少它应该不会少于0 –

+0

好吧。让我试着去解决它。给我一点时间。 –

1

创建的,这样以后可以找到textViews ID数组(不一定局限于) 。

你可以做以下

final int currentID = (int)System.currentTimeMillis(); 
final int lastID ; 

plusButton.setOnClickListener(new OnClickListener(){ 
@Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      addNewTextView() ; 
      lastID = currentID ; 
      currentID++ ; 
     } 
} 

minusButton.setOnClickListener(new OnClickListener(){ 
@Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      removeLastTextView() ; 
      lastID-- ; 
     } 
} 

您可以通过

textView.setId(currentID); 
采取如何创建 addNewTextView()TextView帮助从 here 在解决方案,您可以修改设置标识线

若要移除removeLastTextView()的视图,您可以从here获取帮助。

+0

我尝试我们的代码,但得到错误的TextView默认值,, PLZ看到我编辑的问题 –

+0

请在'onClickListener'方法之外尝试currentID或lastID,否则按钮不能跟踪ID。 –

+0

thx fr帮助....我gt解决方案whih @sneha给了 –