0

我有来自数据库的查询标题列,并想在 GridView 的 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;
}

主要活动

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

1 回答 1

1

因为您要在 List 中获取所有标题,所以model_Inserts您需要将此 List 传递给 CustomBaseAdapter以便在 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 中

于 2013-02-26T08:59:34.193 回答