I have included my code to help clear things up. Basically I have two screens on my app, one will display a list, and the other will allow the user to add a new item to that list. Eventually I will incorporate database data to save the list view, but I do not require help with that. I have not yet coded the second screen, so I have not included the code for it.
The problem is that I cannot get the list view to display. I have even tried adding data to the array through code, with no luck. Can anyone see a problem here?
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>
Actually, I can't see any problem in your code. I will give you two things I have noticed.
1)Instead of listing your data into an ArrayList
use String[]
2)You don't have to call notifyDataSetChanged()
when you first give the adapter
the input data. Only when you change the list of data later after. So instead of re-initialize the adapter, just call notifyDataSetChanged
Give this code a try and let me know if you still have a problem.
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);
}
And use this .xml instead of yours
<?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>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments