-4

私はこの優れたサイトと Java プログラミング Android を初めて使用します。私は自分の町のお気に入りの場所をリストするための小さなテスト アプリケーションを作成し始めました。さまざまなページでいくつかのチュートリアルに従おうとしましたが、プロジェクトを Eclipse に入れると、クラスやその他のメソッドをインポートしても、常に 100 万を超える間違いが発生します。

イメージ名 discos の横にある例のイメージ discotheque に基づいて作成し、その名前の下に Diskotek 小さいテキストの追加情報を作成します。

すべての助けに本当に感謝します

4

2 に答える 2

0

誰かが「いくつかのチュートリアルに従おうとした...」と言ったときに最初に思うことですが、うまくいきません。信じがたいことです。

  • 試したコードはどこにありますか?
  • エディタのインポート エラーは何ですか?

その方が解決しやすい問題です。

簡単な 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 を介してレイアウト リソースを設定する必要がないため、ここでは呼び出しません。

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 パターンと何とか何とかを調べたいと思うかもしれません。もちろん、もっと読む必要がありますが、これが出発点として役立つことを願っています。

于 2013-06-04T21:21:38.177 に答える