2012-04-16 4 views
1

Я сделал этот код, чтобы я мог отправлять информацию на мой сайт. Только сейчас я хочу загрузить категорию из spinner, которую я пробовал:Как получить значение spinner для отправки формы

String mCategory = category.getItemAtPosition (что добавить сюда?). ToString();

Я просто не знаю, как это сделать, я надеюсь, что кто-то это сделает.

net.thinkbin; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 

public class share extends Activity { 

final Context context = this; 
private Button post; 
private EditText title, email, author, idea, submit; 
Spinner category; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.share); 

    Button view = (Button) findViewById(R.id.button1); 
    view.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("net.thinkbin.TUTORIAL1")); 
      overridePendingTransition(0, 0); 
      finish(); 
     } 
    }); 

    Button menu = (Button) findViewById(R.id.buttonhome); 
    menu.setOnClickListener(new View.OnClickListener() { 

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

      // custom dialog 
      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.custom); 

      // set the custom dialog components - text, image and button 
      TextView text = (TextView) dialog.findViewById(R.id.text); 
      text.setText("Loading..."); 
      ImageView image = (ImageView) dialog.findViewById(R.id.image); 
      image.setImageResource(R.drawable.hourglass); 

      dialog.show(); 
      Thread th = new Thread(new Runnable() { 
       public void run() { 

        startActivity(new Intent("net.thinkbin.MENU")); 
        overridePendingTransition(0, 0); 
        dialog.dismiss(); 
        finish(); 

       } 
      }); 
      th.start(); 
     } 
    }); 

    ArrayAdapter<CharSequence> adapter = ArrayAdapter 
      .createFromResource(this, R.array.spinnerfood, 
        android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    Spinner s = (Spinner) findViewById(R.id.spinner1); 
    s.setAdapter(adapter); 

    post = (Button) findViewById(R.id.post); 
    title = (EditText) findViewById(R.id.titlefield); 
    email = (EditText) findViewById(R.id.emailfield); 
    author = (EditText) findViewById(R.id.authorfield); 
    category = (Spinner) findViewById(R.id.spinner1); 

    idea = (EditText) findViewById(R.id.ideafield); 
    submit = (EditText) findViewById(R.id.submitfield); 
    post.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      String mTitle = title.getText().toString(); 
      String mEmail = email.getText().toString(); 
      String mAuthor = author.getText().toString(); 
      String mCategory = category.getItemAtPosition().toString(); 
      String mIdea = idea.getText().toString(); 
      String mSubmit = submit.getText().toString(); 

      tryPost(mTitle, mEmail, mAuthor, mCategory, mIdea, mSubmit); 
     } 

    }); 
} 



protected void tryPost(String mTitle, String mEmail, String mAuthor, 
     String mCategory, String mIdea, String mSubmit) { 
    HttpURLConnection connection; 
    OutputStreamWriter request = null; 

    URL url = null; 
    String response = null; 
    String parameters = "title=" + mTitle + "&email=" + mEmail + "&author=" 
      + mAuthor + "&category=" + mCategory + "&idea=" + mIdea 
      + "&submit=" + mSubmit; 

    try { 
     url = new URL("http://www.thinkbin.net/include/api/share.php"); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoOutput(true); 
     connection.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
     connection.setRequestMethod("POST"); 

     request = new OutputStreamWriter(connection.getOutputStream()); 
     request.write(parameters); 
     request.flush(); 
     request.close(); 
     String line = ""; 
     InputStreamReader isr = new InputStreamReader(
       connection.getInputStream()); 
     BufferedReader reader = new BufferedReader(isr); 
     StringBuilder sb = new StringBuilder(); 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     // Response from server after login process will be stored in 
     // response variable. 
     response = sb.toString(); 
     // You can perform UI operations here 
     Toast.makeText(this, "Succes! \n" + response, 0).show(); 
     isr.close(); 
     reader.close(); 
     startActivity(new Intent("net.thinkbin.MENU")); 
     overridePendingTransition(0, 0); 
     finish(); 

    } catch (IOException e) { 
     // Error 

    } 
} 
} 

ответ

1

Там куча getSelected*()methods for the spinner. Вероятно, вы хотите getSelectedItem().

+0

omg спасибо, это было очень просто –

+0

плохо выбрать его как правильный ответ за 2 минуты –

Смежные вопросы