Fill ImageAdapter with Int[]
What I've Done
I have created a GridView in my layout. This GridView I'd like to fill over my ImageAdapter I've created of the Google Documentations: Hello GridView
The picturepath to my pictures, did i save into my datbase and I'd like to fill it over it. Because in my Datbase everything is a String I wrote in my Activity a short function which changes the String[] into a Int[], but I can't acces that over my ImageAdapter. Afterwards I applied the changes that Mrs. Nikita told me. Also I changed to lil thing to change the String[] to an int[] (is working now).
Question
What do I need to change to get the Int[] to my ImageAdapter that it imports my pictures and that they are shown in the GrindView. Now I have a Nullpointer Exception at:
public int getCount() {
return mThumbIds.length;
}
The Code:
This is the Code of my SmileyConfigActivity.java:
package de.retowaelchli.filterit;
import de.retowaelchli.filterit.database.SmileyDBAdapter;
import de.retowaelchli.filterit.stats.ImageAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
public class SFilterConfigActivity extends Activity {
//Variablen deklarieren
private String name;
private String keyword;
private Integer[] info;
private SmileyDBAdapter SmileyHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sfilter_config);
SmileyHelper = new SmileyDBAdapter(this);
getImages();
}
public Integer[] getImages(){
SmileyHelper.open();
Cursor c = SmileyHelper.getAllSmileys();
startManagingCursor(c);
String[] picture = new String[] { SmileyDBAdapter.INFO };
int i;
for (i=0;i< picture.length-1;i++) {
info[i]=Integer.parseInt(picture[i+1]);
}
SmileyHelper.close();
//Hier wird die Grindview gefüllt
GridView gridview = (GridView) findViewById(R.id.SmileyGrind);
gridview.setAdapter(new ImageAdapter(this, info));
return info;
}
public void onClickSConfigSave(View v){
EditText edtTextName = (EditText)findViewById(R.id.SFConfigName);
EditText edtTextKeyword = (EditText)findViewById(R.id.SFConfigKeyword);
name = edtTextName.getText().toString();
keyword = edtTextKeyword.getText().toString();
final Intent i = new Intent(this, SmileyActivity.class);
startActivity(i);
}
}
This is the Code of my ImageAdapter.java:
package de.retowaelchli.filterit.stats;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
开发者_JS百科
import de.retowaelchli.filterit.SFilterConfigActivity;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c, Integer[] imageIds) {
mContext = c;
mThumbIds = imageIds;
}
//This Guy down here is giving me the NullpointerExcpetion now
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds;
}
And this is how i store the pictures to the Datbase (I only save to place where they are located):
int drawableID = context.getResources().getIdentifier("angel", "drawable", getPackageName());
iv.setImageResource(drawableID);
String info = String.valueOf(drawableID);
mDbHelper.open();
mDbHelper.createSmiley("You have received a heavenly message", info);
mDbHelper.close();
This is the Error-Log I get:
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): FATAL EXCEPTION: main
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.retowaelchli.filterit/de.retowaelchli.filterit.SFilterConfigActivity}: java.lang.NullPointerException
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.os.Looper.loop(Looper.java:143)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.main(ActivityThread.java:4196)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at java.lang.reflect.Method.invoke(Method.java:507)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at dalvik.system.NativeStart.main(Native Method)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): Caused by: java.lang.NullPointerException
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at de.retowaelchli.filterit.stats.ImageAdapter.getCount(ImageAdapter.java:25)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.widget.GridView.setAdapter(GridView.java:131)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at de.retowaelchli.filterit.SFilterConfigActivity.getImages(SFilterConfigActivity.java:54)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at de.retowaelchli.filterit.SFilterConfigActivity.onCreate(SFilterConfigActivity.java:30)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
10-04 13:26:25.776: ERROR/AndroidRuntime(10099): ... 11 more
Thx for your help!
There seems to be a lot of pertinent info missing here but if you fill in the blanks I will update the answer. First problem is what are you retrieving in your getAllSmileys function?
What you need to understand is that a cursor is like a memory table of results. So you need to tell the cursor which record to fetch from by using move commands otherwise it is uniniitilaized. So assuming your smilleys are stored in the SmileyDBAdapter.INFO column then you can retrieve them like this:
SmileyHelper.open();
Cursor c = SmileyHelper.getAllSmileys();
if(c!=null)
{
int i=0;
info = new int[c.getCount()]();
//get the column index
int infoIndex = c.getColumnIndex(SmileyDBAdapter.INFO);
while(c.moveToNext()){
String infoItem = c.getString(infoIndex );
int infoItemInteger = Integer.parseInt(infoItem);
info[i++] =infoItemInteger;
}
}
c.close();
SmileyHelper.close();
You are storing the image id's in the database as string which is making things more complicated. There is no need to parse and valueOf the drawable resource ID's into and out of a text column in the database. Simply change the column type to integer and always work with integers for your images.
You can simply use Cursor.getInt() to retrieve the values.
Try create constructor in ImageAdapter
public ImageAdapter(Context c, Integer[] imageIds) {
mContext = c;
mThumbIds = imageIds;
}
And in getImagesMethod
replace
gridview.setAdapter(new ImageAdapter(this));
with
gridview.setAdapter(new ImageAdapter(this, info));
I was trying this out and found this solution. The only one which worked.
public Integer[] getImages(){
SmileyHelper.open();
Cursor c = SmileyHelper.getAllSmileys();
ArrayList<Integer> infoList = new ArrayList<Integer>();
c.getColumnIndex(SmileyDBAdapter.INFO);
int ColumnIndex = c.getColumnIndex(SmileyDBAdapter.INFO);
if(c!=null)
{
while(c.moveToNext()){
String infoItem = c.getString( ColumnIndex );
infoList.add(Integer.parseInt(infoItem));
}
}
info = infoList.toArray(new Integer[]{});
c.close();
SmileyHelper.close();
//Hier wird die Grindview gefüllt
GridView gridview = (GridView) findViewById(R.id.SmileyGrind);
gridview.setAdapter(new ImageAdapter(this, info));
return info;
}
精彩评论