Android:如何使用xml引用字符串数组资源中的字符串以及如何在该xml中显示我的数据列表

时间:2012-02-08 14:04:08

标签: android android-preferences android-resources

我的项目有问题:我想通过添加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);
}

我想在我的mutichecbboxmachine列表中获取此机器列表...

0 个答案:

没有答案