0

I am trying to display a new toast for every item in my cursor, how would I do this? I've searched SO, but can't find any relevant, useful answers. Here is my code, but it doesn't work:

 while(mNotesCursor.moveToNext()){ 
    Toast.makeText(getActivity(),  
    mNotesCursor.getString(mNotesCursor.getColumnIndex("title")),
            Toast.LENGTH_LONG).show();
    }
Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34
  • Are you getting an exception or just not getting the expected behavior? – JayPea May 24 '13 at 17:26
  • just not getting the expected behavior – Buneme Kyakilika May 24 '13 at 17:27
  • I haven't tested this before, but my guess would be that you can't show a new toast while another one is still showing. Take a look at this question: http://stackoverflow.com/questions/6496725/showing-multiple-toast-at-the-same-time-problem – JayPea May 24 '13 at 17:29
  • I tried that, but its still not working as expected. – Buneme Kyakilika May 24 '13 at 17:36
  • You can show a toast while another is displaying. I believe they queue up. This means you should be careful, because if there are a lot of strings to be displayed, the user could be waiting for toasts to go away for hours. I like Smarth Jain's answer below. – Edward Falk May 24 '13 at 18:39
  • The toasts were just for debugging – Buneme Kyakilika May 24 '13 at 18:47

2 Answers2

3

Toasting while iterating through the the cursor is not the best idea. Here's why:

You are using LENGTH_LONG, and the that means that a toast would last for approx 3 seconds. Whereas your for loop would probably finish execution in a fraction of a second. So the toast would be displayed in order, but they would transition so slowly that it probably wouldn't make sense.

So i would suggest you to display the content in an alert dialog or the activity itself so the user would be able to make more sense out of the content.

EDIT:

I assume you are executing this on the main thread.

LinearLayout root = (LinearLayout) getActivity().findviewById(R.id.rootLayout);
    while(mNotesCursor.moveToNext()){
        TextView tv = new TextView(getActivity());
        tv.setText(mNotesCursor.getString(mNotesCursor.getColumnIndex("title")));
        root.addView(tv);
    }
Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34
Sam
  • 3,413
  • 1
  • 18
  • 20
  • I tried that, but its still not working as expected. My question is, how do I create a new TextView for every item in my cursor, and the TextViews content should be from the cursor, i.e the 2nd TextViews text should be the 2nd cursor items text. – Buneme Kyakilika May 24 '13 at 17:36
1

if you are looking to add textview to your view dynamically then here is how you can do it

<?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout 
    android:id="@+id/lineralayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</LinearLayout>
</ScrollView>

inside your activity class

LinearLayout l = (LinearLayout) findViewById(R.id.lineralayout1);
while(mNotesCursor.moveToNext()){ 
    TextView tv = new TextView(this);
    tv.setText(mNotesCursor.getString(mNotesCursor.getColumnIndex("title")));
l.addView(tv);
}
prvn
  • 916
  • 5
  • 5