this might be a long message but i would like to give a clear question for all of stackoverflow user.
What I did is create a static String of array inside a class that is binded on my gridview
class ParserArrayList {
//some declaration and codes here
private String [] imageCaptionId = {
"My First Medal",
"You ...",
"The ...",
"Gimme ...",
"A ...",
"Seven ...",
".....City",
".... Madness",
"Loyal...",
".....",
"...",
"Champion..."
};
}
And manually binding it on public class ImageAdapter extends BaseAdapter
public class ImageAdapter extends BaseAdapter{
//some declaration and variables here
public View getView(int position, View convertView, ViewGroup parent) {
View v;
ParserArrayList arrayImage = new ParserArrayList();
String[] imagetext = arrayImage.getImageCaptionId();
if (convertView == null) {
//some stuff here
}
//some stuff here
return v;
}
As you see I am calling ParserArrayList’s ‘imageCaptionId’ and sends it to another string of array in which i declare ‘imagetext ‘
Everything works fine until I found out that the array ‘imageCaptionId’ must be based on local database
Ive tried using this code but I cant finish because
class ParserArrayList {
//added this code
public SQLiteAdapter sqlAdapter;
//and this one
public void showData(){
sqlAdapter = new SQLiteAdapter(this);
String str = "Select dTitle from achievement_tb where version =0 order by ID ASC;";
sqlAdapter.openToRead();
Cursor c =sqlAdapter.read(str);
sqlAdapter.close();
}
}
First: I dont know how to bind it to array
Second: I created it inside ParserArrayList and it gives me an error saying
The constructor SQLiteAdapter(ParserArrayList) is undefined (This is already done)
Could you help me sir
EDIT
This is what I am trying to accomplish or my logic
on my ParserArrayList class I am trying to add this code (I dont know if this is correct)
public String[] showImageCaption(){
String imageCaptionIds [];
sqlAdapter = new SQLiteAdapter(mContext);
String str = "Select dTitle from achievement_tb where version =0 order by ID ASC;";
sqlAdapter.openToRead();
Cursor c =sqlAdapter.read(str);
imageCaptionIds [c.getCount()];
sqlAdapter.close();
return imageCaptionIds;
}
which gives me an error saying Syntax error, insert “AssignmentOperator Expression” to complete Expression
now on my ImageAdapter extends BaseAdapter here is my codes
public View getView(int position, View convertView, ViewGroup parent) {
View v;
ParserArrayList arrayImage = new ParserArrayList(mContext);
newArrayList2 = arrayImage.getArraylist();
**String[] imagetext = arrayImage.showImageCaption();**
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.medal_grid, null);
}
else
{
v = convertView;
}
TextView tv = (TextView)
v.findViewById(R.id.grid_item_label);
**tv.setText(imagetext[position]);**
ImageView iv = (ImageView)v.findViewById(R.id.grid_item_image);
iv.setImageResource((Integer) newArrayList2.get(position));
return v;
}
My logic is that I will bind it on my array of String inside ParserArrayList (in which it will return imageCaptionIds) and set it on a textview
I don’t understand the first question, but the second one is easy.
You need to pass a Context (or a class that extends Context, like an Activity) to
new SQLiteAdapter(). Simply change your ParserArrayList to match this:Inside your Activity, perhaps onCreate(), simply call:
I’ll attempt to answer the first question. Why must your String array be put into a
SQLiteDatabase? AnArrayAdapter<String>works quite well with String arrays… Why won’t something like this work?With a declaration: