listView显示在Android Studio XML窗口中,但是当我在GenyMotion中运行时,listView应该是空的。我尝试添加虚拟值但没有运气。
add_device.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#ffffff"
android:weightSum="1">
<EditText
android:layout_width="320dp"
android:layout_height="55dp"
android:id="@+id/deviceName"
android:background="#c0c0c0"
android:layout_marginTop="30dp"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="@dimen/abc_dialog_padding_material"
android:hint="DEVICE NAME"
android:layout_weight="0.73"
android:editable="true"
android:elegantTextHeight="false"
android:enabled="true"
android:focusable="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listOfDevices"
android:layout_marginTop="30dp"
android:layout_below="@+id/deviceName"
android:clickable="true"
android:divider="#33cc00"
android:dividerHeight="2dp"
android:drawSelectorOnTop="false"
android:fastScrollAlwaysVisible="false"
android:headerDividersEnabled="true"
android:choiceMode="singleChoice"
android:contextClickable="true"
tools:listitem="@android:layout/simple_list_item_1"
android:layout_marginBottom="140dp"
android:listSelector="#33cc00"
android:visibility="visible" />
<Button
android:layout_width="@dimen/abc_config_prefDialogWidth"
android:layout_height="55dp"
android:text="CONFIRM"
android:id="@+id/confirmBtn"
android:layout_weight="0.76"
android:background="#33cc00"
android:textSize="25dp"
android:textColor="#ffffff"
android:layout_marginBottom="22dp"
android:layout_above="@+id/companyTagName"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="PereGreen Energy Ltd"
android:id="@+id/companyTagName"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ListViewAndroidExample.Java
public class ListViewAndroidExample extends Activity {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_device);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.listOfDevices);
// Defined Array values to show in ListView
String[] values = new String[] { "Test 1",
"Test 2",
"Test 3",
"Test 4",
"Test 5",
"Test 6",
"Test 7",
"Test 8"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
}
});
}
}
这是我在运行模拟器时看到的内容。
答案 0 :(得分:1)
List <String> sampleValues= new ArrayList<String>(Arrays.asList(values));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, sampleValues);
// Assign adapter to ListView
listView.setAdapter(adapter);
如有任何疑问,请与我们联系。