如何从listview中保存选择ID(How to save selection id's from listview)

编程入门 行业动态 更新时间:2024-10-25 18:29:19
如何从listview中保存选择ID(How to save selection id's from listview)

我有一个活动,其中包含一个提交按钮和一个多选列表视图,我用字符串数组填充,这个数组存在英语和法语。

strings.xml中

<string-array name="trucks"> <item name="truck1">Mini-van</item> <item name="truck2">Pick-up</item> <item name="truck3">4 x 4</item> </string-array>

strings_fr.xml

<string-array name="trucks"> <item name="truck1">Mini-van french</item> <item name="truck2">Pick-up french</item> <item name="truck3">4 x 4 french</item> </string-array>

一旦用户选择了他/她想要的项目,他们就会点击按钮来保存选择(本地到数组)。 我能够获取所选项目的字符串内容,但如果我保存那些,那么它将保存语言特定值。 我需要的是要保存的名称或某种id(truck1,truck2),以便下次加载列表时我可以实际检查已保存的列表项而不管语言,以及将车辆ID发送到服务器什么时候永久保存数据。

以下是我到目前为止的情况..

activity_truck_picker.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn_truck_done" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/themedButton" android:layout_alignParentBottom="true" android:text="Add Truck(s)" /> <ListView android:id="@+id/lst_trucks" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/btn_truck_done" android:layout_alignParentTop="true" /> </RelativeLayout>

TruckPickerActivity.java

import android.app.Activity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.util.SparseBooleanArray; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; public class TruckPickerActivity extends Activity implements View.OnClickListener { private static final String TAG = "Presite"; Button button; ListView listView; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_truck_picker); listView = (ListView) findViewById(R.id.lst_trucks); button = (Button) findViewById(R.id.btn_truck_done); String[] trucks = getResources().getStringArray(R.array.trucks); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, trucks); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(adapter); button.setOnClickListener(this); } public void onClick(View v) { SparseBooleanArray checked = listView.getCheckedItemPositions(); ArrayList<String> selectedItems = new ArrayList<String>(); for (int i = 0; i < checked.size(); i++) { int position = checked.keyAt(i); if (checked.valueAt(i)) selectedItems.add(adapter.getItem(position)); } String[] outputStrArr = new String[selectedItems.size()]; for (int i = 0; i < selectedItems.size(); i++) { outputStrArr[i] = selectedItems.get(i); Log.i(TAG, "Truck Selected: " + selectedItems.get(i)); String itemName = ...; // Some how look up name of string in array for storing in database } } }

我无法找到一种方法来获取所选选项的名称/ ID以进行保存。 任何帮助,将不胜感激。

I have an activity that contains a submit button and a multiple select listview that I populate with a string-array, this array exists in both english and french.

strings.xml

<string-array name="trucks"> <item name="truck1">Mini-van</item> <item name="truck2">Pick-up</item> <item name="truck3">4 x 4</item> </string-array>

strings_fr.xml

<string-array name="trucks"> <item name="truck1">Mini-van french</item> <item name="truck2">Pick-up french</item> <item name="truck3">4 x 4 french</item> </string-array>

Once the user selects which ever items he/she wants they hit a button to save the selections(locally to an array). I am able to get the string content of the selected items but if i save those then it would be saving the language specific values. what I need is the name or some sort of id (truck1, truck2) to save so that the next time the list is loaded I can pragmatically check off the saved list items regardless of language, as well as send the vehicle id's to the server when it comes time to permanently save the data.

Below is what I have so far..

activity_truck_picker.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn_truck_done" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/themedButton" android:layout_alignParentBottom="true" android:text="Add Truck(s)" /> <ListView android:id="@+id/lst_trucks" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/btn_truck_done" android:layout_alignParentTop="true" /> </RelativeLayout>

TruckPickerActivity.java

import android.app.Activity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.util.SparseBooleanArray; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; public class TruckPickerActivity extends Activity implements View.OnClickListener { private static final String TAG = "Presite"; Button button; ListView listView; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_truck_picker); listView = (ListView) findViewById(R.id.lst_trucks); button = (Button) findViewById(R.id.btn_truck_done); String[] trucks = getResources().getStringArray(R.array.trucks); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, trucks); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(adapter); button.setOnClickListener(this); } public void onClick(View v) { SparseBooleanArray checked = listView.getCheckedItemPositions(); ArrayList<String> selectedItems = new ArrayList<String>(); for (int i = 0; i < checked.size(); i++) { int position = checked.keyAt(i); if (checked.valueAt(i)) selectedItems.add(adapter.getItem(position)); } String[] outputStrArr = new String[selectedItems.size()]; for (int i = 0; i < selectedItems.size(); i++) { outputStrArr[i] = selectedItems.get(i); Log.i(TAG, "Truck Selected: " + selectedItems.get(i)); String itemName = ...; // Some how look up name of string in array for storing in database } } }

I cant figure out a way to get the names/id of the selected options for saving. Any help would be appreciated.

最满意答案

您可以将onItemClickListener添加到ListView ,其中long id参数是已单击的项的id:

listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Do something with id parameter } });

如果我理解正确,你想获得在xml中声明的字符串数组的name属性。 我创建了一个第二个数组,其中包含第一个数组的名称,并使用onItemClickListener()的id或position参数获取名称

You can add an onItemClickListener to your ListView, where the long id parameter is the id of the item that has been clicked:

listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Do something with id parameter } });

If I understand you correctly, you'd like to get the name attribute of your string array declared in xml. I'd create a 2nd array holding the name of your 1st array, and fetch the names using the id or position parameter from onItemClickListener()

更多推荐

本文发布于:2023-04-28 00:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ID   listview   save   id   selection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!