82886

Android different Row layout only for first row in BaseAdapter

Question:

I am making an <strong>Custom listview</strong> with <strong>Custom Adapter</strong> extending <strong>BaseAdapter</strong>. Now I want to set a different <strong>layout</strong> only for first row and another <strong>layout</strong> for all other rows. I am using this code but it sets the <strong>special layout</strong> for <strong>1st row</strong> and also repeats it after every 5/6 rows. How can I fix it and can set it only for 1st row and another layout for all other rows.

public class NewsAdapter extends BaseAdapter { private Context context; List<News> newsList; private Typeface customBanglaFont; private LayoutInflater inflater; ImageLoader imageLoader = AppController.getInstance().getImageLoader(); public NewsAdapter(Context context, List<News> newsList){ this.context = context; this.newsList = newsList; } @Override public int getCount() { return newsList.size(); } @Override public Object getItem(int arg0) { return newsList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View convertView, ViewGroup viewGroup) { View row; row = convertView; ViewHolder holder=null; if(row == null){ if(position == 0){ inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row =inflater.inflate(R.layout.row_single_big_view,viewGroup,false); } else{ inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row =inflater.inflate(R.layout.row_single_default,viewGroup,false); } holder = new ViewHolder(row); row.setTag(holder); } else{ holder = (ViewHolder) row.getTag(); } if (imageLoader == null){ imageLoader = AppController.getInstance().getImageLoader(); } final News news = newsList.get(position); customBanglaFont = Typeface.createFromAsset(viewGroup.getContext().getAssets(), "fonts/SolaimanLipi.ttf"); holder.newsTitleView.setTypeface(customBanglaFont); holder.newsTitleView.setText(news.getTitle()); holder.thumbNail.setImageUrl(news.getFeaturedImgSrc(), imageLoader); return row; } } /** * Custom View Holder Class * @author Tonmoy * */ class ViewHolder{ TextView newsTitleView; NetworkImageView thumbNail; public ViewHolder(View v) { // TODO Auto-generated constructor stub newsTitleView = (TextView) v.findViewById(R.id.news_title); thumbNail = (NetworkImageView) v.findViewById(R.id.news_image); } }

Answer1:

Why don't you use the Header functionality already built into ListView for this specific thing.

The docs: <a href="http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View,%20java.lang.Object,%20boolean%29" rel="nofollow">http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View,%20java.lang.Object,%20boolean%29</a>

or a SO question: <a href="https://stackoverflow.com/questions/7978359/using-listview-how-to-add-a-header-view" rel="nofollow">Using ListView : How to add a header view?</a>

Answer2:

your attempting is wrong. Your way you will be able to inflate just one layout. In fact, after the method returns the first time, convertView is not null. To fix quickly you could override getViewTypeCount and getItemViewType. This way you will get convertViews eqaul to the return value of getViewTypeCount, or you could just add the first item as header view for the ListView

Recommend

  • How to clear a text field in Cocoa
  • How to forcefully remove Google default selected Account in Gmail API?
  • How to use nestjs Logging service
  • Use a Custom CakeResponse Class in CakePHP 2.1+
  • Deployed Simple HelloWorld with Spring MVC app (without web.xml) gives 404 error
  • How to initialize the value of an input[range] using AngularJS when value is over 100
  • Angular material design md-tab scroll content
  • Use $rootScope to store a reference to a service
  • Count how many strings in an array have duplicates in the same array [duplicate]
  • Select image from gridview and display it on other intent
  • Can not cast IMAPInputStream to Multipart
  • Opening a floating menu (context menu) in Android?
  • How to store items of multiple JSON Array and display it listview?
  • Error inflating class ir.noghteh.JustifiedTextView
  • Toast not appearing on Android 4.4 KitKat
  • Can you set a background imaged to a PdfPTable when using iText
  • Change Background on a specific row based on a condition in Custom Adapter
  • Angular2 How to display localStorage value inside HTML5 template?
  • How to keep button state across different pages?
  • On which thread JavaFX change listeners are executed ?
  • Fragment gives me an error while inflating 1
  • Wrong row deleted from custom listview with spinner
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • GridView breaks while scrolling
  • Content-Length header not returned from Pylons response
  • Play WS (2.2.1): post/put large request
  • Change multiple background-images with jQuery
  • How to access EntityManager inside Entity class in EJB3
  • Android screen density dpi vs ppi
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • vba code to select only visible cells in specific column except heading
  • Change an a tag attribute in JavaScript based on screen width
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?