-3

コードに 4 つの配列があり、ユーザーが edittext に何かを書き込むたびに、その文字列を配列の 1 つに格納したいので、toCharArray メソッドを使用しようとしましたが、文字列を配置する配列を定義する方法がわかりません入れられる:S

String [] array7 = {"Hey","Was Up","Yeahh"};
    TextView txtV1,txtV2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layouttry);
        txtV1=(TextView)findViewById(R.id.textView1);
        txtV2=(TextView)findViewById(R.id.textView2);



        Bundle extras = getIntent().getExtras();
        String value = extras.getString("Key");  // this value I want to add to the stringarray
4

2 に答える 2

2

新しい要素を追加する必要がある場合は、配列を ArrayLists に置き換えることをお勧めします。これにより、addメソッドを使用して新しい要素を挿入できるようになります。この例:

ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Text here");
于 2013-04-07T18:31:17.367 に答える
0

あなたのコードでは、文字列に 1 つの配列しか表示されないため、実際に何が必要なのかわかりません。でも頑張ります。

String 配列は、3 つのセルのみを含むようにハードコードされており、すべてがいっぱいです。これらの場所のいずれかに文字列を配置する場合は、次のようにします。

array7[0] = value; //or:
array7[1] = value; //or:
array7[1] = value;

既存の値を削除せずに配列に追加valueする場合は、次のようにすることができます。

//Create a new array, larger than the original.
String[] newArray7 = new String[array7.length + 1 /*1 is the minimum you are going to need, but it is better to add more. Two times the current length would be a good idea*/];

//Copy the contents of the old array into the new one.
for (int i = 0; i < array7.length; i++){
   newArray7[i] = array7[i];
}

//Set the old array's name to point to the new array object.
array7 = newArray7;

これは別の方法で行うことができるため、配列のサイズを変更する必要があるときはいつでも使用できます。クラス ArrayList および Vector はすでにこのメカニズムを実装していることを知っておく必要があります。必要なだけ実装できarrayList.add(string)ます。

于 2013-04-07T19:50:55.503 に答える