【发布时间】:2014-06-21 18:16:28
【问题描述】:
我在片段中有一个 ListView,我想在每个列表项的右侧显示一个箭头。我遵循了本教程:http://willvk.blogspot.it/2011/07/adding-arrow-image-to-right-of-listview.html,但是当我打开我的片段时,我唯一看到的是一个空白页面。这是我的代码:
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>
【问题讨论】: