我是Android Studio新手。对我来说listview存在问题。我已正确设置所有ID,但问题仍然存在。
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.prashanthwagle.myapplication.MainActivity">
<ListView
android:id="@+id/sample"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
</RelativeLayout>
爪哇:
package com.example.prashanthwagle.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] s={"asfasaf","bafaf","cbfbd"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.sample);
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s);
listView.setAdapter(a);
}
}
答案 0 :(得分:1)
我认为你应该改变
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s)
到
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R. android.R.layout.simple_list_item_1,s);
答案 1 :(得分:0)
您为ArrayAdapter设置了错误的布局。使用此:
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,s);
适配器初始化和赋值中的布局引用不应该是活动的布局引用。基本上,此参数告诉ListView列表的每一行具有哪种类型的布局。从这里了解更多相关信息:https://developer.android.com/guide/topics/ui/layout/listview.html