0

I have created a list view and it contains integer values. I have set them inside a textview and now from another layout I need to get those integer values and pass it to another activity! How can I do that? Please help I'm new to android .

     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;
    }
7
  • 2
    post the relevant code Commented Mar 23, 2015 at 16:28
  • I have so many other codes as well . These are the relavent codes to that. Commented Mar 23, 2015 at 16:37
  • Actually u need to explain in what scenario you want to retrieve the value from the listview.... Commented Mar 23, 2015 at 16:40
  • I have added some number of bulbs and fans for a listview! What i need to do is take that number from the listview and pass it to another activity. Simply what I need is to retrive the number I have given for the text views Commented Mar 23, 2015 at 16:43
  • If you are using a ListFragment, there is a method called onListItemClick where you can get the TextView which contains your data. Commented Mar 23, 2015 at 16:46

1 Answer 1

1

If you want to store the contents of the list in a variable, you can call getCount to determine the size of the list, and then do a for loop to add each item to an 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));
    }

If you're trying to store the list item selected by the user, you need to implement an onItemClickListener, and then grab the selected item from the adapter using its index in the list:

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
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.