Implement Dynamic textviews inside Asynctask 's onPostExecute method - android

I am downloading some data from the internet in an Asynctask class.So for that i am using doInBackground function. After this function is executed i am returning the value to onpostexecute method.
In this onpostexecute method
i need to make dynamic textviews.
P.S: I am getting all the data in my for loop, i am not getting any errors, i just cannot form the dynamic textviews.
This is my code:
protected void onPostExecute(String file_url) {
//Links.setText(file_url);
int iterator=0;
Elements linksText = doc.select("#chapters .tips");
for (Element link : linksText) {
link_link = link.attr("href");
narutoLinks[iterator]=link_link;//from latestlinks
System.out.println("narutoLinks[iterator]= "+iterator+" "+link_link);
link_Text = link.text();
narutoLinkHeadingName[iterator]=link_Text;
System.out.println("narutoText[iterator]= "+iterator+" "+narutoLinkHeadingName[iterator]);
iterator++;
}
System.out.println("iterator= "+iterator);
TextView[] textViewArray = new TextView[iterator];
for( int i = 0; i < iterator; i++) {
textViewArray[i] = new TextView(narutoLinksOnly.this);
textViewArray[i].setText(narutoLinkHeadingName[i]);
textViewArray[i].setId(i);
textViewArray[i].setTextColor(0xff000000);
textViewArray[i].setTextSize(20);
textViewArray[i].setOnClickListener(this);
textViewArray[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(textViewArray[i]);
}
System.out.println("senkai");
if(connectionTimeout==true)
{showToast("Connection Timeout");
connectionTimeout=false;
}
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
}
and this is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/Ivory"
android:id="#+id/dynamicTextview1"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tvNarutoLinksOnly"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textColor="#color/black" />
</ScrollView>
</LinearLayout
This is how my oncreate method looks like:
View linearLayout;//declaring it inside the class itself
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.narutolinksonly);
linearLayout = findViewById(R.id.dynamicTextview1);
initialize();
new DownloadLinksFromURL().execute(url);
System.out.println("inside on create");
}
Am i missing something in my code?
Also what is the best way to achieve dynamic textview after getting data from an Asynctask class?
Kindly help.

use a listview instead.
And set an array adapter to it with the list of string that you get from the server.
But in your case try:
textViewArray[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

you have to setup a listview adapter. You have the data, but nothing on your onCreateView ties that data to a listView.
Here's an example from the Android site on an adapter using cursors
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1
// Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null,
fromColumns, toViews, 0);
setListAdapter(mAdapter);
Trade their cursor data in for your textViewArray data and make it a BaseAdapter or similar and you're done

Related

confused about Textview and Listview for displaying a list

I tried a few different layouts to get deeper in possibilities and variances.
I started with an array to display the items in an listview that worked fine.
Now I wanted to display items that I got out of a database via JSON.
I get the following error: You must supply a resource ID for a TextView
I used the same XML-file, that worked before, here my all_products.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px">
</ListView>
</LinearLayout>
In my java class I used the code that I used before for the array adapter, I changed only the parameter which should be displayed. Here part of my AllProductsActivity.java:
private void processValue(ArrayList<String> result) {
{
ArrayAdapter adapter = new ArrayAdapter<String>(AllProductsActivity.this, R.layout.all_products, result);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
result comes from my asynctask. here a snippet as well:
JSONArray jsonArray = new JSONArray(result.toString())
ArrayList<String> listdata = new ArrayList<String>();
for(int n = 0; n < jsonArray.length(); n++)
{
JSONObject object = jsonArray.getJSONObject(n);
listdata.add(object.optString("nr"));
}
return listdata;
protected void onPostExecute( ArrayList<String> result) {
pdLoading.dismiss();
processValue(result);
}
Why I get the error? And perhaps what about using only a Textview. As I was searching for the toppic, I found different threats where people using Textviews instead of Listview.
EDIT: So it works I added another xml file, mytextview.xml
<TextView android:id="#+id/mytext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px"
xmlns:android="http://schemas.android.com/apk/res/android">
and changed the adapterstatement to the following:
ArrayAdapter adapter = new ArrayAdapter<String>(AllProductsActivity.this,
R.layout.mytextview,R.id.mytext , result);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
when you use custom layout R.layout.all_products then Adapter don't know about the view to set the data from data list
so simply you need to tell the adapter , the ID of your text view to set data on .
ArrayAdapter adapter = new ArrayAdapter<String>
(AllProductsActivity.this,
R.layout.all_products,R.id.your_text_view_id, result);
// ^^^^^^^^^ pass the text view ID
ArrayAdapter (Context context,
int resource,
int textViewResourceId,
T[] objects)
or
If you just want to display your data without any custom layout then can use in build android resource layout as
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,result);
in Pavneet's first solution take care that:
resource: is the resource containing a textView not your xml file that contain the list itself (not R.layout.all_products)
textViewResourceId: the ID of the textView contained in the resource
When instantiating your ArrayAdapter, the id you provide must be from a layout containing only a TextView. That is what the error You must supply a resource ID for a TextView means

Listadapter not binding values to listview

i created a demo app to display the list of products on screen. the product list comes from remote server. in the logs i can see the data coming and being stored in the arralist but somehow it is not binding with list view. i am making the http call in the background using async class.
below is the activity on create code
ListAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbtest2_all_products);
Log.i("Entrey level=","entere dbtest2allproducts class");
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = new ListView(DBtest2AllProducts.this);
// updating listview
lv.setAdapter(adapter);
below is postexecute method of
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// ((BaseAdapter) adapter).notifyDataSetChanged();
Log.i("before adapter",productsList.toString());
adapter = new SimpleAdapter(
DBtest2AllProducts.this, productsList,
R.layout.activity_dbtest2_list_items, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
//setAdapter(adapter);
though i believe as there is some sync issue with the setAdapter(adapter) step but i since i am new to android so couldn't bet on it. i tried calling setAdapter(adapter) method and adapter.notifyDataSetChanged() withing the postexecute method but both of them throw error saying "method is not defined for this type" However if i cast adapter to (BaseAdapter) then adapter.notifyDataSetChanged() doesn't throw any error but i actully dont know what difference does this casting make and also casting didn't work as well.
Below is log cat
I/before adapter(32729): [{pid=1, name=iphone 4s}, {pid=2, name=samsung}]
Please suggest if you see any issues with the code. Thanks for helping in advance.
below are the xml layouts
all_product.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingTop="20dip"
android:paddingLeft="10dip"
android:textSize="17sp"
android:textStyle="bold" />
</RelativeLayout>
You need to initialize you adapter before setting it to the ListView. Make ListView global and set the adapter only after getting the data
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbtest2_all_products);
Log.i("Entrey level=","entere dbtest2allproducts class");
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get list view from xml file
lv = findViewById(R.id.listview);
FrameLayout rootView = (FrameLayout) findViewById(android.R.id.content);
//Add this line to make sure your list is using the whole screen width
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.addView(lv);
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
adapter = new SimpleAdapter(
DBtest2AllProducts.this, productsList,
R.layout.activity_dbtest2_list_items, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
lv.setAdapter(adapter);
}
#Pawan Rawat, after implementing the above answer, and if your confident in carrying on, switch to Volley, a networking library which is more robust, performance network calls faster with less code.
Please read more at http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

What to use to put layout in activity Android?

I have an activity that uses setContentView to initiate a listview, and then I have an If-Else condition. In the If part of the statement I use a simpleAdapter to place a new layout in the listview and other data from a cursor. In the Else part, I just want to put a sentence in a label to provide some information. How can I do that? I tried parameters and textview but they didn't work. I tried to put another setcontentView but it can't work either.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
doMySearch() is a cursor that extracts data from database based on a keyword input by user.
public void onPostExecute() {
SimpleCursorAdapter adapter;
Int count = doMysearch().getCount;
If (count >= 1){
adapter=new SimpleCursorAdapter(this, R.layout.activity_results,
doMySearch(), new String[] {
DB.COL_NAME,
DB.COL_CITY },
new int[] { R.id.lblName, R.id.lblCity },
0);
setListAdapter(adapter);
}
Else {
}
I just want a sentence that says "No data found" for the Else part. Thanks.
It is not clear for me what you are asking but here we go.
There is empty view setting in ListView for empty lists, you have to add another empty view in your current layout. Then you will set the empty view layout in code like below:
//add to your layout
<LinearLayout
android:id="#+id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There is no item" >
</TextView>
</LinearLayout>
//set empty view
ListView listView = (ListView) container.findViewById(R.id.list);
LinearLayout emptyView = (LinearLayout) container.findViewById(R.id.empty);
listView.setEmptyView(emptyView);
As far as I understand the label doesn't show up because of the listview, right?`
EDIT:
Okay I think I get it now.
To keep it simple you could hide your ListView with myList.setVisibility(View.GONE);
Then make a premade Label visible with myLabel.setVisibility(View.VISIBLE) that already holds your error-string.
The harder way you could make a StringArray, like this in (for example) the strings.xml
<string-array name="error">
<item>No results found</item>
</string-array>
Then, in your else-block you can do somethink like that:
ListView lv = (ListView) findViewById(R.id.myList);
String[] content = getResources().getStringArray(R.array.error);
ArrayAdapter<String> typeList = new ArrayAdapter<String>(this, R.layout.simple_list_item_custom, content);
lv.setAdapter(typeList);

Adding new elements to the top of the ListView

I'm writing an IM client and I need to download (from filesystem or network) and show new elements at the top of ListView (it is history of messages -- older messages are at the top, newer -- at the bottom). I implemented my own Adapter for ListView but I can't add new elements at the beginning of the list and redraw it. (notifyDataSetChanged() isn't good for me, because indexes of messages in ListView changes and android can't redraw it normally).
How do other apps do something similar?
I don't create special code for it, I am simply creating new Adapter for my ListView:
messagesListView.setAdapter(new MessagesListAdapter(this));
And redefine getView() and getCount() method in MessagesListAdapter (extends ArrayAdapter now).
My XML for ListView is
<ListView
android:id="#+id/dialog_messages_list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="#dimen/title_height">
</ListView>
And my XML for one element (one message) is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/dialogMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:background="#drawable/dialog_message_in"
/>
<TextView
android:id="#+id/dialogMessageDatetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
May be you need other code?
EDIT: I tried
messagesListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList));
(new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
arrayList.add(0, "qwer");
}
}).start();
But it also not seems good. I tried to call ((ArrayAdapter<String>)messagesListView.getAdapter()).notifyDataSetChanged(); in thread, but it makes exception.
I suggest reversing the order of the List to display the newest result first.
Run this example:
public class Example extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] array = {"oldest", "older", "old", "new", "newer"};
List<String> list = new ArrayList<String>();
Collections.addAll(list, array);
Collections.reverse(list);
// When you want to add new Strings, put them at the beginning of list
list.add(0, "newest");
ListView listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
}
}
You don't have to override anything in the ArrayAdapter or ListView this way.
you can programmatically add some views in the class associated with your list view. For example:
To add stuff to a layout and make new elements:
TextView tv = new TextView(getApplicationContext());
EditText edit = new EditText(getApplicationContext());
relative.addView(tv);
relative.addView(edit);
This is to manipulate an existing element in the xml layout:
final TextView tv = (TextView) v.findViewById(R.id.listItem);
tv.setText("This an item I am changing");
If you look at some of the related questions, they will give your more information on this. But you can also checkout other people's custom listviews and adapters online. This one is really nice: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Android: get the i-th TextView inside a ListView

I'm try to write a little application and the releated unit tests.
I have a ListView binded to a SimpleCursorAdapter reading data from an SQL table.
The Activity#onCreate() method is:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new DatabaseHelper(this);
SQLiteDatabase dbRead = dbHelper.getReadableDatabase();
String[] columns={BaseColumns._ID, ENTRY_VALUE};
cursor = dbRead.query(ENTRIES_TABLENAME, columns, null, null, null, null, null);
String[] from = {"value"};
int[] to = {R.id.value};
adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to);
setListAdapter(adapter);
}
My test inside the unit-test is:
#UiThreadTest
public void testTheElementInsideTheDBisDisplayedInTheList() {
String entryValue = "clipboard entry 1";
DatabaseHelper dbHelper = new DatabaseHelper(cmActivity);
Cursor beforeCursor = selectAllEntries(dbHelper);
// The table, at the begining of the test, is empty, I control that
assertEquals(0, beforeCursor.getCount());
// I insert the new value in the table
insertEntry(dbHelper, entryValue);
// and I control that is really inside the table now
Cursor afterCursor = selectAllEntries(dbHelper);
assertEquals(1, afterCursor.getCount());
// This method calls the method "requery()" on the cursor associate
// to the listView's adapter to update the list view
cmActivity.updateList();
// I control that the listView is updated
assertEquals(1, entryList.getCount());
// Now I try to retrive the only child inside the list view
// to extract the text inside it and to compare this text with
// the value inserted into the DB table.
TextView entryView = (TextView) entryList.getChildAt(0);
String viewText = entryView.getText().toString();
assertEquals(entryValue, viewText);
}
My problem is in the third-last row:
TextView entryView = (TextView) entryList.getChildAt(0);
I sude getChildAt() to get the first TextView child of the ListView. But this method returns null, so the test gets a NullPointerException.
Maybe getChildAt() is not the right method to get the View child from a ListView, so which is the correct one?
I see from the documenation that the method works with GroupView, I didn't use them, do I need to configure a default GroupView and put all the entry inside it? In this way, will getChildAt(0) work? Is this the correct way to setup a ListView?
thank you, bye
Andrea
As asked by Vivek, I post here the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
/>
</LinearLayout>
As you can see is very very basic. Also le list entry is very simple:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/value"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
I doubt if the list is populated when you call getChildAt() method. So call getChildCount() method and see if the list is populated. And post back the output here.
Edit:
Now I understand the problem. ListView.getCount() method returns the number of items populated in the list. And ListView.getChildCount() Method or ListView.getChildAt() Method will return 0 here because these methods will return a value only when the view is visible to the user. You can use getChildAt() method only after the textviews are generated. i.e If you use the method in OnItemClick method of the listview, or any listview listener implementation, you will get the desired output. What is the need to get the reference to the textviews here in this method anyways?

Categories

Resources