listView.addHeaderView() giving "null pointer exception" - android

I have a requirement that I need to add header to my list view .
Below is my code for achieve the same..
ListView listView;
listView = (ListView) findViewById(R.id.list_view);
//View header = View.inflate(this , R.layout.header, null);
LayoutInflater ll = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v2 = ll.inflate(R.layout.header, null, false);
listView.addHeaderView(v2);
But "listView.addHeaderView(v2)" this line giving "NullPointerException"
Please let me know what can be done to resolve this issue.

just try this:
listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.header, null));
instead of :
LayoutInflater ll = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v2 = ll.inflate(R.layout.header, null, false);
listView.addHeaderView(v2);

Related

Unable to add dynamic textView in android

I want to add textview dynamically for which I have added the below code.But I am do not see any textview on my activity. I cant add a listview as I am already using recycleview . Any one any error ?
{
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout review_layout = (LinearLayout) rootMovieView.findViewById(R.id.review_comment);
for (ReviewsResult comment : review.getReviewsResults()) {
View view = layoutInflater.inflate(R.layout.layout_movie_comments, review_layout, false);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) view.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
review_layout.addView(view);
}
}
You can also try something like this
View myLay; // view that contains you textView
LinearLayout rootlay; //root view in which you want to add
myLay = LayoutInflater.from(this).inflate(R.layout.layout_movie_comments,
null);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) myLay.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
rootlay.addView(myLay);
Hope this helps :)
Do below two changes and check weather it is working or not, as nothing else seems to wrong in code.
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.layout_movie_comments, null);
rootMoviewView is not the rootView it was a recylceView in a rootView .This way the textview is generated in rootView not in recylceView.

Dynamic views are not properly loaded

Im retrieving values from sqlite db and trying to display the values dynamically.
This is how I retrieve the values,
List<PendingOrdersDao> contacts = db.getAllPendingOrders();
for (PendingOrdersDao cn : contacts) {
initView(cn.getProformoInvoiceNumber(), cn.getProformoInvoiceDate());
}
When I invoke initView method, it should add views to the layout dynamically depending on the number of entries present in db. Following is my initView method.
private void initView(String proformaInvoice, String invoiceDate){
LinearLayout mLinearLayoutContainer = (LinearLayout) mRootView.findViewById(R.id.dashboard_container);
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dashboard_fragment, mLinearLayoutContainer, false);
mProformaInvoice = (TextView) v.findViewById(R.id.dashProformaInvoiceNo);
mInvoiceNumber = (TextView) v.findViewById(R.id.dashInvoiceNo);
mProformaInvoice.setText(proformaInvoice);
mInvoiceNumber.setText(invoiceNumber);
mLinearLayoutContainer.addView(v);
}
The problem is that, there are three entries in the db hence 3views should be displayed but it displays only one view
This is my working code :-
for (int i = 0; i < 3; i++) {
LinearLayout mLinearLayoutContainer = (LinearLayout) findViewById(R.id.test_container);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(android.R.layout.simple_list_item_1, mLinearLayoutContainer, false);
mTextView = (TextView) v.findViewById(android.R.id.text1);
mTextView.setText("This is i : " + i);
mLinearLayoutContainer.addView(v);
}
See if helps you. :)
First of all, check the contacts list size.
Do you setup correct orientation for your linear layout? Vertical or horizontal?
advice
Why not to use RecyclerView or ListView?

How can I add an ImageView as a ListView Header?

Can anyone help me to find out how to add an ImageView as a header to a ListView?
My code is here:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snapshot = inflater.inflate(R.layout.map_snapshot, null);
ListView channelsList = (ListView) findViewById(R.id.channelsList);
channelsList.addHeaderView(snapshot);
By now it shows nothing.
Try inflating your header view passing the parent (ListView) as parameter:
ListView channelsList = (ListView) findViewById(R.id.channelsList);
View snapshot = inflater.inflate(R.layout.map_snapshot, channelList, false);
Then, just add it to the ListView:
channelsList.addHeaderView(snapshot);
Try:
ListView channelsList = (ListView) findViewById(R.id.channelsList);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.map_snapshot, channelsList , false);
channelsList .addHeaderView(header, null, false);
In layout map_snapshot,you must be having parent layout as here i have taken Linear Layout likewise take that.
ListView channelsList = (ListView) findViewById(R.id.channelsList);
LinearLayout headerView = (LinearLayout) getLayoutInflater().inflate(R.layout.map_snapshot, null);
listView.addHeaderView(headerView);
if you want to add views in layout:
TextView mUsername=(TextView) headerView.findViewById(R.id.user_name);
You can use CommonsWare MergeAdapter or SectionedListAdapter. With this library you can use any View as Header in your Listview. See this SO question and accepted answer for more information.

How to add a view as ListView header

I have a View extending google MapView. And a ListView with items. What I need is to put MyMapView as a header to the list view. Help please what to do
Update: When I try to do this:
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView list = (ListView) findViewById(R.id.list);
View m = inflater.inflate(R.layout.map, list, false);
list.addHeaderView(m);
I got an error:
MapViews can only be created inside an instances of MapActivity
I think it's because I have removed my xml layout for map into different.xml from my activity
Try this
Create your ListViewHeader.xml first
and do this
ListView lv = getListView(); // Your listView
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.YourHeader, lv, false);
lv.addHeaderView(header, null, false);
take a look on Android: Adding static header to the top of a ListActivity
Try to call
ListView.addHeaderView (View v)
before you set adapter to this ListView
if you are using a custom adapter, in getView() method provide different view at position 0.
if(position==0)
{
//Provide header view
}
else
{
//provide normal row
}
You can do the following to add a view as ListView header in android.
ListView list = (ListView) findViewById(R.id.list);
LayoutInflater inflater = getLayoutInflater();
ViewGroup viewGroup= (ViewGroup)inflater.inflate(R.layout.YourLayoutFile,list, false);
list.addHeaderView(viewGroup, null, false);

This works, but is it the "right" way?

I'm reusing the ListView AND the LayoutInflater here for both a header and a footer:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = (View)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
I reckon reusing the ListView is perfectly sensible, but I'm not so sure about the LayoutInflater. Am I flirting with disaster here, or is this okay?
Please refer to : http://developer.android.com/reference/android/widget/ListView.html#getFooterViewsCount()
Returns the number of footer views in the list. Footer views are
special views at the bottom of the list that should not be recycled
during a layout.
So yeah... I don't think you are doing anything wrong.

Categories

Resources