我的问题是列表视图在我的Android模拟器上不可见 下面是我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MyListview">
<ListView
android:id="@+id/listview"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
tools:ignore="MissingConstraints" />
</LinearLayout>
下面是我的string.xml文件
<resources>
<string name="app_name">MyListView</string>
<string-array name="myarray">
<item value="Abc"></item>
<item value="Def"></item>
<item value="ghi"></item>
<item value="jkl"></item>
</string-array>
下面是my.java文件:
package com.example.mypc.mylistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MyListview extends AppCompatActivity {
ListView lv;
ArrayAdapter ad;
String []arr={"abc","def","ghi","jkl"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_listview);
lv=(ListView) findViewById(R.id.listview);
ad=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,arr);
lv.setAdapter(ad);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String []arr=getResources().getStringArray(R.array.myarray);
String value=arr[position];
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_LONG).show();
}
});
}
}
下面是我的输出:
这是在数组适配器内部传递数组之后的输出
我的java文件中是否存在任何错误或布局错误? 预先感谢
答案 0 :(得分:0)
如您在屏幕截图中所见,一切都很好。列表视图在那里,但是 android.R.layout.simple_list_item_1 的颜色是白色,这就是为什么它不显示的原因。您可以尝试
例如mytextview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/font_content"
android:padding="5sp"
android:layout_width="fill_parent"
android:background="@drawable/rectgrad"
android:singleLine="true"
android:gravity="center"
android:layout_height="fill_parent"/>
您的Java文件现在为:
package com.example.mypc.mylistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MyListview extends AppCompatActivity {
ListView lv;
ArrayAdapter ad;
String []arr={"abc","def","ghi","jkl"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_listview);
lv=(ListView) findViewById(R.id.listview);
ad=new ArrayAdapter<String>(getApplicationContext(),R.layout.mytextview,arr);
lv.setAdapter(ad);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String []arr=getResources().getStringArray(R.array.myarray);
String value=arr[position];
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_LONG).show();
}
});
}
}
它绝对有效。
答案 1 :(得分:0)
Simple use this in MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=findViewById(R.id.listviewa);
String [] value=getResources().getStringArray(R.array.radio_buttons);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, value);
listView.setAdapter(adapter);
}
//---------------------String.xml-
<resources>
<string name="app_name">YourAppName</string>
<string-array name="radio_buttons">
<item>ABC</item>
<item>DEF</item>
<item>GHX</item>
<item>IJK</item>
</string-array>
</resources>