Android 在列表项中显示箭头

Android 在列表项中显示箭头,android,listview,Android,Listview,在片段中有一个ListView,我想在每个列表项的右侧显示一个箭头。我遵循了这个教程:但是当我打开我的片段时,我看到的只有一个空白页。这是我的密码: public class MyFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Strin

在片段中有一个ListView,我想在每个列表项的右侧显示一个箭头。我遵循了这个教程:但是当我打开我的片段时,我看到的只有一个空白页。这是我的密码:

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);
    }
公共类CustomAdapter扩展了ArrayAdapter
{
活动语境;
字符串[]项;
布尔[]箭头;
国际布局;
int-textId;
int-imageId;
公共CustomAdapter(活动上下文、int layoutId、int textId、int imageId、字符串[]项、布尔[]箭头)
{
超级(上下文、布局ID、项目);
this.context=上下文;
这个项目=项目;
这个。箭头=箭头;
this.layoutId=layoutId;
this.textId=textId;
this.imageId=imageId;
}
公共视图getView(int pos、视图转换视图、视图组父视图)
{
LayoutInflater充气器=上下文。getLayoutInflater();
视图行=充气机。充气(layoutId,null);
TextView标签=(TextView)行。findViewById(textId);
label.setText(项目[pos]);
如果(箭头[pos])
{
ImageView图标=(ImageView)行。findViewById(imageId);
icon.setImageResource(R.drawable.arrow);
}   
返回(行);
}
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>

从布局文件中删除ListView,无需覆盖ListFragment的onCreateView方法, 从ur类中删除以下方法

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

你需要将你的片段附加到一个活动中,因为片段本身并不打算运行 查看本教程

请在下面找到带箭头右侧的列表项的答案

ListItems.java

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

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

    ImageAdapter(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.ic_btn_search_go);
        }   

        return(row);
    }
}
导入android.app.ListActivity; 导入android.os.Bundle

公共类ListItems扩展了ListActivity{

String[] listItems={"alpha", "beta", "gamma", "delta", "epsilon"};
 boolean[] listImages={true, true, true, false, true};

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_list);

        setListAdapter(new ImageAdapter(this, R.layout.main_list, R.id.text1, R.id.image1, listItems, listImages ));
    }
}

ImageAdapter.Java

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

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

    ImageAdapter(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.ic_btn_search_go);
        }   

        return(row);
    }
}
main_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layercontainer"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#ffffff">
   <ListView
   android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
   <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:textColor="#000000"   
    android:typeface="sans"/>
   <ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"/>
</RelativeLayout>