繁体   English   中英

Android:ArrayAdapter不适用于ListView

[英]Android: ArrayAdapter not being applied to ListView

在布局中将ArrayAdapter设置为ListView时遇到问题。 一切看起来都很好,并且没有任何错误,但是当我加载XML文件时,在列表视图中看到的只是item1,item2等。 有没有我没有添加的内容吗?

这是我的代码如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LaunchActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launch);

    ListView lv = (ListView) findViewById(R.id.subjectListView);

    String[] subjectArray = new String[]{"Physics","Chemistry","Economics", "Geography"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subjectArray);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                    if (position == 0) {
                        Intent intent = new Intent(LaunchActivity.this, PhysicsFragment.class);
                        startActivity(intent);
                    } else if (position == 1) {
                        Intent intent = new Intent(LaunchActivity.this, ChemFragment.class);
                        startActivity(intent);
                    } else if (position == 2) {
                        Intent intent = new Intent(LaunchActivity.this, EconFragment.class);
                        startActivity(intent);
                    } else if (position == 3) {
                        Intent intent = new Intent(LaunchActivity.this, GeoActivity.class);
                        startActivity(intent);
                    }

                }
            }


    );

}


}

这是我的XML文件:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:id="@+id/subjectListView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

如果您的意思是在Android Studio的XML Renderer中看到item1等,那是正常现象。 编译项目并在设备上运行,所有项目都必须正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM