Reputation: 1202
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();
}
Upvotes: 0
Views: 234
Reputation: 3413
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);
}
Upvotes: 3
Reputation: 916
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);
}
Upvotes: 1