Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用LayoutFlator添加多个视图_Java_Android_Xml - Fatal编程技术网

Java 使用LayoutFlator添加多个视图

Java 使用LayoutFlator添加多个视图,java,android,xml,Java,Android,Xml,我试图在android中动态创建一个“收藏夹”视图,方法是记录用户是否喜欢我的应用程序中的特定音频片段,然后将子视图插入到父视图中,并包含特定音频片段的适当信息。基本上,创建一个自定义的声音片段播放列表 不过,我在排班方面遇到了一些问题。如果在父视图中添加多个子视图,则只有最后添加的子视图才会输入适当的参数,而其余的子视图则具有子视图xml布局中的默认值 下面是我的java代码,展示了添加子视图的基本思想: LayoutInflater l = getLayoutInflater(); Line

我试图在android中动态创建一个“收藏夹”视图,方法是记录用户是否喜欢我的应用程序中的特定音频片段,然后将子视图插入到父视图中,并包含特定音频片段的适当信息。基本上,创建一个自定义的声音片段播放列表

不过,我在排班方面遇到了一些问题。如果在父视图中添加多个子视图,则只有最后添加的子视图才会输入适当的参数,而其余的子视图则具有子视图xml布局中的默认值

下面是我的java代码,展示了添加子视图的基本思想:

LayoutInflater l = getLayoutInflater();
LinearLayout ll = (LinearLayout)findViewById(R.id.favlist);

if(getIntent().getExtras()!=null)
{
    Bundle extras = getIntent().getExtras();
    Intent test = getIntent();

    if(test.hasExtra("favThunder")){
        String favValues = extras.getString("favThunder");

        View v = l.inflate(R.layout.sublayout, null);
        ll.addView(v);

        ImageView favImage = (ImageView)findViewById(R.id.favimage);
        favImage.setImageResource(R.drawable.thundercats);

        TextView tv = (TextView) findViewById(R.id.favtitle);
        tv.setText(R.string.thundercats);

        TextView tvClicks = (TextView) findViewById(R.id.timesplayed);
        tvClicks.setText("Times played: " + favValues);

        favImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp.isPlaying()) {
                    mp.stop();
                }

                mp = MediaPlayer.create(getApplicationContext(), R.raw.thunder);
                mp.start();
            }
        });


    }

    if(test.hasExtra("favSkeletor")){
        String favValues = extras.getString("favSkeletor");

        View v = l.inflate(R.layout.sublayout, null);
        ll.addView(v);

        ImageView favImage = (ImageView)findViewById(R.id.favimage);
        favImage.setImageResource(R.drawable.skeletor);

        TextView tv = (TextView) findViewById(R.id.favtitle);
        tv.setText(R.string.skeletor);

        TextView tvClicks = (TextView) findViewById(R.id.timesplayed);
        tvClicks.setText("Times played: "+favValues);


        favImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp.isPlaying()) {
                    mp.stop();
                }

                mp = MediaPlayer.create(getApplicationContext(), R.raw.skeletor);
                mp.start();
            }
        });
    }
这是我的子视图和父视图xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/favimage"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="200dp"
        android:src="@drawable/thundercats"
        android:scaleType="fitCenter"/>

    <LinearLayout
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/favtitle"
            android:layout_weight="0.4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A TEST TITLE"/>

        <TextView
            android:layout_weight="0.3"
            android:id="@+id/timesplayed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Times played: Heck!"/>

    </LinearLayout>

</LinearLayout>

父xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context="com.example.markohare.ohareb00716779_cw1.MainActivity"
    >

    <LinearLayout
        android:id="@+id/favlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

    </LinearLayout>

</ScrollView>

我做错了什么?我的子视图是否太复杂,或者我的java代码中是否有错误


感谢您在
活动中调用
findViewById()
,因此它将返回使用这些ID找到的第一个
视图。在您正在充气的
视图中调用它,以获得特定于该视图的子
视图。例如,
v.findViewById(…)
。这很有魅力!非常感谢你!