Hej Alle sammen
I mit android projekt har jeg lavet en Activity som nedarver fra klassen ListActivity. Selve min activity bruges til at vise noget simpelt tekst data i et ListView, men denne view har ikke nogen Button control, da jeg ikke ønsker dette, vil meget hellere have at brugeren skal kunne trykke/klikke en gang på skærmen (lige meget hvor) med sin finger, for at denne activity skal afslutte/lukke. Har prøvet at lave en onClickListener() event, for selve activityen, men denne er ikke at finde nogen steder for ListActivity, så hvordan får jeg lavet denne listener?
Jeg har allerede lavet en onListItemClick() event, men denne event kører kun når jeg klikker på et list item, og min activity kan være tom for information (Det er en highscore liste). Min kode ser således ud indtil nu - Undskyld rodet, men den er ikke færdigudviklet
- package dk.rohwedder.game;
-
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
-
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.WindowManager.LayoutParams;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- /**
- * This is the Highscore activity. This activity is a List View Activity,
- * and therefore the responsibility of this activity, is to display highscore
- * data in a list.
- *
- * @author Martin Rohwedder
- * @version 1.0
- */
- public class HighscoreActivity extends ListActivity {
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- Highscore highscore = null;
-
- try {
- //Read the file
- FileInputStream fis = openFileInput("highscore.game");
- ObjectInputStream input = new ObjectInputStream(fis);
-
- highscore = (Highscore) input.readObject();
-
- input.close();
- }
- catch (FileNotFoundException ex)
- {
- highscore = new Highscore();
- }
- catch (IOException e) {
- Toast.makeText(getBaseContext(), "An IO Exception Has Occured", Toast.LENGTH_LONG).show();
- }
- catch (ClassNotFoundException ex) {
- Toast.makeText(getBaseContext(), "An Class Not Found Exception Occured", Toast.LENGTH_LONG).show();
- }
-
- //Populate the players array with all players on the highscore.
- String[] players = highscore.getHighscoreAsStringArray();
-
- //Set this list view with the players array
- setListAdapter(new ArrayAdapter<String>(this, R.layout.highscore, players));
-
- //make the list view fill phone screen
- getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
- }
-
- /**
- * Closes the List View when a user clicks on an item
- */
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- finish();
- }
-
- /**
- * Finish the activity on a key up event
- */
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- finish();
- return true;
- }
- }
Indlæg senest redigeret d. 20.03.2012 10:24 af Bruger #4487