如何在我的java文件中实现微调器?

时间:2014-01-22 14:42:53

标签: java android database spinner

我只是一个初学者所以我有一个特定的问题,关于我如何在我的联系人应用程序的java文件中实现微调器。我已经看过一个关于联系人应用程序的教程,我想继续研究它。所以我创建了一个包含3个项目的字符串数组。现在我的问题是我如何将其实现到我的New Contact java文件的代码中(下面的代码)。它应该是布局中的下拉菜单。 我希望有人可以帮助我。询问您是否需要更多信息。

 public class NewContact extends Activity{

    Spinner typ;
    EditText firstName;
    EditText lastName;
    EditText phoneNumber;
    EditText emailAddress;
    EditText address;
    EditText preference;
    EditText information;

    DBTools dbtools = new DBTools(this);

    @Override
    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_contact);

        typ = (EditText) findViewById(R.id.typ);
        firstName = (EditText) findViewById(R.id.firstName);
        lastName = (EditText) findViewById(R.id.lastName);
        phoneNumber = (EditText) findViewById(R.id.phoneNumber);
        emailAddress = (EditText) findViewById(R.id.emailAddress);
        address = (EditText) findViewById(R.id.address);
        preference = (EditText) findViewById(R.id.preference);
        information = (EditText) findViewById(R.id.information);

    }

    public void AddNewContact(View view){

        HashMap<String, String> queryValuesMap = new HashMap<String, String>();

        queryValuesMap.put("typ", typ.getText().toString());
        queryValuesMap.put("firstName", firstName.getText().toString());
        queryValuesMap.put("lastName", lastName.getText().toString());
        queryValuesMap.put("phoneNumber", phoneNumber.getText().toString());
        queryValuesMap.put("emailAddress", emailAddress.getText().toString());
        queryValuesMap.put("address", address.getText().toString());
        queryValuesMap.put("preference", preference.getText().toString());
        queryValuesMap.put("information", information.getText().toString());

        dbtools.insertContact(queryValuesMap);

        this.callMainActivity(view);

    }

    public void callMainActivity(View view){

        Intent theIntent = new Intent(getApplicationContext(),MainActivity.class);
        startActivity(theIntent);
        finish();
        startActivity(getIntent());
    }
}

2 个答案:

答案 0 :(得分:1)

假设你有一个字符串数组

<string-array name="country_arrays">
        <item>Malaysia</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>New Zealand</item>
        <item>India</item>
    </string-array>

和你的layuot中的一个微调器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
你的java代码中的

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

String[] country_array = getResources().getStringArray(R.array.country_arrays)
    ArrayAdapter dataAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, country_array );
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter);

答案 1 :(得分:0)

好的,非常感谢!但现在我只有两个小问题: 1.如何更改Spinner中Items的TextColor(我有3个不同的.java文件) 2.在适配器和Spinner代码必须实现的位置,我在MainActiviy列表中获得与NewContact布局中添加的相同的选择。因为当我添加它,它将显示在主屏幕列表...如果我点击一个联系人我得到editContact布局,并在那里我想要看到旋转器的选择项目。

`public class MainActivity扩展了ListActivity {

Intent intent;
TextView KontaktID;

DBTools dbtools = new DBTools(this);

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> KontaktList = dbtools.getAllKontakte();

if(KontaktList.size() !=0){

    ListView listView = getListView();
    listView.setOnItemClickListener(new OnItemClickListener(){

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

            KontaktID = (TextView) view.findViewById(R.id.KontaktID);

            String KontaktIDValue = KontaktID.getText().toString();
            Intent theIntent = new Intent(getApplication(), EditContact.class);
            theIntent.putExtra("KontaktID", KontaktIDValue);
            startActivity(theIntent);
            }
    });


ListAdapter adapter = new SimpleAdapter(MainActivity.this, KontaktList, R.layout.contact_entry,
        new String[] {"KontaktID", "typ", "lastName", "firstName"},
        new int[]{R.id.KontaktID, R.id.typ, R.id.lastName, R.id.firstName});

setListAdapter(adapter);}}

public void OnItemClick (AdapterView<?> parent, View view, int position,
        long id){
Spinner typ;    
typ  = (Spinner) findViewById(R.id.typ);
String[] diff = getResources().getStringArray(R.array.diff);
ArrayAdapter <String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, diff);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typ.setAdapter(dataAdapter);
 }



public void showAddContact(View view){

    Intent theIntent = new Intent(getApplicationContext(), NewContact.class);

    startActivity(theIntent);

}

}`