必须从数据库转换静态字符串数组

时间:2012-09-04 03:43:57

标签: java android

这可能是一个很长的消息,但我想为所有stackoverflow用户提出一个明确的问题。 我所做的是在我的gridview

上绑定的类中创建一个静态的数组String
class ParserArrayList {
       //some declaration and codes here
      private String [] imageCaptionId = {
        "My First Medal",
        "You ...",
        "The ...",
        "Gimme ...",
        "A ...",
        "Seven ...",
        ".....City",
        ".... Madness",
        "Loyal...",
        ".....",
        "...",
        "Champion..."
        };
}

在公共类ImageAdapter上手动绑定它扩展了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;
}

如您所见,我正在调用ParserArrayList的'imageCaptionId'并将其发送到另一个数组字符串,其中我声明'imagetext'

一切正常,直到我发现数组'imageCaptionId'必须基于本地数据库 香港专业教育学院尝试使用此代码但我无法完成,因为

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();
}
}

首先:我不知道如何将它绑定到数组

第二:我在ParserArrayList中创建它,它给我一个错误说 构造函数SQLiteAdapter(ParserArrayList)未定义(已经完成)

先生,你能帮我吗

修改 这就是我想要完成的事情或我的逻辑

在我的ParserArrayList类中我试图添加此代码(我不知道这是否正确)

    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;
}

我错误地说语法错误,插入“AssignmentOperator Expression”来完成表达

现在我的ImageAdapter扩展BaseAdapter这里是我的代码

 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;
    }

我的逻辑是我将它绑定在ParserArrayList中的String数组中(它将返回imageCaptionIds)并将其设置在textview上

1 个答案:

答案 0 :(得分:1)

我不理解第一个问题,但第二个问题很简单。

您需要将Context(或将Context扩展的类,如Activity)传递给new SQLiteAdapter()。只需更改您的ParserArrayList即可:

public class ParserArrayList {
    Context mContext;
    ...

    public ParserArrayList(Context context) { 
        mContext = context;
        ... 
    }

    public void showData(){
        sqlAdapter = new SQLiteAdapter(mContext);
        ...
    }
}

在您的Activity中,也许是onCreate(),只需调用:

 ParserArrayList pal = new ParserArrayList(this);

我将尝试回答第一个问题。为什么必须将您的String数组放入SQLiteDatabaseArrayAdapter<String>与String数组相得益彰...为什么会赢得类似这样的工作?

public class ImageAdapter extends ArrayAdapter<String> {
    //some declaration and variables here

    public View getView(int position, View convertView, ViewGroup parent) {
        View v;

        String imagetext = getItem(position); // returns the caption at index
        if (convertView == null) {
            //some stuff here
        }

        //some stuff here
        return v;
    }
}

声明:

private String [] imageCaptionId = { ... };
ImageAdapter adapter = new ImageAdapter(this, R.layout.awesome, imageCaptionId);