0

today i have a question i was wondering about. Let's say that i have a listview which shows only a list of text. The listview layout is a simple list view containing only a Textview.

Now, when i show the result, the first view, i want to show is a textview with let's say Image (i think the layout should change here). After that, it will show the regular textview. You see, in applications like news they show the first news with big pictures and text, and the rest of the list with only texts. Can you give me some idea on that ? or do i have to make it seperate ?

Thank you. my simple adapter->

@Override
        public void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();

             // Updating parsed JSON data into ListView

            ListAdapter adapter = new SimpleAdapter(getActivity(), postList, R.layout.list_item,
                    new String[] { POST_TITLE,POST_CONTENT, GUID },
                    new int[] {R.id.email, R.id.mobile, R.id.guid });

            newsList.setAdapter(adapter);
        }
Wang'l Pakhrin
  • 858
  • 3
  • 15
  • 29
  • I think you can make a unique layout with 2 differents sections: A main linearLayout that has the textview and whatever you want and the other linear that has images text urls etc. And when you are passing the data to your adapter you may have in your Model Class a boolean that indicates if is a complete row or not. Also setVisibility to visible to your linear when you now in your adapter if is a complete row or not. – JpCrow Apr 16 '15 at 19:29

3 Answers3

1

I would put both imageView and textView inside List Item Layout. Then if there is image available, i set imageView.setVisibility(View.Visible). If no image, imageView.setVisibility(View.Gone).


This is old code below, which uses Cursor and some deprecated methods. You can change your adapter to use Cursor with your own way or you can use List, Array to store items inside ListView. Check how i inflate imageView and textView inside adapter.

public class Home_fragment extends Fragment{
ChannelAdapter adapter;
private ListView mListView;
Cursor cursor;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    ...
    View view = inflater.inflate(R.layout.home_fragment, null);
    mListView = (ListView) view.findViewById(R.id.list);
    cursor = ((CounterPP) getActivity().getApplication()).addList.query(); 
    getActivity().startManagingCursor(cursor);
    adapter = new ChannelAdapter(getActivity(), R.layout.list_item, cursor, FROM, TO);
    mListView.setAdapter(adapter);
    ....
}

static class ViewHolderItem {       
        TextView textHere;
        ImageView imageHere;
}

// You can extend ArrayAdapter for your own adapter implementation
public class ChannelAdapter extends SimpleCursorAdapter {
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final  ViewHolderItem viewHolder;
        if(convertView==null){
            LayoutInflater inflater =(LayoutInflater)getActivity().getApplicationContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            //R.layout.list_item xml has imageView and textView
            convertView = inflater.inflate(R.layout.list_item, parent, false);

            viewHolder = new ViewHolderItem();         
            viewHolder.textHere = (TextView) convertView.findViewById(R.id.textNew);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageNew);

            convertView.setTag(viewHolder);

        }else{
            viewHolder = (ViewHolderItem) convertView.getTag();
        }

        if(you have image to show){
            viewHolder.imageHere.setVisible(View.VISIBLE);
            viewHolder.textHere.setText("Text From your List");
            viewHolder.imageHere.setDrawable(your image here);
        }else{
            viewHolder.imageHere.setVisible(View.GONE);
            viewHolder.textHere.setText("Text From your List");       
        }
        return convertView;
    } 

This code may not work properly, but it gives the idea.

Jemshit
  • 9,501
  • 5
  • 69
  • 106
0

The above requirements can be fulfilled in a number of ways based upon your choice:

1) Consider item 0 as a separate view and the rest seaprately. Pass all the items from item 1 to item N in the adapter of listview. While inflate item 0 as a separate view and add it as a header to list view.

Pros: (1) You can inflate completely different views for item 0 and the rest as separately. At the same time you can take full advantage of view reusability feature of listview. (2) If you try to use onItemClickListener to determine on click action,click on Header as well as list items can be received at a single point directly.

Cons: (1) In this way you can add highlighted views only at the top or the bottom effectively but nowhere in the middle if requirements change in future.

2) You can use a unique identifier in your model class used to detect which object need to be shown in a highlighted/different view. Then within your adapter you can use inflate 2 separate layouts for different items highlighted and non highlighted.

Pros: (1) In course of time you can add highlighted items anywhere in the list not only on 0th item.

Cons: (1) You cannot use the view reusability feature via ViewHolder in getView() and need to inflate different views every time based on decision logic.

I would recommend you to go with method 1 for now and switch to method 2 if requirements change in future.

  • Hello, do i need to use getView function to do that ? i am new on android. Sorry if i sound dumb here – Wang'l Pakhrin Apr 16 '15 at 20:00
  • You need to create a custom adapter by extending BaseAdapter or ArrayAdapter and override the getView() method. Next you need to create an object of adapter and pass it to ListView. It is in this getView method where you can inflate the listView layout and decorate the the views based upon decision logic. – Abhilash Bhaduri Apr 16 '15 at 20:18
  • but i use simple adapter , not base or array adapter – Wang'l Pakhrin Apr 17 '15 at 00:09
  • In case you are using a SimpleAdapter you can use only Method 1. Here you need to make a few changes. When instantiating simpleAdapter simply pass the list containing items from position 1 to position n-1 where n is the total items in list .Inflate the item 0 in a separate view and add this as a header to listview. – Abhilash Bhaduri Apr 17 '15 at 08:49
0

i did something like this . Have a look !

public View getView(int position, View convertView, ViewGroup Parent){
                if(convertView == null){
                    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.list_item,null);

                }
                TextView thisview = (TextView) convertView.findViewById(R.id.email);
                int getListPos = newsList.getFirstVisiblePosition();
                //i set the count starting 0 and saved in the hashmap array 
                //to compare the first result with the first position of listview
                int count = Integer.parseInt(objects.get(position).get("ListCount"));

                if(getListPos == count) {
                    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.list_item_header,null);
                    TextView HeaderText = (TextView) convertView.findViewById(R.id.headertext);
                    TextView HeaderContent = (TextView) convertView.findViewById(R.id.headercontent);
                    HeaderText.setText(objects.get(position).get(POST_TITLE));
                    HeaderContent.setText(objects.get(position).get(POST_CONTENT));

                }else{
                    thisview.setText("Normal line");
                }

                return convertView;
            }
Wang'l Pakhrin
  • 858
  • 3
  • 15
  • 29