当有人说“我尝试遵循一些教程......”但它们不起作用时,我首先想到的是,这有点难以置信。
那将是一个更容易解决的问题。
给你一个简单的 ListView 例子:
首先,根据您的喜好创建一个资源文件:(example.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
<ImageView
        android:id="@+id/disco_image"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_home"
        android:layout_width="96dp"
        android:layout_height="96dp"/>
<TextView
        android:id="@+id/disco_title"
        android:padding="12dp"
        android:layout_toRightOf="@+id/disco_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<TextView
        android:id="@+id/disco_info"
        android:padding="12dp"
        android:layout_toRightOf="@+id/disco_image"
        android:layout_below="@+id/disco_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>
然后,创建一个自定义适配器,它们非常简单。现在只需扩展一个 BaseAdapter。
public class ExampleAdapter extends BaseAdapter {
//Let's create some constants first, to fill out the rows
private static final String [] DISCO_NAMES = {"Disco One", "Disco Two", "Disco Three", "Disco Four"};
private static final String [] DISCO_INFO = {"Some Info One", "Some Info Two", "Some Info Three", "Some Info Four"};
private LayoutInflater mInflater;
//Our custom adapter needs a constructor, so you can create from your activity.
public ExampleAdapter (final Context context) {
    //for now, let's just get the context, we'll need it to inflate views
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    //this needs to return to the amount of rows you want to display.
    //right now we return a fixed value, this could vary based on your needs
    return DISCO_NAMES.length;
}
@Override
public Object getItem(int pos) {
    //this is useful for knowing what item is at what position
    //for now, let's just return the disco name shall we?
    return DISCO_NAMES[pos];
}
@Override
public long getItemId(int pos) {
    //This returns an id to the item
    //personally I don't use this, so you can just return the position
    return pos;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    //Ha, here's the important part
    //ListViews reuse rows, so let's check if the view (also known as convertview) is new or being reused
    if (view == null) {
        //this means it's a new view, so we need to inflate it
        view = mInflater.inflate(R.layout.example, null);
    }
    ((TextView) view.findViewById(R.id.disco_title)).setText(DISCO_NAMES[position]);
    ((TextView) view.findViewById(R.id.disco_info)).setText(DISCO_INFO[position]);
    //You can also set some images to the imageview on the layout we created earlier
    return view;
    }
}
然后,让我们创建一个 ListActivity 作为示例。注意ListActivit 不需要通过 setContentView 设置 Layout Resource,所以我们这里不调用它。
public class ExampleListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //create the adapter
    ExampleAdapter mExampleAdapter = new ExampleAdapter(this);
    //fill the listView
    setListAdapter(mExampleAdapter);
    }
}
这应该按原样编译,但出于性能原因,您可能需要查看 ViewHolder 模式和等等等等。显然您需要阅读更多内容,但我希望这有助于作为一个起点。