2016-05-10 6 views
0

ここでアンドロイドアプリを開発しています。EditTextと、CallRemoveのようなボタンをrow.xmlファイルから動的に生成しています。 LinearLayoutにこれらのコントロールを追加し、私が好きな私には実装されますが、私の問題は、私はxmlファイルにEditText値を保存したいということです。次回このアプリケーションを開くときに、特定の値がxmlファイルから読み込まれます。LinearLayoutの値をxmlファイルに保存できません

enter image description here

これらの値は動的にLinearLayoutに追加されたテキストボックスに名前と携帯電話番号を追加し、"Add"ボタンをクリックします。 [追加]をクリックすると、この値がxmlファイルに保存され、次回にアプリを起動するたびにxmlの値がLinearLayoutに読み込まれます。

これは、私はあなたがアンドロイドでSharedPreferencesを参照する必要があると思う私のコード

public class MainActivity : Activity 
    { 
     int count = 1; 
     EditText textIn, txtPhoneNo; 
     Button buttonAdd; 
     LinearLayout container; 
     EditText textOut; 
     System.Collections.ArrayList arrList = new System.Collections.ArrayList(); 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      SetContentView(Resource.Layout.Main); 

      textIn = (EditText)FindViewById(Resource.Id.textin); 
      txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo); 
      buttonAdd = (Button)FindViewById(Resource.Id.add); 
      container = (LinearLayout)FindViewById(Resource.Id.container); 
     } 

     private void buttonAdd_Click(object sender, EventArgs e) 
     { 
      LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater; 
      View addView = layoutInflater.Inflate(Resource.Layout.row, null); 
      textOut = (EditText)addView.FindViewById(Resource.Id.textout); 
      arrList.Add(txtPhoneNo.Text); 
      if (textIn.Text != "" && txtPhoneNo.Text != "") 
      { 
       textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal); 
       container.AddView(addView); 
       Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall); 
       btnCall.Click += BtnCall_Click; 
       Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove); 
       buttonRemove.Click += ButtonRemove_Click; 
      } 
      else 
      { 
       Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show(); 
      } 
     } 

     private void BtnCall_Click(object sender, EventArgs e) 
     { 
      var callDialog = new AlertDialog.Builder(this); 
      string strNo = After(textOut.Text,":"); 
      callDialog.SetMessage("Call " + strNo + "?"); 
      callDialog.SetNeutralButton("Call", delegate 
      { 
       var callIntent = new Intent(Intent.ActionCall); 
       callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo)); 
       StartActivity(callIntent); 
      }); 
      callDialog.SetNegativeButton("Cancel", delegate { }); 

      // Show the alert dialog to the user and wait for response. 
      callDialog.Show(); 
     } 
} 
} 

Main.axml 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 
    <EditText 
     android:id="@+id/textin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:hint="name" /> 
    <EditText 
     android:id="@+id/txtPhoneNo" 
     android:layout_width="345.0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:hint="Phone No." /> 
    <Button 
     android:id="@+id/add" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Add" /> 
    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" /> 
</LinearLayout> 
row.xml 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:id="@+id/remove" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Remove"/> 
    <Button 
     android:id="@+id/btnCall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/remove" 
     android:text="Call"/> 
    <EditText 
     android:id="@+id/textout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/remove"/> 
</RelativeLayout> 

答えて

1

んが、あなたは

することはできませんあなたのxmlレイアウトにコントロールを動的に保存することはできません。両方の方法でxml

<include 
    android:layout="id of your layout" 
//height ... width 
/> 

を どちらかあなたはxmlに手動で追加するか、別のxml制御のために作成することができ、その後includeそれxmlあなたの親に、あなたはxmlを作成する必要があります。

我々はxmlを作成し、私たちのjavaクラスでそれを膨らませています。あなたがしたいことは、それとは全く逆です。

更新

使用共有好み

SharedPreference shared= context.getSharedPreferences("your preference name", 
       Context.MODE_PRIVATE); 

String value = shared.getString("UserPhone","no data"); 

//set this value to your control 
+0

私はコントロールを動的に格納していますが、その値を格納したいだけです。 – Pritish

+0

それを共有設定に保存し、ダイナミックビューを作成するときに共有設定から古い値を設定します。 – KDeogharkar

+0

ここでは、共有プリファレンスフォルダedit.PutString( "UserName"、textIn.Text.Trim())に保存しています。 edit.PutString( "UserPhone"、txtPhoneNo.Text.Trim()); 編集。適用();このコードでは、どのように私はそれを次回にアプリを再び開くときにそれを得ることができます – Pritish

0

です。これはあなたの目的に役立ちます。

+0

あなたが任意のサンプルを持っています.... – Pritish

+0

http://www.101apps.co.za/index.php/articles/使用-アンドロイド-sharedpreferences-に保存-Data.HTMLに...例 – Sush

+0

私はListオブジェクトのようにSharedPreferencesで複数の値を保存することはできません – Pritish

関連する問題