Java/Android 更改由对象 ArrayList 填充的 ListView 中的数据

标签 java android listview arraylist

我有一个似乎无法解决的问题。我有一个 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?
}

这里是我的 fragment ,应该可以操作这一切

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
    }
}

最佳答案

我是安卓新手。但我会尝试在几乎类似的情况下我会做的事情。 首先,我不知道您在示例中谈论的 listview 。我可以看到您使用 textview,我怀疑默认情况下它是否可点击。我已经使用了像这样的android ListView ,它可以在每次点击时更新:

  <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);

这就是我更新listview的方法(longClick示例,您可以使用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 可以提供什么。

希望它没有让你感到困惑。上面的示例来 self 出于学习目的而实现的实际代码,因此它可能不是最佳解决方案。

关于Java/Android 更改由对象 ArrayList 填充的 ListView 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747191/

相关文章:

java - 如何在 ListView 中显示 arraylist 中的所有解析数据?

android - setText 不适用于从 ArrayListAdapter 获取的 View

java - mockito 中的 anyString() 抛出参数匹配器的无效使用

安卓小部件。强制停止后如何恢复进程?

Android:如何动态地在屏幕的一部分上制作 Canvas ?

java - ArrayList,只打印值

android - 为什么在底层游标上调用重新查询后,Android listview 有时会消失?

java - 接口(interface)与抽象类

java - 从对象中检索类名/实例

java - Java 中泛型的策略模式