简体   繁体   English

选择选项时将ArrayList转换为字符序列的问题

[英]ArrayList to charsequence conversion issue when selecting an option

I populate an alerttdialog from a database. 我从数据库中填充了一个Alerttdialog。 I store these values in an arrayList , convert them to an charsequence list then set them to my alertdialog builder. 我将这些值存储在arrayList ,将它们转换为字符序列列表,然后将其设置为我的alertdialog构建器。 As shown: 如图所示:

This is a screenshot of my populated 'text template' options from my database: 这是数据库中填充的“文本模板”选项的屏幕截图:

在此处输入图片说明

At the moment when I click one of my options for example Call me . 目前,当我单击一个选项时,例如“ Call me it displays as it should within a specified edittext. 它在指定的edittext中应显示的样子。 If I click on one of the other options such as 'Email me' this is ignored, only my first 'if' option Call me . 如果我单击其他选项之一,例如“给我发电子邮件”,则将被忽略,只有我的第一个“如果”选项Call me will work as shown: 将如下所示工作:

在此处输入图片说明

This leads me to believe for some reason only Call me has been added to my charsequence array but I'm not sure why. 这使我相信由于某种原因,“ Call me已添加到我的字符序列数组中,但是我不确定为什么。 Here is my complete class. 这是我的完整课程。 I am getting this issue at the longOnClick method. 我在longOnClick方法上遇到此问题。 I have marked this issue area on the code below: 我已在以下代码中标记了此问题区域:

 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;
        }


}

有点尴尬,但我刚刚意识到我将IF与字符序列中存储的字符串进行比较时使用了不同的字符串,因此它现在可以工作了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM