0

I have a ListView that gets TutorialTitels from the string file like so

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) {

        }
    });
}
}

When I click on a listViewItem then I'd like to open up an Activity that'll show the following things:

  • The clicked TutorialTitel

  • Tutorial content (This will also come from a string file, like so String tutorialContent1 = getResources().getString(R.string.tutorial1_content);)

  • Tutorial example ((This will also come from a string file, like so String tutorialExample1 = getResources().getString(R.string.tutorial1_example);))

I already have an XML file like so

<?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>

The question: How do I pass the data to my activity corresponding to my clicked ListviewItem? should I do something like

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

But there should be a better way I think, but I don't know how exactly, because what if I'll getting 100 tutorials how should I manage a list long like that? Can someone point me in the correct direction and what is the best approach to do this?

4

3 回答 3

1

您可以添加一个侦听器来ListView使用setOnItemClickListener();此侦听器回调将提供三个参数,即 onItemClick (AdapterView<?> parent, View view, int position, long id)在哪个位置将给出单击的列表项的位置。

所以使用这个position你可以使用 asvalues[position]和 same for tutorialContentandtutorialExample并传入它intent并从Activity这个 Intent 开始,具有选定的项目数据

这是您的问题的快速解决方案。但我不会推荐,因为您实现的方式不在面向对象的标准中。

我建议您创建一个Tutorial 具有成员变量标题、内容和示例的模型类。

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

并在意图中传递这个对象。

于 2013-06-10T16:16:32.157 回答
0

您可以使用enum. 例如,您可以Tutorials像这样定义枚举:

public enum Tutorials
{
    TUTORIAL_1(R.string.tutorial1_title, R.string.tutorial1_content, R.string.tutorial1_example),
    TUTORIAL_2(R.string.tutorial2_title, R.string.tutorial2_content, R.string.tutorial2_example),
    TUTORIAL_3(R.string.tutorial3_title, R.string.tutorial3_content, R.string.tutorial3_example);

    private int mTitleResourceId;
    private int mContentResourceId;
    private int mExampleResourceId;

    private Tutorials(int titleResourceId, int contentResourceId, int exampleResourceId)
    {
        mTitleResourceId = titleResourceId;
        mContentResourceId = contentResourceId;
        mExampleResourceId = exampleResourceId;
    }

    public int getTitleResourceId()
    {
        return mTitleResourceId;
    }

    public int getContentResourceId()
    {
        return mContentResourceId;
    }

    public int getExampleResourceId()
    {
        return mExampleResourceId;
    }
}

如果您愿意,可以在此枚举中定义 100 个教程。然后在您的活动中,您可以像这样以通用方式使用它:

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

    List<String> titlesList = new ArrayList<String>();
    for (Tutorials tutorial: Tutorials.values())
    {
        titlesList.add(getResources().getString(tutorial.getTitleResourceId()));
    }
    String[] values = titlesList.toArray(new String[titlesList.size()]);

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

最后点击列表中的项目,您可以轻松地通过其字符串标题检索教程枚举,然后您将免费获得教程内容和教程示例。

设置此代码后,您将仅向Tutorials枚举添加教程,仅此而已。

希望能帮助到你。当然,使用 sqlite 也是一种选择。

于 2013-06-10T15:47:35.427 回答
0

首先将字符串保存在数组文件 (array.xml) 中。然后将它们读入一个字符串数组;

String [] myresources=resources.obtainTypedArray(R.array.somelist);

    for(int i=0;i<NumberOfTutorials;i++){
        if(position==i){
            //send myresources[i];
     }

}
于 2013-06-10T15:20:40.793 回答