0

私は文字列ファイルからTutorialTitelsを取得するListViewを持っています

public class tutorialActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tutorial);
        registerClickCallBack();
        ListView listView = (ListView) findViewById(R.id.tutorialList);

        String tutorialTitle1 = getResources().getString(R.string.tutorial1_title);
        String tutorialTitle2 = getResources().getString(R.string.tutorial2_title);
        String tutorialTitle3 = getResources().getString(R.string.tutorial3_title);
        String tutorialTitle4 = getResources().getString(R.string.tutorial4_title);
        String tutorialTitle5 = getResources().getString(R.string.tutorial5_title);
        String tutorialTitle6 = getResources().getString(R.string.tutorial6_title);
        String tutorialTitle7 = getResources().getString(R.string.tutorial7_title);
        String tutorialTitle8 = getResources().getString(R.string.tutorial8_title);
        String tutorialTitle9 = getResources().getString(R.string.tutorial9_title);
        String tutorialTitle10 = getResources().getString(R.string.tutorial10_title);
        String tutorialTitle11 = getResources().getString(R.string.tutorial11_title);
        String tutorialTitle12 = getResources().getString(R.string.tutorial12_title);
        String tutorialTitle13 = getResources().getString(R.string.tutorial13_title);
        String tutorialTitle14 = getResources().getString(R.string.tutorial14_title);

        String[] values = new String[] { tutorialTitle1, tutorialTitle2, tutorialTitle3, tutorialTitle4, tutorialTitle5, tutorialTitle6, tutorialTitle7, tutorialTitle8, tutorialTitle9, tutorialTitle10, tutorialTitle11, tutorialTitle12, tutorialTitle13, tutorialTitle14};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
        listView.setAdapter(adapter);
    }

    private void registerClickCallBack() {
        ListView list = (ListView) findViewById(R.id.tutorialList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {

        }
    });
}
}

listViewItem をクリックすると、次の内容を表示するアクティビティを開きたいと思います。

  • クリックされた TutorialTitel

  • チュートリアル コンテンツ (これも文字列ファイルから取得されます。そのように String tutorialContent1 = getResources().getString(R.string.tutorial1_content);)

  • チュートリアルの例 ((これも文字列ファイルから取得されます String tutorialExample1 = getResources().getString(R.string.tutorial1_example);))

私はすでにそのようなXMLファイルを持っています

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" >

            <TextView
                android:id="@+id/tutorialTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Title"
                android:textSize="25sp" />

            <LinearLayout
                android:id="@+id/split"
                android:layout_width="fill_parent"
                android:layout_height="2dip"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:layout_marginTop="5dp"
                android:background="#38b34a"
                android:orientation="horizontal" />


            <TextView 
                android:id="@+id/tutorialContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:text=""/>


            <TextView 
                android:id="@+id/tutorialExample"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:text=""/>


        </LinearLayout>
    </RelativeLayout>

</ScrollView>

質問: クリックしたアクティビティにデータを渡すにはどうすればよいListviewItemですか? 私は何かをする必要があります

if(position == 0){
//Send data through extra bundle
}
else if(position == 1){
//send data through extra bundle
} 

しかし、私が考えるより良い方法があるはずですが、正確にはわかりません.100のチュートリアルを取得するとしたら、そのような長いリストをどのように管理すればよいでしょうか? 誰かが私を正しい方向に向けることができますか?これを行うための最良のアプローチは何ですか?

4

3 に答える 3

1

ListViewこのリスナー コールバックを使用してリスナーを追加すると、クリックされたリスト項目の位置を示す位置setOnItemClickListener();として 3 つのパラメーターが 与えられます。onItemClick (AdapterView<?> parent, View view, int position, long id)

したがって、これpositionを使用して as values[position]and same for tutorialContentandを使用してtutorialExampleそれを渡し、選択した項目データを持つこのインテントintentから開始できますActivity

これは、問題の迅速な解決策です。しかし、あなたが実装した方法はオブジェクト指向の標準にないため、お勧めしません。

Tutorial メンバー変数のタイトル、コンテンツ、および例を持つモデル クラスを作成することをお勧めします。

Class Tutorial{
    private String title;
    private String content;
    private string example;
}

このオブジェクトをインテントに渡します。

于 2013-06-10T16:16:32.157 に答える