Android:如何使用 xml 引用字符串数组资源中的字符串以及如何在该 xml 中显示我的数据列表
我的项目中有一个问题:我想通过添加 string.xml 来检索我的首选项中使用的多复选框列表中的数据(机器列表),其中存储了多复选框列表数据的项目。
这是我的偏好设置。
<com.scytec.datamobile.vd.gui.android.ListPreferenceMultiSelect
android:defaultValue="#ALL#"
android:key="@string/pref_DataEntry_key"
android:title="Data Entry Machine"
android:dialogTitle="Choose Machines"
android:summary="Select a Machine"
android:entries="@array/pref_DataEntry_entries"
android:entryValues="@array/pref_DataEntry_values"/>
这是我的SettingView类:
public class SettingsView extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawableResource(R.drawable.logo_480x854);
addPreferencesFromResource( R.xml.settings_preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
这是String.xml
<string name="pref_DataEntry_key">DataEntryMachine</string>
<string-array name="pref_DataEntry_entries">
<item>All</item>
<item>Test1 Display</item>
<item>Test2 Display</item>
</string-array>
<string-array name="pref_DataEntry_values">
<item>#ALL#</item>
<item>Test1 Value</item>
<item>Test2 Value</item>
</string-array>
,这是getmachine列表代码:
public class MachineListView extends Activity implements IObserver{
private ListView machine_listview;
ArrayAdapter<String> adapter;
ArrayList<String> arrayListofMachines;
ArrayList<String> arrayListofMachineNumbers;
Vector<MDCMachineStatus> machineStatus_vector;
Handler handler;
private static int oldPosition = 0;
private Boolean firstClick = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.machinelistview);
machine_listview = (ListView) findViewById(R.id.machine_listview);
machine_listview.setFastScrollEnabled(true);
MachineStatusSingleton.Register(this);
getData();
adapter = new ArrayAdapter<String>(MachineListView.this, R.layout.listviewrow, arrayListofMachines);
machine_listview.setAdapter(adapter);
machine_listview.setSelection(oldPosition);
machine_listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
oldPosition = position;
MachineStatusSingleton.setMachineNumber(arrayListofMachineNumbers.get(position));
MachineListView.this.finish();
}
});
handler = new Handler(){
public void handleMessage(android.os.Message msg) {
machine_listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
};
};
}
public void Update(ISubject arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy()
{
super.onDestroy();
MachineStatusSingleton.Unregister(this);
}
private void getData(){
machineStatus_vector = MachineStatusSingleton.GetData();
arrayListofMachines = new ArrayList<String>();
arrayListofMachineNumbers = new ArrayList<String>();
for(MDCMachineStatus temp: machineStatus_vector){
arrayListofMachines.add(temp.toString());
arrayListofMachineNumbers.add(temp.getNumber());
}
Collections.sort(arrayListofMachines);
Collections.sort(arrayListofMachineNumbers);
}
private void updateData(){
getData();
handler.sendEmptyMessage(0);
adapter.notifyDataSetChanged();
int index = machine_listview.getFirstVisiblePosition();
View v = machine_listview.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// ...
// restore
machine_listview.setSelectionFromTop(index, top);
}
我想在我的mutiecbboxmachine列表中获取此机器列表...
I have a problem in my project: I want to retrieve data(machine list) in a multicheckbox-list used in my preferences by adding string.xml where items are stored for multicheckbox-list data.
Here is my Preferences.
<com.scytec.datamobile.vd.gui.android.ListPreferenceMultiSelect
android:defaultValue="#ALL#"
android:key="@string/pref_DataEntry_key"
android:title="Data Entry Machine"
android:dialogTitle="Choose Machines"
android:summary="Select a Machine"
android:entries="@array/pref_DataEntry_entries"
android:entryValues="@array/pref_DataEntry_values"/>
and here is my SettingView class:
public class SettingsView extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawableResource(R.drawable.logo_480x854);
addPreferencesFromResource( R.xml.settings_preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
and here is the String.xml
<string name="pref_DataEntry_key">DataEntryMachine</string>
<string-array name="pref_DataEntry_entries">
<item>All</item>
<item>Test1 Display</item>
<item>Test2 Display</item>
</string-array>
<string-array name="pref_DataEntry_values">
<item>#ALL#</item>
<item>Test1 Value</item>
<item>Test2 Value</item>
</string-array>
and this is getmachine list code:
public class MachineListView extends Activity implements IObserver{
private ListView machine_listview;
ArrayAdapter<String> adapter;
ArrayList<String> arrayListofMachines;
ArrayList<String> arrayListofMachineNumbers;
Vector<MDCMachineStatus> machineStatus_vector;
Handler handler;
private static int oldPosition = 0;
private Boolean firstClick = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.machinelistview);
machine_listview = (ListView) findViewById(R.id.machine_listview);
machine_listview.setFastScrollEnabled(true);
MachineStatusSingleton.Register(this);
getData();
adapter = new ArrayAdapter<String>(MachineListView.this, R.layout.listviewrow, arrayListofMachines);
machine_listview.setAdapter(adapter);
machine_listview.setSelection(oldPosition);
machine_listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
oldPosition = position;
MachineStatusSingleton.setMachineNumber(arrayListofMachineNumbers.get(position));
MachineListView.this.finish();
}
});
handler = new Handler(){
public void handleMessage(android.os.Message msg) {
machine_listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
};
};
}
public void Update(ISubject arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy()
{
super.onDestroy();
MachineStatusSingleton.Unregister(this);
}
private void getData(){
machineStatus_vector = MachineStatusSingleton.GetData();
arrayListofMachines = new ArrayList<String>();
arrayListofMachineNumbers = new ArrayList<String>();
for(MDCMachineStatus temp: machineStatus_vector){
arrayListofMachines.add(temp.toString());
arrayListofMachineNumbers.add(temp.getNumber());
}
Collections.sort(arrayListofMachines);
Collections.sort(arrayListofMachineNumbers);
}
private void updateData(){
getData();
handler.sendEmptyMessage(0);
adapter.notifyDataSetChanged();
int index = machine_listview.getFirstVisiblePosition();
View v = machine_listview.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// ...
// restore
machine_listview.setSelectionFromTop(index, top);
}
I want to get this machine list in my mutichecbboxmachine list...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论