user3673449 user3673449 - 5 months ago 36
Android Question

Displaying an arrow in list items

I have a ListView in a fragment, and I want to display an arrow at the right of every list item. I followed this tutorial: http://willvk.blogspot.it/2011/07/adding-arrow-image-to-right-of-listview.html but the only thing I see when I open my fragment is a blank page. Here's my code:

public class MyFragment extends ListFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


String[] values = new String[] { "Item1", "Item2", "Item3",
"Item4"};

boolean[] listImages={true, true, true, true, true};
setListAdapter(new CustomAdapter(getActivity(), R.layout.fragment_my, R.id.text1, R.id.image1, values, listImages ));

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my, container, false);
}

@Override
public void onListItemClick(ListView list, View v, int position, long id) {
Intent intent = null;
switch(position) {
case 0:
intent = new Intent(getActivity(), Item1Activity.class);
break;
case 1:
intent = new Intent(getActivity(), Item2Activity.class);
break;
case 2:
intent = new Intent(getActivity(), Item3Activity.class);
break;
case 3:
intent = new Intent(getActivity(), Item4Activity.class);
break;

}
startActivity(intent);
}
}


CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<String>
{
Activity context;
String[] items;
boolean[] arrows;
int layoutId;
int textId;
int imageId;

public CustomAdapter(Activity context, int layoutId, int textId, int imageId, String[] items, boolean[] arrows)
{
super(context, layoutId, items);

this.context = context;
this.items = items;
this.arrows = arrows;
this.layoutId = layoutId;
this.textId = textId;
this.imageId = imageId;
}

public View getView(int pos, View convertView, ViewGroup parent)
{
LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(layoutId, null);
TextView label=(TextView)row.findViewById(textId);

label.setText(items[pos]);

if (arrows[pos])
{
ImageView icon=(ImageView)row.findViewById(imageId);
icon.setImageResource(R.drawable.arrow);
}

return(row);
}


fragment_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >

<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp" />

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:textSize="16sp"
android:typeface="sans" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />

</RelativeLayout>

Answer

Remove the ListView from your layout file, and no need to override the onCreateView method the the ListFragment, Remove the following method from ur class

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my, container, false);
    }