Display ArrayList to TextView - java

I am trying to do something simple: display my ArrayList to a TextView. I have tried to use methods that would do this without success. Or am I supposed to use a ListView instead of a TextView?
Anyway here is the code. I hope someone can help.
public class MainActivity extends Activity {
Button aButton; // Global Scope
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
for (int i = 0; i < list.size(); i++) {
//System.out.println(list.get(i));
Log.i("Results", list.get(i));
text2.setText(text2.getText());
}
}
});
}

You are not changing text of TexView:
text2.setText(text2.getText());
You rather thought about:
text2.setText(list.get(i));
eventually:
text2.setText((text2.getText() != null ? text2.getText() : "") + list.get(i));

try this :
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
String listString = "";
for (String s : list) {
listString += s + " ";
}
text2.setText(listString);
}
});

Your prolem is here
text2.setText(text2.getText())
The working code will be:
public class MainActivity extends Activity {
Button aButton; // Global Scope
TextView text2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> list = new ArrayList<String>();
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
for (int i = 0; i < list.size(); i++) {
//System.out.println(list.get(i));
Log.i("Results", list.get(i));
text2.setText(list.get(i));
}
}
});
}

Related

access to dynamic layout

I have created dynamic layouts, but I don't know how to access their information,search on the interent and I got to this code, but it does not enter the first if
Button add, send,delete;
EditText size;
LinearLayout list, list2,list3;
ArrayList<String> text = new ArrayList<>();
Context context;
#SuppressLint("MissingInflatedId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = findViewById(R.id.add);
size = findViewById(R.id.size);
send = findViewById(R.id.send);
list =findViewById(R.id.list);
list2 =findViewById(R.id.list2);
list3 =findViewById(R.id.list3);
delete = findViewById(R.id.delete);
context = this;
//Toast.makeText(context,""+et.getText().toString(),Toast.LENGTH_SHORT).show();
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("tag","4444444");
for (int i = 0; i<4-list.getChildCount(); i++){
Log.e("tag","555555");
if (list.getChildAt(i) instanceof LinearLayoutCompat) {
Log.d("tag","333333");
LinearLayoutCompat ll = (LinearLayoutCompat) list.getChildAt(i);
for (int j = 0; j < ll.getChildCount() ; j++) {
Log.d("tag","77777");
if(ll.getChildAt(j) instanceof EditText)
{
Log.d("tag","22222222");
EditText et = (EditText) ll.getChildAt(j);
if(et.getId() == R.id.size){
Log.d("tag","1111111");
Toast.makeText(context,""+et.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
}
}
}
}
});
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
addView();
}
});
}
what I expected was that in the for it would print the values ​​that I enter in my textView through the toast

How can you make a random not repeat it self?

I'v created a list of strings which I choose randomly to display, and so I want to make it that it won't repeat its self. How can I do that.
public class qustionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
final String randomQuestions = questions[new Random().nextInt(questions.length)];
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randomQuestions = questions[new Random().nextInt(questions.length)];
theQuestion.setText(randomQuestions);
}
});
}
The reason that I have two "randomQuestions" string is because I want a question to display as soon as you get into the activity and that on the click of a button it will randomise a new string.
Instead of everytime randomizing the pick, you can randomly shuffle the questions and sequentially take the next (random) question.
private final List<String> randomQuestions;
private int questionI;
// Cycling endlessly
private String nextQuestion() {
String question = randomQuestions.get(questionI);
++questionI;
if (questionI > randomQuestions.size()) {
questionI = 0;
}
}
// Once through the list:
private String nextQuestion() {
return randomQuestions.remove(0); // Always take the first, removing it.
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
randomQuestions = Collections.shuffle(Arrays.asList(questions));
Maybe you can create a list where you put the list of existing number like :
public class qustionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
int a=new Random().nextInt(questions.length);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
ArrayList<Integer> existingNumber=new ArrayList<>();
final String randomQuestions = questions[a];
existingNumber.add(a);
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
do{
a=new Random().nextInt(questions.length);
}while(!existingNumber.add(a))
String randomQuestions = questions[a];
theQuestion.setText(randomQuestions);
}
});
}
public class qustionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
final int index = new Random().nextInt(questions.length);
final String randomQuestions = questions[index];
questions[index] = questions[questions.length-1];
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randomQuestions = questions[new Random().nextInt(questions.length-1)];
theQuestion.setText(randomQuestions);
}
});
}
Use ArrayList to store your questions and after every retrieval of a question from a random index value, remove the same from ArrayList
Below is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
metadata = getIntent().getStringExtra("metadata");
final ArrayList<String> questions = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.questions)));
int i=new Random().nextInt(questions.size());
final String randomQuestions = questions.get(i);
questions.remove(i);
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(questions.size()==0) //To make sure we don't access the empty ArrayList
{
theQuestion.setText("End of questions");
return;
}
int i=new Random().nextInt(questions.size());
String randomQuestions =questions.get(i);
questions.remove(i);
theQuestion.setText(randomQuestions);
}
});
}

How can I show the integer in listview?

I want to show the for loop numbers on the listview, but it doesn't work.
App's Layout
public class MainActivity extends AppCompatActivity {
private ListView marksixlist;
private Button mRandombtn, mCleanbtn;
private TextView mText;
private ArrayList<Integer> marksixnum = new ArrayList<Integer>();
private ArrayAdapter arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
marksixlist = (ListView) findViewById(R.id.listview1);
mRandombtn = (Button) findViewById(R.id.randombtn);
mCleanbtn = (Button) findViewById(R.id.cleanbtn);
mText = (TextView) findViewById(R.id.items);
mRandombtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int j = 1; j <= 6; j++) {
int random = (int) (Math.random()* 49+1);
marksixnum.add(random);
Collections.shuffle(marksixnum);
}
}
});
arrayAdapter = new ArrayAdapter<Integer>(this, R.layout.listitempage, marksixnum);
marksixlist.setAdapter(arrayAdapter);
Log.d("aaa", "The markssix is - " + marksixnum);
}
}
Once you generate the random numbers and shuffle them then at the end of for loop add the arrayAdapter to the listview marksixlist again and it will work as a charm.
I have additionally cleared the ArrayList marksixnum before generating random numbers so that your list doesn't keep on growing.
Its optional, if you need last click elements then delete marksixnum.clear()
mRandombtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do this if you do not want
// numbers from last click
marksixnum.clear();
for (int j = 1; j <= 6; j++) {
int random = (int) (Math.random()* 49+1);
marksixnum.add(random);
Collections.shuffle(marksixnum);
}
// add arrayAdapter to listview
marksixlist.setAdapter(arrayAdapter);
}
});

App which displays List does not work

I try to make an App with button which after the push will display all elements from list in editText.
But I have a problem, currently after I push the button I see only "second" in editText
In my opinion I need a tips from you about how to do it. Maybe I shouldn't use a loop ?
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
private ArrayList<String> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
list.add(0, "first");
list.add(1, "second");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (String abc: list){
editText.setText(abc);
}
}
});
}
}
____ edit
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
private ArrayList<String> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
list.add(0, "first");
list.add(1, "second");
list.add(2, "3");
list.add(3, "4");
list.add(4, "5");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String final_text = "";
for (String abc: list){
final_text = final_text + "\n" + abc;
}
editText.setText(final_text);
}
});
}
}
enter image description here
You are overwriting the text in editText. Accumulate the strings from the list and do a setText outside the loop:-
#Override
public void onClick(View v) {
String final_text ="";
for (String abc: list){
final_text = final_text +"\n" + abc;
}
editText.setText(final_text);
}
On this loop you will always get the last string of the list into the edittext
for (String abc: list){
editText.setText(abc);
}
You need to create multiple edittext to add multiple value from your list, or else you need to compile all value from list to one string like #Zakir has propose.

How to make 100 buttons in android studio without xml file?

I'm searching for a code that it doesn't need a long .xml and I can easily change number of buttons too 200 or everything.
public class buttons extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
}
}
in activity file add this:
// create buttons in a loop
for (int i = 0; i < 200; i++) {
Button button = new Button(this);
button.setText("Button " + i);
}
Something like that
public class Buttons extends Activity {
{
Button button;
List<Button> buttonList = new ArrayList<Button>();
LinearLayout.LayoutParams params;
LinearLayout list;
OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.long);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
list = (LinearLayout) findViewById(R.id.list);
listener = new OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
Button b = buttonList.get(id);
///
}
};
for (int i = 0; i < 200; i++)
addButton();
}
public void addButton()
{
button = new CheckBox(this);
button.setLayoutParams(params);
button.setText("TEXT");
button.setId(buttonList.size());
button.setOnClickListener(listener);
buttonList.add(button);
list.addView(button);
}
}

Categories

Resources