I am trying to display a list after clicking a button on Android.
This is my code:
//Button after clicking should display a list
final Button button4 = (Button) findViewById(R.id.widget30);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), TestListActivities.class);
startActivity(myIntent);
}
public class TestListActivities extends Activity {
String[] listItems = {"Game1", "Game2",
"Game3", "Game4"};
public ListView la;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
la=(ListView)findViewById(R.id.widget50);
la.setAdapter(new ArrayAdapter<String>(this,
R.layout.main2, listItems));
}
}
My XML file is:
?xml version="1.0" encoding="utf-8"?
AbsoluteLayout
android:id="#+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
ListView
android:id="#+id/widget50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
AbsoluteLayoute
But when I click the button, the application is forced to close and it crashes.
Manifest file
<application android:icon="#drawable/icon" android:label="BAM! Pong" android:debuggable="true">
<activity android:name=".GUI" android:label="#string/app_name" android:screenOrientation="portrait"><intent-filter><action android:name="android.intent.action.MAIN"></action>
It's difficult to know without seeing the stacktrace. Run "adb logcat" from the command line while running your app or watch the logs in the DDMS perspective in Eclipse. The stacktrace will give you the location of the bug or reason for the FC.
You seem to use the R.layout.main2 both as the activity layout (setContentView(R.layout.main2);) and as the ListView layout. (a.setAdapter(new ArrayAdapter(this, R.layout.main2, listItems));) and that is not the right way to use the ListView, the Activity layout should be a layout that contains a ListView, while the layout you are giving to the ArrayAdapter should be a different layout containing the views you would like to be repeated for each entry in your list.
Take a look at the API demos in the android sdk, there are several ListView examples that show you how to use the ListView.
Have you declared Activity in AndroidManifest?
Related
Good day to everyone, I am new to Android, I keep on messing with ListView. The code below wont work! I dont know why. Please let me know where's the mistake.
Main Activity.java
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> data = new ArrayList<String>();
data.add("Follow Me!");
data.add("This is now.");
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</LinearLayout>
After running the above code, my adroid device will show a dialogue saying:
"Sorry!
The application CodeGenerator has stopped unexpectedly. System failed to repair the error. Please use other software."
Please help!
The setContentView(R.layout.activity_main); line should be immediately after super.onCreate(savedInstanceState);.
What this
setContentView(R.layout.activity_main);
line does is it basically displays the screen (your android xml to the user). Now when you do
ListView listView = (ListView) findViewById(R.id.listView);
You are trying to get your ListView from your xml in your java code. But if you dont set the content itself, then there is no ListView and hence your application is crashing.
Anyway, I changed the code, and its running now. Here's the code:
public class MainActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> data = new ArrayList<String>();
data.add("Follow Me!");
data.add("This is now.");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
setListAdapter(adapter);
}
}
Still a mystery for me, why my first code did not work! If anyone can explain that, please let me know! Thank you!
I am creating an application that saves GPS coordinates; I have created a button which launches a new Activity to display, as a list all the save coordinates. The program crashes a soon as I hit the button, here is what I am doing.
class 1:
public void openLatLonData(View view)
{
Intent intent = new Intent(this, GpsDataList.class);
ArrayList<String> allLocalStrings = new ArrayList<String>();
for(int i =0; i< AllLocals.size();i++)
{
allLocalStrings.add(AllLocals.get(i).getLat());
allLocalStrings.add(AllLocals.get(i).getLon());
}
intent.putStringArrayListExtra("data", allLocalStrings);
startActivity(intent);
}
This method calls the new activity:
public class GpsDataList extends ListActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle bundleIn)
{
super.onCreate(bundleIn);
Intent intent = getIntent();
ArrayList<String> listData = new ArrayList<String>();
listData = intent.getStringArrayListExtra("data");
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listData));
getListView().setTextFilterEnabled(true);
//finish();
}
}
the button contains the following XML:
<Button android:layout_height="wrap_content"
android:text="Show Collected Data"
android:id="#+id/view_lat_lon_data"
android:layout_width="wrap_content"
android:onClick="openLatLonData">
</Button>
And I have added a new .xml file named gpsdatalist.xml under the res/layout folder:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
Lastly the manifest contains this: .
I believe I have all the pieces in play- Hopefully someones see the mistake
Thanks in advance guys!
Did you define the activity in the AndroidManifest.xml file?
Anyway, your best clue resides in the logcat of your exception. You will solve it in 2 seconds after cheking the logcat.
First all i'm new to Android programming, so my level of coding isn't too great :p
I have a deck of cards class in which I want to return the value of the card drawn into a text view.
cardValue = "The " + numString + " of " + suitString;
return cardValue;
This is the end of the method drawCard in my deck class.
In my virtualDeck class, I have created an instance of this class,
final textdeck deck = new textdeck();
String value = deck.drawCard();
What i'm trying to do is create a button on the virtualDeck class, that once clicked will run the drawCard method from the textDeck class, and return the result into a textView. With each time the button is clicked, having the textView change to print the new value.
I can quite seem to get my head around how to do this.
Any help is much appreciated.
Cheers.
I hope this example of how to setup a TextView helps:
In your main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/card"/>
</LinearLayout>
In your source (TextViewExample.java)
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TextViewExample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Do all your deck setup here
String value = "8 of Spades";
//New textView
TextView cardDisplay = (TextView)findViewById(R.id.card);
//Update Text
cardDisplay.setText(value);
}
}
I don't quite understand what you are trying to do, but this is how you change the view via button:
Button nextButton =(Button)findViewById(R.id.next);
nextButton.setOnClickListener(this);
Where next is the Button created inside the current layout.
Then in the onclick listener:
Intent intent = new Intent(this,SecondIntent.class);
startActivity(intent);
finish();
and SecondIntent is the name of the second class you are calling. That class can have its own view because it can call setContentView().
The only thing is to make sure that you open up the AndroidManifest.xml and add the following in the tag
<activity android:name=".SecondIntent"
android:label="#string/app_name">
</activity>
Adding that to the manifest will let android know that the other activity exists.
Now if you want to open the second view and be able to close the second view to return to the first, only add finish() to the second activity and not the first.
Good luck!
-A Fellow Android Noob
I've created a layout in XML and it includes a ListView. I can use the ListView just find inside my AsyncTask. In onCreate, I did: listView = (ListView)findViewById(R.id.listView1);
And in onPostExecute of my AsyncTask, I did: listView.setAdapter(new OfferAdapter(Main.this, offers)); THIS WORKS JUST FINE.
But if I try listView.setDivider(null) in onCreate, then the app crashes with a nullpointer there.
How am I supposed to grab hold of my ListView when I'm not using ListActivity?
If findViewById is returning null, I'd make sure you are calling it after setContentView, and that you are using the correct ID.
One option you have is to set the divider in xml, ie:
android:divider="#drawable/icon".
If you want more control, verify that you are following this syntax in your activity:
public class DividerExampleActivity extends Activity {
ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
listView.setDivider(null);
}
}
I've examined the ListView.java source code, and setting it to null should be fine.
Another option might be to make yourself a very thin, transparent divider in xml (say 0.5dp).
I want to know how to open another ListView after one of rows on the previous ListView has been clicked?
Bee VOA reader could be a good example to see what I'm talking about.
List A List B
DeskTop Development ---> Buttons
TextView
ScrollBar
Mobile
Graphic
Game
So there is one list A on the phone's screen, and there are many topics in the list A
If I clicked on one of the topic,let's say DeskTop Development, then the whole ListView
will be slided away from the screen and the new List B will be presented.
So how to implement it?
Opening a new Activity while passing the appropriate data to display the correct information is a good way to do what you're doing. This is a code example of a list of states which goes to a list of cities in the selected state from the previous list. The state listview from the first activity sends the name of the state to the city listview in second activity which displays a list of cities from that state.
private ListView lv;
setContentView(R.layout.displaylayout);
lv = (ListView)findViewById(R.id.DisplayList);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(), DisplayLocationsCities.class);
myIntent.putExtra("state",lv.getItemAtPosition(position).toString());
startActivityForResult(myIntent, 0);
}
});
//////////////////////back button code allowing you to go back to the previous list (close anything that need to be closed in onDestroy()):
this.backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// equivalent of 'return'
finish();
}
});
//////////////////////adding items to your list dynamically:
String [] list = {"New York","Illinois","California","Wisconsin"};
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));
//////////////////////Extra Credit: Layout for each listView item - R.layout.list_item (list_item.xml in the res/layout folder):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:cacheColorHint="#android:color/transparent"
android:textSize="12sp" android:textColor="#000000" android:textStyle="bold" android:gravity="center">
</TextView>
Implement the onListItemClick() method so that on click it starts a new ListActivity. This way the navigation in the menu will be easier as you will be able to use the back button.