在按钮中的列表视图中选择时获取值而不是单击

问题描述

public class ListItem extends AppCompatActivity
{
    ListView listView1;
    ArrayList<String> listproduct;
    ArrayAdapter<String > adapternew;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listitem);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setTitle("Please Select the Item");
      SearchView search=(SearchView)findViewById(R.id.Search);
        ScrollView scroll=(ScrollView)findViewById(R.id.Scroll);
        ListView listView1=(ListView)findViewById(R.id.Listview);
        listproduct=new ArrayList<>();
        listproduct.add("Stove Top Stuffing");
        listproduct.add("Campell's Soup");
        listproduct.add("Tide");
        listproduct.add("Pampers");
        listproduct.add("Pepsi Products");
        listproduct.add("Tang");
        listproduct.add("Top Ramen");
        listproduct.add("Knorr");
        listproduct.add("Palmolive");
        listproduct.add("Scotch-Brite");
        listproduct.add("Bounty Paper Towls");
        listproduct.add("Oreo Cookies");
        listproduct.add("quaker Oats");
        listproduct.add("Lays Potato Chips");
        adapternew = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listproduct);
        listView1.setAdapter(adapternew);
        SearchManager manager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
        search.setonQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                search.clearFocus();
            if(listproduct.contains(query))
            {
            adapternew.getFilter().filter(query);
            }
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                adapternew.getFilter().filter(newText);
                return false;
            }
             });
          listView1.setonItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int newposition,long id) {
                Toast.makeText(ListItem.this,listproduct.get(newposition)+"",Toast.LENGTH_SHORT).show();
                Intent i=new Intent(ListItem.this,AddOrder.class);
                i.putExtra("position",listproduct.get( newposition));

                startActivity(i);
            }
        });
    }
}

这是在按钮中选择列表视图项的代码,我希望在选择项时它会自动显示在按钮中,而不是单击

getstock.setonClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(AddOrder.this,ListItem.class);
              Intent intent = getIntent();
               Bundle bd = intent.getExtras();

                if (bd != null) {
                    String getname = (String) bd.get("position");

                    getstock.setText(getname);
                    System.out.println(getname);


                   if(getstock.getText().toString().equals("Stove Top Stuffing")) {
                    //   getstock.setText("Stove Top Stuffing");
                       taxgiven.setText("12");
                        stockgiven.setText("250");
                        rategiven.setText("598.214");
                        MRPRate.setText("999");


                       //Showqty.setText(addorder.getText().toString());
                       // ShowRate.setText(TaxInclrate.getText().toString());

                        //percentage calculation
                       double taxamount = Double.parseDouble(taxgiven.getText().toString());
                       double rateamount = Double.parseDouble(rategiven.getText().toString());
                       Double result = ((double)Math.round(rateamount* taxamount) / 100);
                       Double finalamount=(result + rateamount);
                       TaxInclrate.setText(""+finalamount);

                                          }
                   else if(getstock.getText().toString().equals("Campell's Soup"))
                   {
                       taxgiven.setText("5");
                       stockgiven.setText("100");
                       rategiven.setText("150");
                       MRPRate.setText("456");

                       //percentage calculation
                       double taxamount = Double.parseDouble(taxgiven.getText().toString());
                       double rateamount = Double.parseDouble(rategiven.getText().toString());
                       Double result = ((double)rateamount* taxamount) / 100;
                       Double finalamount=(result + rateamount);
                       TaxInclrate.setText(""+finalamount);
                   }
                 
                              }
                startActivity(i);
            }
        });

这是获取按钮中选定值的代码,我想获取按钮中的值而不是单击(如果可能,我想在列表视图中选择按钮时获取按钮中的值)

请帮忙弄清楚,提前谢谢..

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...