1

Here list is an array list. whats happen is the text gets over written and always displays the last value. the size of list is 4.

for (int i = 0; i < list.size(); i ++){
  TextView name = (TextView) convertView.findViewById(R.id.user_names);
  name.setText(list.get(i).toString());
  TextView email = (TextView) convertView.findViewById(R.id.user_emails);
  email.setText(list.get(i).toString());
}
5
  • 1
    It's doing that because you're pretty much asking it to... Commented Jul 14, 2016 at 15:50
  • 1
    You should be using ListView or Recyclerview. and also you shouldn't use TextView name = (TextView) convertView.findViewById(R.id.user_names); this in a loop. Commented Jul 14, 2016 at 15:51
  • 1
    wow.. 3 people answer almost at the same time.. :v Commented Jul 14, 2016 at 15:54
  • @siddheshdighe i am using a listview. any help on how can i add text in listviews one by one Commented Jul 14, 2016 at 16:10
  • @ManishGupta check out my answer Commented Jul 14, 2016 at 16:32

5 Answers 5

3

Try to use this solution for example:

private int[] food_list_dates = {
        R.id.date_1,
        R.id.date_2,
        R.id.date_3,
        R.id.date_4,
        R.id.date_5,
        R.id.date_6,
        R.id.date_7,
        R.id.date_8,
        R.id.date_9,
        R.id.date_10,
        R.id.date_11,
        R.id.date_12,
        R.id.date_13,
        R.id.date_14,
};

...

for (int i = 0; i > foodList.size(); i++) {
    ((TextView) findViewById(food_list_dates[i])).setText( i + "" );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just append everything.And as @siddhesh dighe mentioned don't use TextView initialization in loops.

TextView name = (TextView) convertView.findViewById(R.id.user_names);
TextView email = (TextView) convertView.findViewById(R.id.user_emails);

for (int i = 0; i < list.size(); i ++){
  name.append(list.get(i).toString());     
  email.append(list.get(i).toString());
}

Comments

0
  TextView name = (TextView) convertView.findViewById(R.id.user_names);
  TextView email = (TextView) convertView.findViewById(R.id.user_emails);

  for (int i = 0; i < list.size(); i ++){
   name.append(list.get(i).toString());
   email.append(list.get(i).toString());
  }

3 Comments

thanks i am using a list view to display user name & email your code appends name & email together in one list view like this : MANISHMANISHGUPTA.COM JOHNJOHN.COM i want it to be separate like one text view display name & other display email
@ManishGupta from where r u getting email
both from database table
0

Try to use append() instead :

TextView name = (TextView) convertView.findViewById(R.id.user_names);
TextView email = (TextView) convertView.findViewById(R.id.user_emails);
for (int i = 0; i < list.size(); i ++){
  if(i==0)
  {
    name.append(list.get(i).toString());
    email.append(list.get(i).toString());
  }else {
    name.append("," + list.get(i).toString());
    email.append("," + list.get(i).toString());
  }
}

Comments

0

You have an ArrayList with your data and you have a ListView, So you can just use ArrayAdapter for your listView and pass your Arraylist to it.

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, animalsNameList);
listView.setAdapter(arrayAdapter); 

Checkout the Complete tutorial here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.