[英]How to get integer values from a list view in android?
我創建了一個列表視圖,它包含整數值。 我已經在textview中設置了它們,現在需要從另一個布局中獲取這些整數值並將其傳遞給另一個活動! 我怎樣才能做到這一點? 請幫助我是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;
}
如果要將列表的內容存儲在變量中,則可以調用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
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.