0

現在、約 20 のトピック (文字列) のリストを表示する Android ListView クラスがあります。リストの各ボタンをクリックして、そのボタンでそのトピックに固有のビューを開くことができるようにする必要があります。

たとえば、これがレシピ リストの場合、すべてのレシピ ビューのレイアウトは同じにすることができますが、ユーザーがリストから特定のレシピをクリックすると、プログラムはそのレシピを共通のレイアウトにロードして、そのビューへのユーザー。

OnItemClickListener は機能していると思いますが、残りを実装する方法がわかりません。

レシピごとに新しいアクティビティとレイアウトが必要ですか? 何十もの同一のレイアウトとアクティビティ ファイルを作成せずにこれを実装する簡単な方法はありますか?

また、ビューにレシピを入力するにはどうすればよいですか?

役に立つ考えをありがとう!

--- 関連コード: Listview アクティビティ コード

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, studiesList);  

    // Set the ArrayAdapter as the ListView's adapter.  
    mainListView.setAdapter( listAdapter );    
    mainListView.setClickable(true);
    mainListView.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> a, View view, int position, long id) { 

            switch( position )
            {
               case 0:  Intent intent = new Intent(StudyActivity.this, pos.class); 
                        startActivity(intent);
                        break;

SimpleRow.xml ファイル: (リストのボタン)

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</Button>
4

2 に答える 2

0

あなたがやりたいと思うことは、標準の「レシピ」ビューを持つことができる新しいアクティビティでレシピを開くことです。

新しいアクティビティにデータを渡すには、新しいアクティビティを起動するインテントにエクストラを追加できます (インテントとインテント フィルター - API ドキュメントのエクストラを参照)。必要なレシピを識別する int または String を渡すことができます。

インテントでエクストラを渡すための基本的な概要は次のとおりです。

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("EXTRA_ID", data);
startActivity(intent);

次に、新しいアクティビティでこれらの値を取得できます。

Bundle extras = getIntent().getExtras();
if(extras.hasExtra("EXTRA_ID")) {
    int value = extras.getString("EXTRA_ID");
}

その値を使用して、データを取得しているソースからレシピをロードすると、準備が整います!

于 2012-07-16T19:01:02.847 に答える
0

必要なことは、いくつかの属性を持つシリアル化可能なレシピ クラスを作成し、20 個のレシピのそれぞれに対してそのクラスの新しいオブジェクトを作成することです。

私はあなたが次のようなものを持っていると思います

public class Recipe extends Serializable{ 
private String name; 
private String ingredients; 

public Recipe(String name, String ingredients){
this.name = name;
this.ingredients = ingredients;
}

}

次に、これらのオブジェクトの配列リストを作成します

ArrayList<Recipe> recipes = new ArrayList<Recipe>();
recipes.add(new Recipe("Chicken Curry", "Random cooking instructions"));

リストアダプターでそのarraylistを使用します。

次に、 onItemClickListener で次のようなものが必要です

Intent i = new Intent(this, recipeDisplay.class)
i.putExtra("recipe",  listAdapter.getItemAtPosition(position)); 

レシピ表示クラスでは、インテントを受け取り、オブジェクトを使用してアクティビティ フィールドにデータを入力するだけです。

Intent intent = getIntent():
intent.getSerializableExtra("recipe");
于 2012-07-16T19:11:58.613 に答える