2012-09-04 18 views
9

điều này có thể là một thông điệp dài nhưng tôi muốn đưa ra một câu hỏi rõ ràng cho tất cả người dùng stackoverflow. Những gì tôi đã làm là tạo một static String của mảng bên trong một lớp học mà được binded trên GridView của tôiChuỗi tĩnh của chuỗi phải được chuyển đổi từ cơ sở dữ liệu

class ParserArrayList { 
     //some declaration and codes here 
     private String [] imageCaptionId = { 
     "My First Medal", 
     "You ...", 
     "The ...", 
     "Gimme ...", 
     "A ...", 
     "Seven ...", 
     ".....City", 
     ".... Madness", 
     "Loyal...", 
     ".....", 
     "...", 
     "Champion..." 
     }; 
} 

Và tự ràng buộc nó trên public class ImageAdapter kéo dài 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; 
} 

Như bạn thấy tôi kêu gọi 'ImageCaptionId' của ParserArrayList và gửi nó tới một chuỗi mảng khác, trong đó tôi khai báo 'imagetext'

Mọi thứ hoạt động tốt cho đến khi tôi phát hiện ra rằng 'imageCaptionId' của mảng phải dựa trên cơ sở dữ liệu cục bộ Ive đã cố gắng sử dụng mã này, nhưng tôi không thể hoàn thành vì

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

Thứ nhất: Tôi không biết làm thế nào để ràng buộc nó vào mảng

Thứ hai: Tôi tạo ra nó bên ParserArrayList và nó mang lại cho tôi một lỗi nói Các constructor SQLiteAdapter (ParserArrayList) là undefined (Điều này đã được thực hiện)

ông có thể giúp tôi thưa ông

E DIT Đây là những gì tôi đang cố gắng để thực hiện hoặc logic của tôi

trên lớp ParserArrayList của tôi, tôi đang cố gắng để thêm mã này (Tôi không biết nếu điều này là đúng)

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

mà mang lại cho tôi một lỗi nói lỗi cú pháp, chèn "AssignmentOperator Expression" để hoàn thành Biểu

giờ trở đi ImageAdapter của tôi kéo dài BaseAdapter đây là mã của tôi

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

Logic của tôi là tôi sẽ ràng buộc nó vào mảng của tôi về chuỗi bên ParserArrayList (trong đó nó sẽ trở lại imageCaptionIds) và thiết lập nó trên một TextView

+0

Hope ai đó có thể trả lời câu hỏi nguyên nhân này tôi có thể gặp phải nó cũng^_^ – Janwel

+0

vấn đề thứ hai xảy ra bởi vì bạn đang đi qua các tham số sai cho 'sqlAdapter = new SQLiteAdapter (này); 'This' của bạn tham chiếu đến' ParserArrayList' (là một đối tượng bạn đã tạo), bạn nên tạo một 'SQLiteOpenHelper' để truy cập cơ sở dữ liệu của bạn. Xem [hướng dẫn này] (http://www.vogella.com/articles/AndroidSQLite/article.html) – Aprian

+0

bị thử đọc bài viết của bạn thưa ông –

Trả lời

1

Tôi không hiểu câu hỏi đầu tiên, nhưng một trong những thứ hai dễ.

Bạn cần phải vượt qua Ngữ cảnh (hoặc một lớp mở rộng Ngữ cảnh, như Hoạt động) thành new SQLiteAdapter(). Đơn giản chỉ cần thay đổi ParserArrayList của bạn để phù hợp với điều này:

public class ParserArrayList { 
    Context mContext; 
    ... 

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

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

Bên trong hoạt động của bạn, có lẽ onCreate(), chỉ cần gọi:

ParserArrayList pal = new ParserArrayList(this); 

tôi sẽ cố gắng trả lời câu hỏi đầu tiên. Tại sao phải mảng Chuỗi của bạn được đưa vào một số SQLiteDatabase? An ArrayAdapter<String> hoạt động khá tốt với các mảng String ... Tại sao một thứ gì đó không giống như vậy?

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

Với một tuyên bố:

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

Đây là những gì tôi đang cố gắng hoàn thành. Xin vui lòng xem tôi chỉnh sửa mã trên –

+0

là logic của tôi có thể thưa ông? Hãy xem nó ngay bây giờ –

+0

ok sir Tôi nghĩ rằng tôi sẽ tạo lại logic của tôi dựa trên câu trả lời của bạn .. Cảm ơn bạn đã trả lời^_^ –

Các vấn đề liên quan