【发布时间】:2013-12-23 16:15:45
【问题描述】:
我有一个似乎无法解决的问题。我有一个ListView,这个ListView 包含一个对象(IndividualRow)。这个 IndividualRow 有 3 个值,它们显示在 textviews 和一个开关中。这些IndividualRows 存储在ArrayList 中,但后来我卡住了。
我想按下TextViews(显示温度)的1,当我按下它时,它应该自动加1。
现在的问题是我不知道如何实现它。我尝试了几件事,但我无法弄清楚。我不是特别想要代码,但我想知道我应该做什么,所以我不会复制过去,也不会从中学习。这是我目前的代码。
(我有一个自定义适配器,以便 ListView 相应地显示我的行,如果需要,我可以在此处包含它)
list_item.xml(这是一行的样子)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="14dp"
android:paddingBottom="14dp" >
<Switch
android:id="@+id/switch1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="30dp"
android:textSize="22sp"
android:textOff="@string/Off"
android:textOn="@string/On"
android:paddingLeft="30dp" />
<TextView
android:id="@+id/tijd"
android:textSize="22sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:clickable="true"/>
<TextView
android:id="@+id/temperatuur"
android:textSize="22sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:clickable="true"/>
<TextView
android:layout_width="wrap_content"
android:textSize="22sp"
android:layout_height="wrap_content"
android:text="@string/GradenCelcius"
android:paddingRight="10dp" />
</LinearLayout>
这是 IndividualRow 代码
//holds values for each individual row
public class IndividualRow {
//instance variables
private String tijd;
private int temperatuur;
private boolean onoroff2;
//constructor > what an individual row needs to be an individual row
public IndividualRow(String data1,int data2, boolean onoroff){
tijd = data1;
temperatuur = data2;
onoroff2 = onoroff;
}
//sends value of tijd
public String getString1(){
return tijd;
}
//sends value of temperatuur
public String getString2(){
String x = Integer.toString(temperatuur);
return x;
}
//sends value of switch state, so on or off (on = true, off = false)
public boolean getBoolean() {
return onoroff2;
}
//probably should add setString1 and so on here right?
}
比这里是我应该操作它的片段
public class SettingTimeFragment extends Fragment{
//Instance Variables
private List<IndividualRow> items = new ArrayList<IndividualRow>();
private CustomAdapter adapter;
private ListView rowListView;
private String tijd = "0:00";
private int temperatuur = 15;
private boolean onoroff = true;
//creates the SettingTimeFragment
public static final SettingTimeFragment newInstance(){
SettingTimeFragment f = new SettingTimeFragment();
return f;
}
//providing it's layout
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.setting_time_fragment, container, false);
}
//creates the content of SettingTimeFragment
@Override
public void onActivityCreated(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onActivityCreated(savedInstanceState);
items.add(new IndividualRow(tijd, temperatuur, onoroff));// add initial row
adapter = new CustomAdapter(getActivity().getApplicationContext(),R.layout.list_item, items); // create new CustomAdapter
rowListView=(ListView)getActivity().findViewById(R.id.rowListView); // find rowListView
rowListView.setAdapter(adapter); //attach adapter to rowListView
}
//creates menu on top of the screen
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.setting_time_fragment_menu, menu);
}
//finds each individual button in menu and handles click events
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.additionbutton:
addRow(new IndividualRow(tijd, temperatuur, onoroff));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//adds a new IndividualRow
public void addRow(IndividualRow newRow) {
items.add(newRow);
adapter.notifyDataSetChanged(); //updates adapter that there is new information to display
}
}
【问题讨论】:
标签: java android listview arraylist