我已将我的代码包含在内以帮助您解决问题。基本上我的应用程序上有两个屏幕,一个将显示一个列表,另一个将允许用户将新项目添加到该列表。最终我将合并数据库数据以保存列表视图,但我不需要帮助。我还没有编写第二个屏幕,所以我没有包含它的代码。
问题是我无法显示列表视图。我甚至尝试通过代码向数组添加数据,没有运气。谁能在这里看到问题?
Issues.java
public class Issues extends Activity {
public static ArrayList<String> strArray;
public static ArrayAdapter<String> arrayAdapter;
public static ListView serieslistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.issues);
strArray = new ArrayList<String>();
Button btaddnewseries = (Button) findViewById(R.id.button1);
serieslistview = (ListView) findViewById(R.id.listView1);
//opens the add new series menu
btaddnewseries.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(ComicIssues.this, AddSeries.class);
Issues.this.startActivity(myIntent);
}
});
strArray.add("Item One");
arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, strArray);
serieslistview.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
issues.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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="+"
tools:ignore="HardcodedText" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp" >
</ListView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="21dp"
android:text="Issues"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
答案 0 :(得分:0)
实际上,我在代码中看不到任何问题。我会告诉你我注意到的两件事。
1)而不是将您的数据列入ArrayList
使用String[]
2)当您第一次提供notifyDataSetChanged()
输入数据时,您不必致电adapter
。仅在您稍后更改数据列表时。因此,只需调用notifyDataSetChanged
尝试使用此代码,如果您仍有问题,请告诉我。
public class Issues extends Activity {
public static ArrayAdapter<String> arrayAdapter;
public static ListView serieslistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.issues);
Button btaddnewseries = (Button) findViewById(R.id.button1);
serieslistview = (ListView) findViewById(R.id.listView1);
//opens the add new series menu
btaddnewseries.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(ComicIssues.this, AddSeries.class);
Issues.this.startActivity(myIntent);
}
});
String[] values = new String[] { "Item 1",
"Item 2",
"Item 3",
"Item 4"
};
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,values);
serieslistview.setAdapter(arrayAdapter);
}
并使用此.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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="+"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:text="Issues"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@+id/textView1" />
</RelativeLayout>