数据库中的字符串和可绘制文件夹中的可绘制图像

时间:2015-12-04 17:21:27

标签: java android sqlite android-adapter

如何从我的数据库中获取String并将其与我的drawable文件夹中的Image相结合。请帮助。

这是我的主要活动。

package com.apk.foodanalyzer.healthandnutrifactsanalyzer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.apk.foodanalyzer.healthandnutrifactsanalyzer.goactivity.DatabaseHelper;
import com.apk.foodanalyzer.healthandnutrifactsanalyzer.goactivity.GoItem;
import com.apk.foodanalyzer.healthandnutrifactsanalyzer.goactivity.ListGoAdapter;
import com.apk.foodanalyzer.healthandnutrifactsanalyzer.growactivity.GrowDatabaseHelper;
import com.apk.foodanalyzer.healthandnutrifactsanalyzer.growactivity.GrowItem;
import com.apk.foodanalyzer.healthandnutrifactsanalyzer.growactivity.ListGrowAdapter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by Pavilion on 11/18/2015.
 */
public class Grow extends AppCompatActivity {

    private ListView listviewGrow;
    private ListGrowAdapter adapter;
    private List<GrowItem> mGrowItemList;
    private GrowDatabaseHelper mDBHelper;
    private SearchView searchtext;

    Integer[] imgid = {R.drawable.carp,R.drawable.catfish, R.drawable.cavalla };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        searchtext = (SearchView) findViewById(R.id.search_text);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grow_layout);
        listviewGrow = (ListView)findViewById(R.id.listview_growtable);
        mDBHelper = new GrowDatabaseHelper(this);

        mGrowItemList = mDBHelper.getListGrow();
        adapter = new ListGrowAdapter(imgid, this, mGrowItemList);
        listviewGrow.setAdapter(adapter);
        listviewGrow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View row, int position, long index) {
                Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Grow.this, GrowValueActivityContent.class);
                intent.putExtra("RowId", position);
                startActivity(intent);
            }
        });

    }

这是我的适配器。我不知道该修改什么。希望你能帮助我。

package com.apk.foodanalyzer.healthandnutrifactsanalyzer.growactivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apk.foodanalyzer.healthandnutrifactsanalyzer.R;
import com.apk.foodanalyzer.healthandnutrifactsanalyzer.goactivity.GoItem;

import java.util.List;

/**
 * Created by Pavilion on 12/1/2015.
 */
public class ListGrowAdapter extends BaseAdapter {

    private Context mContext;
    private List<GrowItem> mGrowItemList;
    private final Integer[] imgid;

    public ListGrowAdapter(Integer[] imgid, Context mContext, List<GrowItem> mGrowItemList) {
        this.imgid = imgid;
        this.mContext = mContext;
        this.mGrowItemList = mGrowItemList;
    }

    @Override
    public int getCount() {
        return mGrowItemList.size();
    }
    @Override
    public GrowItem getItem(int position) {
        return mGrowItemList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return mGrowItemList.get(position).getRowId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GrowItem item = getItem(position);
        View vv = View.inflate(mContext, R.layout.go_listview, null);
        TextView tvFoodDescription = (TextView)vv.findViewById(R.id.txtvw_fooddescription);
        ImageView imgFoodImage = (ImageView)vv.findViewById(R.id.food_image);

        tvFoodDescription.setText(item.getFoodDescription());
        imgFoodImage.setImageResource(imgid[position]);

        return vv;
    }

}

0 个答案:

没有答案