2

I made a listview in a fragment. Now I need to add a simple icon to the listview items. The icon must be the same on every item. This will be a simple arrow (imageview). Is this possible with the listview I made? And if yes, how do I do it?

So like this format: My text here --space--space-- >


The code for the fragment:

  public class BiblioFragment extends Fragment {

final String[] items = new String[] { "Astma en alcohol", "Astma en huisdieren", "Astma en lichaamsgewicht",
        "Astma en ouder worden", "Astmamedicatie", "Bekende mensen met astma", "Longfunctieonderzoek", "Reizen en vakantie",
   "Sociaal leven", "Weetjes over astma", "Enzovoort", "Enzovoort", "Enzovoort" };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_biblio, container, false);

    ListView list = (ListView)view.findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Log.v("TAG", "CLICKED row number: " + arg2);

            Intent myIntent = new Intent(getActivity(), BiblioDetail.class);
            myIntent.putExtra("welkerij", arg2);
            startActivity(myIntent); 

        }

    });

    return view;

   }
  }

My XML for that fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" 
  android:background="#ffffff">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

</LinearLayout>
Charles
  • 50,943
  • 13
  • 104
  • 142
user2883477
  • 153
  • 1
  • 3
  • 16

2 Answers2

0

Create ListAdapter

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub
}

private List<Item> items;

public ListAdapter(Context context, int resource, List<Item> items) {

    super(context, resource, items);

    this.items = items;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.itemlistrow, null);

    }

    Item p = items.get(position);

    if (p != null) {


        ImageView ICon = ( ImageView) v.findViewById(R.id.description);




        }
    }

    return v;

}

In your Activity

ListView yourListView = (ListView) findViewById(R.id.itemListView);

// get data from the table by the ListAdapter
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, List<yourItem>);

yourListView .setAdapter(customAdapter);
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • Can I implement this in my fragment? – user2883477 Dec 11 '13 at 15:10
  • yeah you can implement it in fragment – Nambi Dec 11 '13 at 15:13
  • This does seem a bit overkill actually. I have everything running simply want to add the same icon in every row. Can't I just: ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, items); Put the Imageview in simple_list_item_1? There has to be a easier solution.. – user2883477 Dec 11 '13 at 15:17
  • @user2883477 that is not a over kill. that is how you need to do it. Except you can use compound textview. see my post for complete answer – Raghunandan Dec 11 '13 at 15:20
  • @user2883477 posted a snap shot also – Raghunandan Dec 11 '13 at 15:25
0

In onCreateView

ListView list = (ListView)view.findViewById(R.id.listView1);
CustomAdapter cus = new CustomAdapter(getActivity(),items);   
list.setAdapter(cus);

Use a custom adapter

public class CustomAdapter extends BaseAdapter {

    String items[];
    LayoutInflater mInflater;

    public CustomAdapter(Context context, String[] items) {
        mInflater = LayoutInflater.from(context);
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView ==null)
        {
            convertView = mInflater.inflate(R.layout.list_item,parent,false);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.textView1);
            holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag(); 
        }
        holder.tv.setText(items[position]) 
            // use holder.iv to set whatever image you want according to the position
        return convertView;
    }

    static class ViewHolder
    { 
        ImageView iv;
        TextView tv;
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="32dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="64dp"
        android:layout_marginTop="14dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="TextView" />

</RelativeLayout>

Snap

enter image description here

Edit:

If you feel this is over kill you can use a compound textview with image on the left and text on the right.

In the above I have used list_item.xml which is a custom layout. I inflate that layout in getView of custom adapter. I set the text to textview. I have set the default launcher icon to imageview. You can change it though.

I have also used a ViewHolder

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Also check

How ListView's recycling mechanism works

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256