当我在布局中将 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等,那是正常的。编译您的项目并在设备上运行它,所有这些都必须正常工作。