如何从android中的列表视图中获取整数值?

时间:2015-03-23 16:27:27

标签: java android

我创建了一个列表视图,它包含整数值。我已将它们设置在textview中,现在从另一个布局我需要获取这些整数值并将其传递给另一个活动!我怎样才能做到这一点?请帮助我成为新手。

     private void viewFunction() {

        Button messageButton=(Button)findViewById(R.id.btnCreate);
           messageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           ;
         Units units=new Units(dbHandler.getUnitsCount(),String.valueOf(myTry.getText()),String.valueOf(bulbSpn.getSelectedItem()),String.valueOf(fanSpn.getSelectedItem()));
                  //addUnits(0,myTry.getText().toString(),bulbSpn.getSelectedItem().toString(),fanS       pn.getSelectedItem().toString());
             if(!unitExsits(units)) {
              dbHandler.createUnit(units);
              Unit.add(units);
              unitsAdapter.notifyDataSetChanged();

         //   populateList();
         //   startActivity(new Intent(getApplicationContext(),HomePage.class));

        Intent intent_valueBulb = new Intent(CreateNewUnit.this,
                  HomePage.class);
            intent_valueBulb.putExtra("key2",noOfBulb);
            intent_valueBulb.putExtra("keyFan",noOfFan);
            startActivity(intent_valueBulb);




                         Toast.makeText(getApplicationContext(),String.valueOf(myTry.getText())+"has been    added to units",Toast.LENGTH_SHORT).show();
        }
            else {

              Toast.makeText(getApplicationContext(), String.valueOf(myTry.getText()) + " already exists !!! Please use a different name!", Toast.LENGTH_SHORT).show();
          }
          }
    });

}

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

        if (view == null) {
            view = getLayoutInflater().inflate(R.layout.listview_item,parent     ,false);

        }

        Units currentUnit = Unit.get(position);
        TextView name = (TextView)view.findViewById(R.id.lblLUnitName);
        name.setText(currentUnit.getName());

        TextView bulb=(TextView) view.findViewById(R.id.lblLNoofBulbs);
        bulb.setText("Number of Bulbs :"+currentUnit.getBulbNo());
        TextView fan=(TextView) view.findViewById(R.id.lblLNoOfFans);
        fan.setText("Number of Fans :"+currentUnit.getFanNo());


        return  view;
    }

1 个答案:

答案 0 :(得分:1)

如果要将列表的内容存储在变量中,可以调用getCount来确定列表的大小,然后执行for循环以将每个项目添加到ArrayList。

ArrayList<Integer> numbers = new ArrayList<Integer>();
int size = myList.getAdapter().getCount();
    for(int i = 0; i < size; i++)
    {
        numbers.add((Integer) myList.getAdapter().getItem(i));
    }

如果您尝试存储用户选择的列表项,则需要实现onItemClickListener,然后使用列表中的索引从适配器中获取所选项:

myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        //this is what happens when an item in the list is clicked
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //the location of the selected item is stored in i
            //use this location to pull the value and store it in a variable

            int selectedNumber = (Integer) adapterView.getAdapter().getItem(i);

            //you now have the chosen number stored in a variable
        }
    });