旋转器是空的

时间:2013-07-29 08:44:26

标签: android

我正在尝试用来自MySQL的数据填充微调器(我需要行),但它们保持空白。此外,那些旋转器应该相互依赖。有什么想法吗?当我在本地主机中调用url时,它返回正确的值,因此PHP工作正常(PHP的一行示例:[{“Grad”:“Beograd”,“Predmet”:“matematika”} ...],I我更担心将这些行放在列表中。

public class IzboraGrada extends Activity {
    Spinner spinner1, spinner2;
    private Button button,izlaz;
    static String str_grad,str_predmet,url;

    InputStream is=null;
    String result=null;
    String line=null;
    String[] grad, predmet;   


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_izbora_grada);

        spinner1=(Spinner) findViewById(R.id.spinner1);
        spinner2=(Spinner) findViewById(R.id.spinner2);

        final List<String> list1=new ArrayList<String>();
        final List<String> list2=new ArrayList<String>();

        Button b=(Button) findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                try
                {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.1.14/test/spinner1.php");
                HttpResponse response = httpclient.execute(httppost);
                Log.e("Fail 1", "3");

                HttpEntity entity = response.getEntity();
                Log.e("Fail 1", "4");

                is = entity.getContent();
                Log.e("Pass 1", "connection success ");
                }
                catch(Exception e)
                {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
                finish();
                } 

                try
                {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
                }
                catch(Exception e)
                {
                Log.e("Fail 2", e.toString());
                }    

                try
                {
                JSONArray JA=new JSONArray(result);
                JSONObject json= null;
                grad = new String[JA.length()];       
                predmet = new String[JA.length()];

                for(int i=0;i<JA.length();i++)
                {
                json=JA.getJSONObject(i);
                grad[i] = json.getString("Grad");
                predmet[i]=json.getString("Predmet");
                }
                Toast.makeText(getApplicationContext(), "sss",Toast.LENGTH_LONG).show();

                for(int i=0;i<grad.length;i++)
                {
                list1.add(grad[i]);
                list2.add(predmet[i]);
                }

                spinner_fn();

                }
                catch(Exception e)
                {

                Log.e("Fail 3", e.toString());
                //login.this.finish();

                }
            }
        });
    }

    private void spinner_fn() {
        // TODO Auto-generated method stub

        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
                                    android.R.layout.simple_spinner_item, grad);
    dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter1);



        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(),
                                    android.R.layout.simple_spinner_item, predmet);
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter2);   

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
        public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
        {
        // TODO Auto-generated method stub

        spinner2.setSelection(position);

        }

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

        });


        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
        // TODO Auto-generated method stub

        spinner1.setSelection(position);

        }

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

        }   
}

和PHP代码:

$con = mysqli_connect($host, $user, $pwd, $db);

if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 


$sql = "SELECT Grad, Predmet FROM lista";
$result = mysqli_query($con, $sql);

$rows = array();

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

        $rows[] = $row; 

}

mysqli_close($con);

echo json_encode($rows);

1 个答案:

答案 0 :(得分:2)

您没有填充您的微调器,查看您的部分代码;

ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Android将其作为一行读取,没有对正确数据的有效引用。请用以下内容替换

// adapter for spinner
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(), $$$, list1);
// set layout
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

将$$$替换为XML布局中的spinner item-id。对spinner2做同样的事情