0

我是 Android 开发的新手。我在java中创建了一个自定义对象。如何将其渲染到屏幕上?我环顾四周,对整个 Android 渲染过程感到非常困惑。

我的对象不是列表。它是一棵树。我还阅读了有关适配器、布局和视图的内容,但不明白。这是我的代码:

public class GeoArea {

    public String id;
    public String name;
    public String searchLocation;
    public static String searchTerm;
    public List<GeoArea> subGeoAreas;
    public GeoArea parentGeoArea;

    public GeoArea(String id) {
        this.id = id;
        name = id;
        searchLocation = "";
        subGeoAreas = new LinkedList<GeoArea>();
    }

    @Override
    public String toString() {
        return name;
    }

    public int size(){
        int result = 0;
        for(GeoArea curGeoArea:subGeoAreas){
            result += curGeoArea.size();
        }
        return result + 1;
    }
}

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activity_GeoAreas" >

    <TextView
        android:id="@+id/textMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Choose Suburb"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:id=""@+id/geoAreasLayout""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textMain"
        android:orientation="vertical" >
    </LinearLayout>


</RelativeLayout>

所以想法是将主GeoArea渲染成geoAreasLayout,由于它是一棵树,每个subGeoArea都会依次渲染。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_geo_areas);

    GeoArea.searchTerm =  "Bar & Grill";

    GeoArea torontoArea = new GeoArea("cityOfToronto");
    torontoArea.name =  "City of Toronto";
    torontoArea.searchLocation =  "Toronto, ON";

    GeoArea testGeoArea = null;

    testGeoArea = new GeoArea("DownTownCore");
    testGeoArea.name =  "Down Town Core";
    testGeoArea.searchLocation =  "Downtown Core, Toronto, ON";
    testGeoArea.parentGeoArea = torontoArea;
    torontoArea.subGeoAreas.add(testGeoArea);

    testGeoArea = new GeoArea("AlexandraPark");
    testGeoArea.name =  "Alexandra Park";
    testGeoArea.searchLocation =  "Alexandra Park, Toronto, ON";
    testGeoArea.parentGeoArea = torontoArea;
    torontoArea.subGeoAreas.add(testGeoArea);

    // what's next?

    }

}

我尝试编写一个适配器,但我什至不确定它的用途或如何使用它:

public class GeoAreaAdapter extends BaseAdapter implements Filterable{
    private Context _context;   
    private int resource;
    private GeoArea _myGeoArea;

    public GeoAreaAdapter(Context context, int resource, GeoArea myGeoArea) {
        _context = context;
        _myGeoArea = myGeoArea;
    }

    public int getCount() {
        return _myGeoArea.size();
    }

    public Object getItem(int position) {
        return _myGeoArea;
    }

    public long getItemId(int position) {
        return _myGeoArea.id.hashCode();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout GeoAreaView;
        if (convertView == null) { 
            GeoAreaView = new LinearLayout(_context);
            LayoutInflater li = LayoutInflater.from(_context);
            convertView = li.inflate(R.layout.layout_geo_area, null);
        }
        else {
            GeoAreaView = (LinearLayout) convertView;
        }

        TextView name = (TextView) GeoAreaView.findViewById(R.id.txtGeoAreaName);
        name.setText(_myGeoArea.name);

        return convertView;
    }

    static class ViewHolder { 
        TextView name;
        RelativeLayout childContainer;
    }

    @Override
    public Filter getFilter() {
        return null;
    }
}
4

1 回答 1

1

首先,您的类必须从 Views 或 ViewGroups 类之一扩展以在 LinearLayout 中呈现和使用它。像这样:

public class GeoArea extends View{

根据此问题,您可以在 xml 布局文件中使用它,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <com.example.YourClassName
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:id="@+id/mapview"
        ></com.example.YourClassName>
</LinearLayout>

如您所见,您必须使用包名称在 xml 布局文件中呈现新对象。

于 2013-11-10T18:26:03.373 回答