将选定的微调器项值设置为编辑文本视图 - Android

时间:2014-04-10 10:49:12

标签: java android spinner onitemselectedlistener

我是android新手。我在我的微调器中显示字符串,当用户选择字符串时,我想使用编辑文本视图输入选定的微调器字符串值(当用户从​​微调器中选择食物项目时我想使用编辑文本视图输入食物的值10) 。我已完成设置微调器项目,但我不知道如何输入选定的微调器值。

这是我的java文件,

public class PaymentsActivity extends Activity implements OnItemSelectedListener {

private Button btnViewExpenses;
private Spinner spinner;
private EditText selectedSpinner;
// array list for spinner adapter
private ArrayList<Category> categoriesList;
ProgressDialog pDialog;

// API urls
// Url to get all categories
private String URL_CATEGORIES = "http://10.0.2.2/category_api/get_categories.php";

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

    selectedSpinner = (EditText) findViewById(R.id.categoryItem);
    spinner = (Spinner) findViewById(R.id.categoryList);
    btnViewExpenses = (Button) findViewById(R.id.btnViewExpenses);

    categoriesList = new ArrayList<Category>();

    // spinner item select listener
    spinner.setOnItemSelectedListener(this);

    new GetCategories().execute();

}

/**
 * Adding spinner data
 * */
private void populateSpinner() {
    List<String> lables = new ArrayList<String>();

//  txtCategory.setText("");

    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(spinnerAdapter);
}

/**
 * Async task to get all categories
 * */
private class GetCategories extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(PaymentsActivity.this);
        pDialog.setMessage("Loading Expenses Information...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("categories");                        

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getInt("id"),
                                catObj.getString("category"));
                        categoriesList.add(cat);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        populateSpinner();
    }

}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    Toast.makeText(
            getApplicationContext(),
                    parent.getItemAtPosition(position).toString() + " Selected" ,
            Toast.LENGTH_LONG).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {        
}

public void onClickViewExpenses(View view){
    Intent viewExpenses = new Intent(this, ExpensesList.class);
    startActivity(viewExpenses);
}
}

这是我的xml文件,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:layout_gravity="center_horizontal">

<TextView
    android:id="@+id/accountBalanceView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/labelView"
    android:layout_marginLeft="26dp"
    android:textSize="20dp"
    android:text="Account Balance :" />

<Spinner
    android:id="@+id/categoryList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

<Button
    android:id="@+id/btnSubmitJobOpenAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/jobOpeningAmount"
    android:layout_alignRight="@+id/accBalanced"
    android:text="Submit" />

<TextView
    android:id="@+id/jobOpeningbalance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/accountBalanceView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:gravity="center_horizontal"
    android:text="Job Opening Amount"
    android:textSize="20dp" />

<TextView
    android:id="@+id/accBalanced"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/labelView"
    android:layout_below="@+id/labelView"
    android:layout_marginRight="17dp"
    android:text="10000"
    android:textColor="#FF0040"
    android:textSize="20dp" />

<EditText
    android:id="@+id/jobOpeningAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/categoryList"
    android:layout_alignLeft="@+id/accountBalanceView"
    android:layout_alignRight="@+id/accountBalanceView"
    android:layout_marginBottom="22dp"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/categoryItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/jobOpeningAmount"
    android:layout_alignRight="@+id/jobOpeningAmount"
    android:layout_below="@+id/categoryList"
    android:layout_marginTop="46dp"
    android:ems="10"
    android:inputType="number" />

<Button
    android:id="@+id/btnSubmitCategoryItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/categoryItem"
    android:layout_alignLeft="@+id/btnSubmitJobOpenAmount"
    android:text="Submit" />

<Button
    android:id="@+id/btnViewExpenses"
    android:onClick="onClickViewExpenses"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="31dp"
    android:text="View Previous Expenses" />

<TextView
    android:id="@+id/labelView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:padding="10dp"
    android:text="Enter Expenses Here"
    android:textSize="25dp"
    android:textStyle="bold" />

</RelativeLayout>
请帮我做这件事。

3 个答案:

答案 0 :(得分:2)

使用此功能。

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
    long id) {

   String myStr = spinner.getSelectedItem().toString();
   selectedSpinner.setText(myStr);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {        
}

答案 1 :(得分:1)

试试这个..

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    Toast.makeText(
            getApplicationContext(),
                    parent.getItemAtPosition(position).toString() + " Selected" ,
            Toast.LENGTH_LONG).show();

       String type = spinner.getSelectedItem().toString().trim();
       selectedSpinner.setText(type);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {        
}

答案 2 :(得分:0)

使用这个......

<强> MainActivity.java

String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY",
        "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
int[] val1 = { 0, 1, 2, 3, 4, 5, 6};


Spinner  spinner1;
TextView  textView1;

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

    textView1 = (TextView)findViewById(R.id.text1);
    spinner1 = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 =
            new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_item, text1);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}

OnItemSelectedListener onItemSelectedListener1 =
        new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String s1 = String.valueOf(val1[position]);
                textView1.setText(s1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}

        };

XML CODE

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />


</LinearLayout>
相关问题