2014-09-02 5 views
0

은 editText를 사용하여 주요 활동을 생성했으며 버튼을 클릭하면 두 번째 활동에서 TextView가 만들어지고 텍스트가 전달됩니다. 이제 첫 번째 활동으로 돌아가서 새로운 데이터를 제공하여 새 데이터가 이전 데이터와 함께 표시되도록해야합니다. 그러나 제 경우에는 추가하는 대신 새 데이터가있는 새 페이지가 이전 데이터와 겹쳐서 표시됩니다. 초보자로서 나는 최고 수준을 시도했지만 어떤 해결책도 찾을 수 없습니다. 도움이 필요 ... 안드로이드 - 동적 텍스트 뷰 생성 활동

은 여기 내 주요 활동

public class MainActivity extends Activity 
{ 

EditText txt1; 
String value1; 
Button button_1; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    txt1=(EditText)findViewById(R.id.editText1); 
    value1=txt1.getText().toString(); 

    button_1=(Button)findViewById(R.id.button1); 
    button_1.setOnClickListener(onClick()); 


} 

private OnClickListener onClick() 
{ 
    // TODO Auto-generated method stub 
    return new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      Intent i=new Intent(getApplicationContext(),Activity2.class); 
      i.putExtra("v",txt1.getText().toString()); 
      startActivity(i); 

     } 
    }; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) 
    { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

내 두 번째 활동

public class Activity2 extends MainActivity 
{ 
LinearLayout ll; 
TextView txtview; 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity2); 

    ll=(LinearLayout)findViewById(R.id.linear); 

    txtview=new TextView(this); 
    txtview.setText(getIntent().getExtras().getString("v")); 



    //txtview.setText(String.valueOf(getIntent().getExtras().getString("1v"))); 
    ll.addView(createNewTextView(txtview.getText().toString())); 

    Button exit=(Button)findViewById(R.id.button1); 
    exit.setOnClickListener(new Button.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      finish(); 
     } 
    }); 
    Button New=(Button)findViewById(R.id.button2); 
    New.setOnClickListener(new Button.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 

      Intent i=new Intent(getApplicationContext(),MainActivity.class); 
      startActivity(i); 
     } 
    }); 
} 
private View createNewTextView(String text) 
{ 
    // TODO Auto-generated method stub 
    final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    final TextView txtview=new TextView(this); 
    txtview.setLayoutParams(lp); 
    txtview.setText("New text: "+text); 
    return txtview; 
} 
} 

activity_main.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="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" 
tools:context="com.example.self.MainActivity" > 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:ems="10" 
    android:inputType="text" 
    android:text="@string/e" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/editText1" 
    android:layout_alignRight="@+id/editText1" 
    android:layout_below="@+id/editText1" 
    android:text="@string/app_name" /> 

activity2.xml 파일입니다

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/linear" 
    android:layout_width="match_parent" 
    android:layout_height="418dp" 
    android:orientation="vertical" > 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/hl" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="154dp" 
     android:layout_height="wrap_content" 
     android:text="@string/New" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/exit" /> 

</LinearLayout> 

의 AndroidManifest.xml은

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.studentmarklist" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="16" 
    android:targetSdkVersion="16" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".act2"></activity> 
</application> 

+0

뜻은 .. 먼저 활동 1에 "data1"을 입력하고 활동 2로 이동하십시오. "data1"이 표시되어야합니다. 다시 누를거야. 활동 2에 "data 2"를 입력하고 활동 2로 이동하십시오. 이제 "data 1 data 2"가 표시되어야합니까? –

+0

먼저 활동 1에 "data1"을 입력하고 활동 2로 이동하십시오."data1"이 표시되어야합니다. 뒤로 누르십시오. 활동 1에 "data 2"를 입력하고 활동 2로 이동하면 이제 "data1 and data2"가 표시됩니다. – Subi

+0

매번 활동이 만들어지기 때문에 어렵습니다. 한 가지를 할 수 있습니다. 매번 Shared Preference에 쉼표로 구분 된 문자열을 입력하십시오. 그리고 매번 textviews에 루프로 표시하십시오. –

답변

0

당신은 안드로이드에 life-cycle of activities에 보일 것이다 파일. 대부분 활동은 파괴되지 않고 다시 사용됩니다. 너무 2.

저장 데이터를이 활동에

@Override 
public void OnBackPressed(){ 
super.OnBackPressed(); 
finisch(); 
} 

것처럼 Finisch link stackoverflow

0

당신은 활동이 시작되면 이전 데이터를 각 시간을 유지하려면, 당신이 공유 사용할 수있는 기본 설정을 onbackpressed 함수를 재정의 강제 setText를 사용하는 동안 공유 환경 설정에서 다음 시간을 표시하기 위해 onCreate에서 검색하십시오.

은 코드 아래 참조 :이 도움이

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

    sp = getSharedPreferences("textPref", Context.MODE_PRIVATE); 
    if(sp.contains("text")) 
     newText = sp.getString("text", "").concat(" and ").concat(getIntent().getExtras().getString("v")); 
    else 
     newText = getIntent().getExtras().getString("v"); 


    ll=(LinearLayout)findViewById(R.id.ll); 
    ll.addView(createNewTextView(newText)); 
} 

private View createNewTextView(String text) 
{ 
    // TODO Auto-generated method stub 
    final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    final TextView txtview=new TextView(this); 
    txtview.setLayoutParams(lp); 
    txtview.setText("New text: "+text); 

    Editor editor = sp.edit(); 
    editor.putString("text", text); 
    editor.commit(); 

    return txtview; 
} 

희망을. 영업 이익의 설명에 따라

편집 :

때마다 새로운 텍스트 뷰를 표시하려면, 쉼표 분리와 공유 환경 설정에서 각 값을 저장하려고합니다.

배열로 배열하십시오. 따라서 텍스트 배열을 표시 할 수 있습니다. 그런 다음 새로운 TextView를 루프로 표시하고 LinearLayout에 TextView를 추가하십시오.

발췌문 :

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

    sp = getSharedPreferences("textPref", Context.MODE_PRIVATE); 
    if(sp.contains("text")) 
     newText = sp.getString("text", "").concat(" and ").concat(getIntent().getExtras().getString("v")); 
    else 
     newText = getIntent().getExtras().getString("v"); 


    ll=(LinearLayout)findViewById(R.id.ll); 
    createNewTextView(newText); 
} 

private void createNewTextView(String text) 
{ 
    // TODO Auto-generated method stub 
    Editor editor = sp.edit(); 
    editor.putString("text", text); 
    editor.commit(); 

    String[] texts = text.split(","); 

    for (int i =0; i < texts.length; i++){ 
     final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     final TextView txtview=new TextView(this); 
     txtview.setLayoutParams(lp); 
     txtview.setText("New text: "+texts[i]); 
     ll.addView(txtview); 
    } 
} 

희망이 분명하다.

+0

코드가 값을 연결하는 데 도움이됩니다. 고맙습니다. 하지만 나는 이러한 답변을 별도의 텍스트보기에 표시하고자합니다. 별도의 텍스트보기에서 "data1", 다른 별도의 텍스트보기에서 "data2" – Subi

+0

@Subi 그런 다음 매번 새로운 TextView를 만들어야합니다. :) –

+0

나는 초보자로서 노력했지만 그게 나를 위해 복잡했다. 그것에 대해 샘플을 주시겠습니까 ???? – Subi