Problem with onItemClick
This might be really long and I'm sorry for the poor soul who reads through it all.
Goal: I want to create a Pokemon Pokedex application (Yeah I know very childish). I thought this would be great for a first application to build because there is tons of data to deal with. For those not familiar with Pokemon it's basically creatures in a list from #001 - #649. I want to display all the creatures in a list and have the user click on that creature. Once the creature is clicked on it will display statistics about it on a different screen.
Ok back to programming...
package com.pokemon.pokdex;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
public class PokedexActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.pokemon_titles, R.layout.list_item));
final String[] links = getResources().getStringArray(R.array.pokemon_stats);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String content = links[position];
}
});
}
}
I'm getting errors on getListView().setOnItemClickListener(new OnItemClickListener() {
where is says OnItemClickListener()
and View view
,. it says The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView<?>, View, int, long)
for OnItemClickListener()
and View cannot be resolved to a type for View view,
package com.pokemon.pokdex;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
public class PokedexViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pokemon_stats);
};
}
On this file I'm getting an error on setContentView(R.layout.pokemon_stats);
Here is the error pokemon_stats cannot be resolved or is not a field
My string.xml file looks like (this is where the pokemon will be set as well as the stats. I did the first pokemon below.)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Pokemon Pokedex</string>
<string name="app_name">Pokedex</string>
<string-array name="pokemon_titles">
<item>#001 - Bulbasaur</item>
</string-array>
<string-array name="pokemon_stats">
<item>Grass Type</item>
</string-array>
</resources>
My main.xml looks like
<?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:text="@string/hello"
/>
</LinearLayout>
My list_item.xml looks like
<?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="wrap_content"
android:textSize="24dp"
android:padding="6dp" />
Any help is appreciated and thank you if you made it this far :)
EDIT: Updated and added the exact errors I'm getting.
For your error setContentView(R.layout.pokemon_stats);
pokemon_stats is not a layout. main.xml is your layout. pokemon_stats is a string array, that's your problem. There is no layout called pokemon_stats.xml, I think you want R.layout.main
.
Also, why do you have a semi-colon after }
here?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pokemon_stats);
};
Edit: At the top of your other error, the one with view, you forgot to import android.view.View; add this. You can keep your original code.
import android.view.View;
That should be it, really.
Try now
package com.pokemon.pokdex;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
public class PokedexActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.pokemon_titles, R.layout.list_item));
});
}
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String[] links = getResources().getStringArray(R.array.pokemon_stats);
String content = links[position];
}
}
You don't have any ID on the textView you are using to hold the information, where is this information going to be displayed?
Also, for your resources, your data should really be held in a single array with CSV or something, it will be much simpler than holding different information in different arrays. Parse the data in with tags or by array order, and bind them to your view that way.
Your main.xml is going to be what is displayed here, as well. If you don't have anything defined for main.xml, the program is going to error out. setContentView() sets the content view for the ENTIRE screen. If you want to add an item to a view, use View.addItem(Layout).
Hope this helps. It's the best I can do without any error messages!
精彩评论