2013-02-26 52 views
0

我從數據庫查詢標題列,並希望在GridView的TextView中設置它。從數據庫查詢標題列並將其設置爲TextView

怎麼樣?

CafeDatasource

public List<Model_Insert> findTblCafe(){ 
    List<Model_Insert> model_Inserts = new ArrayList<Model_Insert>(); 
    Cursor cursor = database.query(CafeDbOpenHelper.TABLE_CAFE, rtv_tbl_Cafe, 
      null, null, null, null, null); 
    Log.i("number", "return" + cursor.getCount()+ " rows"); 

    if(cursor.getCount() > 0){ 
     while (cursor.moveToNext()) { 
      Model_Insert model_Insert = new Model_Insert(); 
      model_Insert.setCafe_Id(cursor.getInt(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_ID))); 
      model_Insert.setCafe_Title(cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_TITLE))); 
      model_Insert.setCafe_Been(cursor.getInt(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_BEEN))); 
      model_Insert.setCafe_Want(cursor.getInt(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_WANT))); 
      model_Insert.setCafe_Address(cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_ADDRESS))); 
      model_Insert.setCafe_Thumb(cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_THUMB))); 
      model_Insert.setCafe_Description(cursor.getString(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_DESCRIPTION))); 
      model_Insert.setCafe_WifiRate(cursor.getInt(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_WIFI_RATE))); 
      model_Insert.setCafe_CoffeeRate(cursor.getInt(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_COFFEE_RATE))); 
      model_Insert.setCafe_Latitude(cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LATITUDE))); 
      model_Insert.setCafe_Longitude(cursor.getDouble(cursor.getColumnIndex(CafeDbOpenHelper.CAFE_LONGITUDE))); 
      model_Inserts.add(model_Insert); 
     } 
    } 

    return model_Inserts; 
} 

MainActivity

public ArrayList<HashMap<String, String>> placeList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_card); 

=============================================================== 
dataSource = new CafeDataSource(this); 
dataSource.open(); 
List<Model_Insert> model_Inserts = dataSource.findTblCafe();// query database (findTblCafe) 

if(model_Inserts.size() == 0){ 
    new DownloadImageTask().execute(); 
    model_Inserts = dataSource.findTblCafe(); 
} 
=============================================================== 

} // End onCreate 


public void ShowAllContent() { 
    GridView gridView1 = (GridView) findViewById(R.id.grid_all); 
    gridView1.setAdapter(new ImageAdapter(TopActivity.this, placeList)); 
} 

public class DownloadImageTask extends AsyncTask<String, Void, Void>{ 

    @Override 
    protected Void doInBackground(String... params) { 

     placeList = new ArrayList<HashMap<String,String>>(); 
     JSONParser jParser = new JSONParser(); 
     JSONObject jsonO = jParser.getJSONUrl(url); 

     try { 
      places = jsonO.getJSONArray("place"); 

      for (int i = 0; i < places.length(); i++) { 
       JSONObject jobj = places.getJSONObject(i); 

       int cafe_id = jobj.getInt(TAG_CAFE_ID); 
       String cafe_title = jobj.getString(TAG_CAFE_TITLE); 
       int cafe_been = jobj.getInt(TAG_CAFE_BEEN); 
       int cafe_want = jobj.getInt(TAG_CAFE_WANT); 
       String cafe_address = jobj.getString(TAG_CAFE_ADDRESS); 
       String cafe_thumb = jobj.getString(TAG_CAFE_THUMB); 
       String cafe_description = jobj.getString(TAG_CAFE_DESCRIPTION); 
       int cafe_wifi_rate = jobj.getInt(TAG_CAFE_WIFI_RATE); 
       int cafe_coffee_rate = jobj.getInt(TAG_CAFE_COFFEE_RATE); 
       double cafe_latitude = jobj.getDouble(TAG_CAFE_LATITUDE); 
       double cafe_longitude = jobj.getDouble(TAG_CAFE_LONGITUDE); 

       // Table Save 
       Model_Insert model_Insert = new Model_Insert(); 
       model_Insert.setCafe_Id(cafe_id); 
       model_Insert.setCafe_Been(cafe_been); 
       model_Insert.setCafe_Want(cafe_want); 
       model_Insert = dataSource.createTableCafeSave(model_Insert); 
       Log.i("data", " ID " + model_Insert.getCafe_Id()); 

       // Table Cafe 
       model_Insert = new Model_Insert(); 
       model_Insert.setCafe_Id(cafe_id); 
       model_Insert.setCafe_Title(cafe_title); 
       model_Insert.setCafe_Been(cafe_been); 
       model_Insert.setCafe_Want(cafe_want); 
       model_Insert.setCafe_Address(cafe_address); 
       model_Insert.setCafe_Thumb(cafe_thumb); 
       model_Insert.setCafe_Description(cafe_description); 
       model_Insert.setCafe_WifiRate(cafe_wifi_rate); 
       model_Insert.setCafe_CoffeeRate(cafe_coffee_rate); 
       model_Insert.setCafe_Latitude(cafe_latitude); 
       model_Insert.setCafe_Longitude(cafe_longitude); 
       model_Insert = dataSource.createTableCafe(model_Insert); 
       Log.i("data", " Picture " + model_Insert.getCafe_Id()); 

       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put(TAG_CAFE_TITLE, cafe_title); 

       placeList.add(map); 

      } 

      // for " piture " Object in json 
      pictures = jsonO.getJSONArray("pictures"); 
      for (int i = 0; i < pictures.length(); i++) { 
       JSONObject jObj = pictures.getJSONObject(i); 
       int cafe_id = jObj.getInt(TAG_CAFE_ID); 
       String picture_url = jObj.getString(TAG_PICTURE_URL); 

       // Table Picture 
       Model_Insert model_Insert = new Model_Insert(); 
       model_Insert.setCafe_Id(cafe_id); 
       model_Insert.setPitureUrl(picture_url); 
       model_Insert = dataSource.createTablePicture(model_Insert); 
       Log.i("pic", " Picture " + model_Insert.getPitureurl()); 

       HashMap<String, String> map = new HashMap<String, String>(); 
       placeList.add(map); 





      } 
     } catch (JSONException e) { 
      // TODO: handle exception 
     } 
     return null; 
    } 

    protected void onPostExecute(Void unused) { 
     ShowAllContent(); // When Finish Show Content 

    } 

} 


private static class ViewHolder { 
    public ImageView imageview; 
    public TextView txtTitle; 
} 

public class ImageAdapter extends BaseAdapter { 
    private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String,String>>(); 
    public ImageAdapter(Context c, ArrayList<HashMap<String, String>> myArrayList){ 
     context = c; 
     MyArr = myArrayList; 
    } 
    @Override 
    public int getCount() { 
     return MyArr.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View converView, ViewGroup parent) { 
     ViewHolder viewHolder = new ViewHolder(); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if(converView == null){ 
      converView = inflater.inflate(R.layout.grid_item, null); 
     } 
     viewHolder.imageview = (ImageView) converView.findViewById(R.id.imv_card_cafe); 
     viewHolder.txtTitle = (TextView) converView.findViewById(R.id.txt_title); 

     /* String str = "frute : juse text"; 
     Integer len; 
     len = str.length(); 
     if(len > 20){ 
      String result = str.substring(0, 15); 
      viewHolder.txtTitle.setText(result); 

     } 
     else { 
      viewHolder.txtTitle.setText(str); 
     } */ 


============================================================================= 
     viewHolder.txtTitle.setText(...............................?); 
============================================================================= 
     viewHolder.imageview.setImageResource(mThumb[position]); 

     return converView; 
    } 
} 
+0

顯示您正在使用當前的代碼得到什麼問題呢? – 2013-02-26 08:41:37

+0

在ImageAdapter中。 'viewHolder.txtTitle.setText .......?' – kongkea 2013-02-26 08:43:03

+0

你想在setText中想要什麼,你可以解釋一下是什麼問題,而不是說'....',並且你沒有在'map'裏面添加任何值' doInBackground' – 2013-02-26 08:48:30

回答

1

,因爲你正在裏面model_Inserts列出所有冠軍,你需要這個列表傳遞給自定義BaseAdapter在TextView中顯示爲:

變化ShowAllContent()方法爲:

public void ShowAllContent() { 
GridView gridView1 = (GridView) findViewById(R.id.grid_all); 
gridView1.setAdapter(new ImageAdapter(TopActivity.this, placeList,model_Inserts)); 
} 

ImageAdapter構造函數:

public class ImageAdapter extends BaseAdapter { 
    private ArrayList<HashMap<String, String>> MyArr = 
             new ArrayList<HashMap<String,String>>(); 
    List<Model_Insert> model_Inserts=null; 
    public ImageAdapter(Context c, 
       ArrayList<HashMap<String, String>> myArrayList, 
       List<Model_Insert> model_Inserts){ 
     context = c; 
     MyArr = myArrayList; 
     this.model_Inserts=model_Inserts; 
    } 

///your code here... 

現在使用model_Inserts用於獲取標題裏面getView

+0

@kongkea:你有沒有創建getCafe_Title()方法獲取標題內的Model_Insert類? – 2013-02-26 10:32:37

+0

.viewHolder.txtTitle.setText(model_Inserts.get(position).getCafe_Title());.但沒有工作 – kongkea 2013-02-26 10:39:28

相關問題