从微调器获取所选项目

时间:2011-09-07 06:43:21

标签: android

我已按照其他人的说法here进行了说明。但是,这似乎是我的编码问题。最初,我的微调器应该在我第一次到达此活动时显示列表中的第一个项目,但不是它只是空的。幸运的是,当我点击微调器进行选择时,列表仍然存在。而且,在我做出选择之后,吐司并没有出现。在我做出选择之后,我不确定这是否是它的原因,因为它在DDMS中弹出:

09-07 06:30:06.470: WARN/InputManagerService(50): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43cbe3b0

以下是此活动的编码:

package android.wps;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class locationFinder extends Activity {

WifiPositioningServices wifiPositioningServices = new WifiPositioningServices();

ArrayList<Profile> profileList = new ArrayList<Profile>();
ArrayList<String> profileNames = new ArrayList<String>();
ArrayList<String> mapNames = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locationfinder);

    String[] temp = wifiPositioningServices.GetAllProfileData();

    final Spinner profiles = (Spinner)findViewById(R.id.profileList);

    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line,
            profileNames);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    profiles.setAdapter(spinnerArrayAdapter);

    profiles.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long arg3) {
            // TODO Auto-generated method stub
            String test = parent.getItemAtPosition(pos).toString();
            Toast toast = Toast.makeText(parent.getContext(), test, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    //store all profile data into a ArrayList of Profile objects
    for(int i=0; i<temp.length; i++){
        String[] result = temp[i].split(",");
        Profile profile = new Profile();
        profile.setProfileName(result[0]);
        profile.setOwner(result[1]);
        profile.setMap(result[2]);
        profile.setVisible(result[3]);
        boolean visible = Boolean.valueOf(result[3]);
        if(visible){
            //if visible is true, then add profile name and mac filters into arraylist 
            profileNames.add(result[0]);
            for(int j=4; j<result.length; j++){
                profile.setMacFiltersList(result[j]);
            }
        }
        profileList.add(profile);
    }
}

}

locationfiner.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/txtPofile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile:"
android:textStyle="bold"
android:layout_x="10px"
android:layout_y="12px"
>
</TextView>
<Spinner
android:id="@+id/profileList"
android:layout_width="245px"
android:layout_height="36px"
android:layout_x="70px"
android:layout_y="12px"
>
</Spinner>
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_x="0px"
android:layout_y="32px"
/>
</AbsoluteLayout>

1 个答案:

答案 0 :(得分:0)

奇怪。在我将整个for循环的段落移到顶部之后,在微调器的初始化代码之前,现在一切正常。在选择之前/之后,项目显示在微调器中,并且在选择之后确实出现了吐司。

我指的是for循环

 for(int i=0; i<temp.length; i++){
    String[] result = temp[i].split(",");
    Profile profile = new Profile();
    profile.setProfileName(result[0]);
    profile.setOwner(result[1]);
    profile.setMap(result[2]);
    profile.setVisible(result[3]);
    boolean visible = Boolean.valueOf(result[3]);
    if(visible){
        //if visible is true, then add profile name and mac filters into arraylist 
        profileNames.add(result[0]);
        for(int j=4; j<result.length; j++){
            profile.setMacFiltersList(result[j]);
        }
    }
    profileList.add(profile);
}