0

EDITTEXT から値を取得し、ボタンがクリックされたときに SPINNER でアクション (スピナー アイテムとして定義) を実行する小さなアプリを作成したい... onclick アクションは sendMessage(ビュー ビュー) です...誰かに方法を教えてもらえますかこのコードを書いてください..私はこれまでのところこれを手に入れました

public class MainActivity extends Activity implements OnItemSelectedListener{


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

      addItemsOnSpinner();

}

  public void addItemsOnSpinner() {

Spinner spinner=(Spinner)findViewById(R.id.selection_spinner);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.selection_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
  }
  public void sendMessage(View view) {
     EditText edittext=(EditText) findViewById(R.id.edit_message);
    String message= edittext.getText().toString();

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


        Spinner spinner=(Spinner)findViewById(R.id.selection_spinner);
        String select=spinner.getSelectedItem().toString();
       if(select.equals("Call")){
        System.out.println(select);
        // Build the intent(Call)
        Uri number = Uri.parse(message);
        Intent callIntent = new Intent(Intent.ACTION_CALL, number);

        // Verify it resolves
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(callIntent, 0);
        boolean isIntentSafe = activities.size() > 0;

        // Start an activity if it's safe
        if (isIntentSafe) {
            startActivity(callIntent);
        }
       }
        if(select.equalsIgnoreCase("E-Mail to")){
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, 0);
        }
        Toast.makeText(MainActivity.this, "The value selected is" + select, Toast.LENGTH_LONG).show();
        Toast.makeText(MainActivity.this, "The position is" +pos, Toast.LENGTH_LONG).show();
  }
  }
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
     @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode== 0){
                try{
                        if(resultCode==Activity.RESULT_OK){    
                            Uri contactData = data.getData();
                            Cursor c = getContentResolver().query(contactData, null, null, null, null);

                            // has result
                            if (c.moveToFirst()) {
                                String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                                Toast.makeText(MainActivity.this, "The name is" +name, Toast.LENGTH_LONG).show();
                                Toast.makeText(MainActivity.this, "The id is" + id, Toast.LENGTH_LONG).show();
                                // new query for the mail
                                Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);

                                 //has result
                                if(emailCursor.moveToFirst()) {
                                    String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                                    String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                                    Toast.makeText(MainActivity.this, "The email is" +email, Toast.LENGTH_LONG).show();
                                   Toast.makeText(MainActivity.this, "The emailtype is" +emailType, Toast.LENGTH_SHORT).show();

                                }

                                emailCursor.close();
                            }

                            c.close();
                        }

                }
                 catch(Exception e){
                 e.getCause();
                 }        
                }
                }
4

1 に答える 1

0

関数を別の関数内に配置することはできません。関数 onItemSelected は、sendMessageの外にある必要があります。

さらに、ボタンがクリックされたときに選択されたアイテムのみが必要な場合は、OnItemSelectedListener は必要ありません。

これは私がすることです(あなたのコードに基づいて):

public class MainActivity extends Activity{

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

        addItemsOnSpinner();

    }

    public void addItemsOnSpinner() {

        Spinner spinner=(Spinner)findViewById(R.id.selection_spinner);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.selection_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    public void sendMessage(View view) {
        EditText edittext=(EditText) findViewById(R.id.edit_message);
        String message= edittext.getText().toString();
            Spinner spinner=(Spinner)findViewById(R.id.selection_spinner);
        String select=spinner.getSelectedItem().toString();

        if(select.equals("Call")){
            System.out.println(select);
            // Build the intent(Call)
            Uri number = Uri.parse(message);
            Intent callIntent = new Intent(Intent.ACTION_CALL, number);

            // Verify it resolves
            PackageManager packageManager = getPackageManager();
            List<ResolveInfo> activities = packageManager.queryIntentActivities(callIntent, 0);
            boolean isIntentSafe = activities.size() > 0;

            // Start an activity if it's safe
            if (isIntentSafe) {
                startActivity(callIntent);
            }
        }
        if(select.equalsIgnoreCase("E-Mail to")){
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, 0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode== 0){
            try{
                if(resultCode==Activity.RESULT_OK){    
                    Uri contactData = data.getData();
                    Cursor c = getContentResolver().query(contactData, null, null, null, null);

                    // has result
                    if (c.moveToFirst()) {
                        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                        Toast.makeText(MainActivity.this, "The name is" +name, Toast.LENGTH_LONG).show();
                        Toast.makeText(MainActivity.this, "The id is" + id, Toast.LENGTH_LONG).show();
                        // new query for the mail
                        Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);

                        //has result
                        if(emailCursor.moveToFirst()) {
                            String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                            Toast.makeText(MainActivity.this, "The email is" +email, Toast.LENGTH_LONG).show();
                            Toast.makeText(MainActivity.this, "The emailtype is" +emailType, Toast.LENGTH_SHORT).show();

                        }

                        emailCursor.close();
                    }

                    c.close();
                }

            }
            catch(Exception e){
                e.getCause();
            }        
        }
    }
}

Spinner と Button の部分を修正しましたが、テストしていないので、アクティビティの変更などすべてが間違っている可能性があります。

于 2013-06-07T10:26:08.967 に答える