如何通过imageid从sqlite中获取图像?

时间:2016-11-30 06:52:51

标签: android sqlite

我使用baseadapter来检索imageid,title,fromtime,totime.i无法将imageid设置为我的imageview

我的代码如下:

public class SQliteAdapter extends BaseAdapter {

    Context context;
    ArrayList<String> Imageid;
    ArrayList<String> Title;
    ArrayList<String> fromtimer;
    ArrayList<String> totimer;
    //   ArrayList<String> UserSubject ;


    public SQliteAdapter(
            Context context2,
            ArrayList<String> Imageid,
            ArrayList<String> Title,
            ArrayList<String> fromtimer,
            ArrayList<String> totimer
            // ArrayList<String> subject
    )
    {

        this.context = context2;
        this.Imageid = Imageid;
        this.Title = Title;
        this.fromtimer = fromtimer;
        this.totimer =  totimer;
        //this.UserSubject = subject ;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Imageid.size();
    }

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

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View child, ViewGroup parent) {

        Holder holder;

        LayoutInflater layoutInflater;

        if (child == null) {
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            child = layoutInflater.inflate(R.layout.showlist, null);

            holder = new Holder();

            holder.image = (ImageView) child.findViewById(R.id.showimage);
            holder.title = (TextView) child.findViewById(R.id.title1);
            holder.fromtimer = (TextView) child.findViewById(R.id.fromtimer);
            holder.totimer = (TextView) child.findViewById(R.id.totimer);
            // holder.textviewsubject = (TextView) child.findViewById(R.id.textViewSUBJECT);

            child.setTag(holder);

        } else {

            holder = (Holder) child.getTag();
        }
        holder.image.setImageResource(Integer.parseInt(Imageid.get(position)));
        holder.title.setText(Title.get(position));
        holder.fromtimer.setText(fromtimer.get(position));
        holder.totimer.setText(totimer.get(position));
        // holder.textviewsubject.setText(UserSubject.get(position));

        return child;
    }

    public class Holder {
        ImageView image;
        TextView title,fromtimer,totimer;
        //TextView textviewphone_number;
        //  TextView textviewsubject;
    }

我的主要活动是:

public class MainActivity extends ActionBarActivity {
    private ImageView control;


    ArrayList<String> sName = null;
    ArrayList<String> sImageID = null;
    ArrayList<String> iFrom = null;
    ArrayList<String> iTo = null;


    ListView list;
    List<Program> rowItems;
    int  iImageId;
    String sTitle,sFrom,sTo ;
    SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);


        db =openOrCreateDatabase("MukilProgram", Context.MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS proname(ID INTEGER PRIMARY KEY AUTOINCREMENT,ImageID INTEGER,Title TEXT,FromTiming INTEGER,ToTiming INTEGER);");

        rowItems = new ArrayList<Program>();
        for (int i = 0; i < titles.length; i++) {
            Program item = new Program(images[i], titles[i],fromtime[i],totime[i]);
            rowItems.add(item);
        }




        db.execSQL("DELETE FROM proname;");


        list = (ListView) findViewById(R.id.list);

        // listView.setVisibility(View.INVISIBLE);
        final ProgramAdapter adapter = new ProgramAdapter(this,rowItems, false);
        for (int i = 0; i < adapter.getCount(); i++) {
            Program rowItem = (Program) adapter.getItem(i);

            iImageId = rowItem.getImageId();
            sTitle = rowItem.getTitle();
            sFrom = rowItem.getFromtime();
            sTo  = rowItem.getTotime();

            db.execSQL("INSERT INTO proname (ImageID,Title,FromTiming,ToTiming) VALUES("+ iImageId + ",'" + sTitle + "','"+sFrom+"','"+sTo+"');");
            //list.setAdapter(adapter);

        }


        media = MediaPlayer.create(this, R.raw.mukil_master_jingle);


        final Cursor cView = db.rawQuery("SELECT * FROM proname WHERE FromTiming <= time('now', 'localtime')\n" + "" +
                "AND ToTiming >= time('now', 'localtime')\n", null);
        if (cView.getCount() > 0) {

            sImageID = new ArrayList<String>();
            sName = new ArrayList<String>();
            iFrom = new ArrayList<String>();
            iTo = new ArrayList<String>();



            while (cView.moveToNext()) {

                sImageID.add(cView.getString(0));
                sName.add(cView.getString(1));
                iFrom.add(cView.getString(2));
                iTo.add(cView.getString(3));

                SQliteAdapter sqliteadapter = new SQliteAdapter(MainActivity.this,sImageID,sName,iFrom,iTo);
                list.setAdapter(sqliteadapter);
            }

        }

请帮帮我..

1 个答案:

答案 0 :(得分:0)

不要依赖资源 ID,而是保存图标名称或创建图像和“名称”的映射。

 <input type="file" id="file" name="name[]" multiple />

if(isset($_FILES['upload_picture']['name']))
    {
        $file_name_all="";
        for($i=0; $i<count($_FILES['upload_picture']['name']); $i++) 
        {
               $tmpFilePath = $_FILES['upload_picture']['tmp_name'][$i];    
               if ($tmpFilePath != "")
               {    
                   $path = "propertyimages/"; // create folder 
                   $name = $_FILES['upload_picture']['name'][$i];
                  $size = $_FILES['upload_picture']['size'][$i];

                   list($txt, $ext) = explode(".", $name);
                   $file= time().substr(str_replace(" ", "_", $txt), 0);
                   $info = pathinfo($file);
                   $filename = $file.".".$ext;
                   if(move_uploaded_file($_FILES['upload_picture']['tmp_name'][$i], $path.$filename)) 
                   { 
                      $file_name_all.=$filename."*";
                   }
             }
        }
         $filepath = rtrim($file_name_all, '*'); 
         $tablename = 'album_picture';
         $abc = new Demo();
         $res = $abc->insert($tablename,$values);
         unset($abc);
        }
        else
    {
        $filepath="";
    }