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 渲染器中看到 item1 等,那是正常的。编译您的项目并运行它在设备上,并且一切都必须正常工作。
我在布局中将 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 渲染器中看到 item1 等,那是正常的。编译您的项目并运行它在设备上,并且一切都必须正常工作。