0

Android開発は初めてです。Java でカスタム オブジェクトを作成しました。画面にレンダリングするにはどうすればよいですか? 私は周りを見回して、Android のレンダリング プロセス全体について本当に混乱しています。

私のオブジェクトはリストではありません。木です。Adapter、Layout、View についても読みましたが、理解できません。これが私のコードです:

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.like でレンダリングして使用する必要があります。

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 に答える