0

I'm making application that send a String Array list to another activity and in the another activity it takes all the items and make text view for every item but i don't know how to do it.

this is my first activity code:

package com.TOF.versus; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity{ Button exitBtn; EditText namesTxt; TextView namesNumber; Button namesBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); exitBtn=(Button) findViewById(R.id.exitBtn); exitBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); namesNumber=(TextView)findViewById(R.id.namesNumber); namesTxt = (EditText) findViewById(R.id.namesTxt); namesBtn=(Button)findViewById(R.id.namesBtn); inputName(); } public void inputName(){ final ArrayList <String> names= new ArrayList<String>(); namesTxt.setSingleLine(); namesTxt.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { names.add(namesTxt.getText().toString()); namesNumber.setText(Integer.toString(names.size())); namesTxt.setText(""); namesBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent send = new Intent(v.getContext(), Names.class); send.putStringArrayListExtra("E",names); startActivity(send); } }); } return false; }});}} 

and this the second activity code that i don't know how to make the text view for :

package com.TOF.versus; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.ArrayList; public class Names extends AppCompatActivity { Button backBtn; RelativeLayout names_layout; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.names_layout); backBtn=(Button)findViewById(R.id.backBtn); backBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); names_layout=(RelativeLayout) findViewById(R.id.db2_root); TextView text=new TextView(this); Intent recv=getIntent(); ArrayList<String> str= recv.getStringArrayListExtra("E"); text.setText(str.get()); text.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); names_layout.addView(text); System.out.println(str); } } 
1
  • Why don't u use listview in Names activity? Commented Dec 23, 2016 at 13:55

1 Answer 1

2

you'll need to loop through the ArrayList you created with the list you got from the Intent.

ArrayList<String> str= recv.getStringArrayListExtra("E"); for (int i = 0; i < str.size(); i++) { TextView text = new TextView(this); text.setText(str.get(i)); text.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); names_layout.addView(text); } 

so this loops through the str ArrayList, creates a new instance of a TextView, gets the string corresponding to that position in the array, sets the text, layout params and then add it's to your layout.

Edit:

I would reccomend using a LinearLayout with an orientation assigned to it rather than a RelativeLayout for names_layout if you are adding views dynamically, unless you learn how to apply rules.

Example taken from How do I specify layout_below programmatically for a LinearLayout?

RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow); 

Edit 2:

What is the difference between LinearLayout and RelativeLayout?

This should provide enough information for you to understand

  1. What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?
  2. LinearLayout example
Sign up to request clarification or add additional context in comments.

1 Comment

what is the difference between linearLayout and RelativeLayout

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.