2013-12-23 74 views
0

我有一个问题,我似乎无法解决。我有一个ListView,这个ListView包含一个对象(IndividualRow)。这IndividualRow有3个值显示在textviews和交换机中。这些IndividualRows存储在ArrayList,但后来我卡住了。Java/Android更改由对象ArrayList填充的ListView中的数据

我想按在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 
    } 
} 

回答

2

我是Android的新手。但我会尝试我为了几乎类似的情况所做的/做的。 首先,我不知道您在example中讨论的listview。我可以看到你使用textview,我怀疑这是否可以默认点击。我用这样的机器人列表视图,可以在每一次点击更新:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/item_list" /> 

这是我如何创建适配器:

final ArrayAdapter<ContentData> adapter = new ArrayAdapter<ContentData>(this, android.R.layout.simple_list_item_1, values); 

,这是我怎么会更新listviewlongClick例如,你可以使用OnClick):

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> myAdapter, View myView, int position, long id) { 
      //update your list and perform your actions      
       //notify your adapter about the changes if required 
      return true; 
     } 
    }); 

你可能会需要在如何使用values.get()研究在上面的方法。基本上关于positionid在上述方法中代表什么以及adapterview能提供什么。

我希望它不会混淆你。以上示例来自实际用于学习目的的代码,因此它可能不是最佳解决方案。