2013-02-08 121 views
0

我從數據庫填充alerttdialog。我將這些值存儲在arrayList中,將它們轉換爲charsequence列表,然後將它們設置爲我的alertdialog構建器。如圖所示:選擇選項時ArrayList與charsequence轉換問題

這是從我的數據庫我的人口稠密的「文本模板」選項截圖:

enter image description here

此刻,當我點擊我的例如Call me選項之一。它將顯示爲應該在指定的編輯文本中。如果我點擊其他選項之一,例如'給我發電子郵件',這將被忽略,只有我的第一個'如果'選項Call me。將工作,如下所示:

enter image description here

這使我相信,由於某種原因,只Call me已經添加到我的CharSequence數組,但我不知道爲什麼。這是我完整的課程。我在longOnClick方法得到這個問題。我已經標在下面的代碼這個問題領域:

package com.example.flybase2; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.AlertDialog.Builder; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.text.Editable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnLongClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class ContactsEmail extends Activity implements OnClickListener, OnLongClickListener{ 

String emailPassed; 
String emailAdd; 
String emailSub; 
String emailMess; 
EditText setEmailAddress; 
EditText setEmailSubject; 
EditText setEmailMessage; 
Button btnSendEmail; 
int i; 
CharSequence[] items; 
DBHandlerTempComms addTemp = new DBHandlerTempComms(this, null, null); 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

setContentView(R.layout.emaillayout); 



Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    emailPassed = extras.getString("passedEmailAdd"); 
} 


setEmailAddress = (EditText) findViewById (R.id.inputEmailAddress); 
setEmailAddress.setText(emailPassed); 
setEmailSubject = (EditText) findViewById (R.id.inputEmailSubject); 
setEmailMessage = (EditText) findViewById (R.id.inputEmailMessage); 



btnSendEmail = (Button)findViewById(R.id.btnSendEmail); 

btnSendEmail.setOnClickListener(this); 

setEmailMessage.setOnLongClickListener(this); 

} 


@Override 
public void onClick(View sendEmailClick) { 

    emailAdd = setEmailAddress.getText().toString(); 
    emailSub = setEmailSubject.getText().toString(); 
    emailMess = setEmailMessage.getText().toString(); 

    Intent sendEmailIntent = new Intent(Intent.ACTION_SEND); 
    sendEmailIntent.setType("message/rfc822"); 
     sendEmailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {emailAdd}); 
     sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSub); 
     sendEmailIntent.putExtra(Intent.EXTRA_TEXT, emailMess); 
     startActivity(Intent.createChooser(sendEmailIntent, "Send mail...")); 
     finish(); 

} 

*********************ISSUE AREA******************** 
     @Override 
     public boolean onLongClick(View v) { 



      addTemp.open(); 
      Cursor getTemps = addTemp.setList(); 
      addTemp.close(); 



      if (getTemps != null) { 
       String[] from = new String[getTemps.getCount()]; 
       startManagingCursor(getTemps); 
       if (getTemps.moveToFirst()) { 
        int count = 0; 
        do { 
         String userName = getTemps.getString(1); 
         from[count] = userName; 
         count++; 
        } while (getTemps.moveToNext()); 
       } 

       ArrayList<String> content = new ArrayList<String>(); 

       for (int a = 0; a < from.length; a ++) 
       { 

       content.add(from[a]);    

       }   
       items = content.toArray(new CharSequence[content.size()]); 
      } 


      Builder alertDialogBuilder = new AlertDialog.Builder(ContactsEmail.this); 

      alertDialogBuilder.setTitle("Message Templates:"); 



      alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int item) { 

        if (items[item].equals("Call me.")) { 

         setEmailMessage.setText(items[item]); 

        } 

        else if (items[item].equals("Text me.")) { 

         setEmailMessage.setText(items[item]); 


        } 

        else if (items[item].equals("Leaving the house now.")) { 


         setEmailMessage.setText(items[item]); 

        } 

        else if (items[item].equals("Leaving work now.")) { 


         setEmailMessage.setText(items[item]); 

        } 

        else if (items[item].equals("Create New Template +")) { 


         AlertDialog.Builder builder = new AlertDialog.Builder(ContactsEmail.this); 
         builder.setTitle("Type New Template:"); 


               final EditText input = new EditText(ContactsEmail.this); 

               builder.setView(input); 

               builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
               public void onClick(DialogInterface dialog, int whichButton) { 
                Editable value = input.getText(); 

                setEmailMessage.setText(value); 

                String templateValue = (String)value.toString(); 
                addTemp.open(); 
                addTemp.insertTemplate(templateValue); 
                addTemp.close(); 


                } 
               }); 

               builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int whichButton) { 

                } 
               }); 

               builder.show(); 

        } 

       } 
       }); 

      alertDialogBuilder.show(); 


      return true; 
     } 


} 
+0

爲什麼不通過在'if'語句中檢查''Create New Template +''並在'else'中放置'setEmailMessage.setText(items [item]);'來擺脫重複的代碼條款? – harpun 2013-02-08 21:28:39

回答

0

略顯尷尬,但我剛剛意識到我有我的比較,國際單項體育聯合會存儲在CharSequence的字符串不同的字符串,所以它現在的工作!