如何使用SQLite中的游标适配器在列表视图中显示图像和文本?

时间:2014-04-07 12:15:11

标签: java android sqlite

我的文字绝对正确,因为我应该得到的。但是图像重复出现并显示相同的图像(即我在数据库中插入的第一个图像)。

代码:Samsunglists.java

public class Samsunglists extends ListActivity {    
    BaseAdapter adapter = null;
    Cursor mcursor;
    static final Class[] classArray = new Class[] { Samsung.class,Samsungs1.class};
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_samsunglists);

        Sqlite sqllll = new Sqlite(this);
        SQLiteDatabase database = sqllll.getReadableDatabase();  
        ListView lv= getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int pos,
                    long id) {
                // TODO Auto-generated method stub
                Intent in = new Intent(Samsunglists.this, classArray[pos]);
                startActivity(in);
            }
        });               
        mcursor = database.query(Sqlite.TABLE_PRODUCT,new String[] {Sqlite._ID, Sqlite.PRODUCT_IMAGE, Sqlite.PRODUCT_NAME}, null, null, null, null, null, null);
       startManagingCursor(mcursor);     
      adapter = new ImageCursorAdapter(this, R.layout.samsunglists_entry,mcursor, new String[]
              {Sqlite.PRODUCT_IMAGE, Sqlite.PRODUCT_NAME}, new int[] {R.id.imageentry, R.id.samsungtype} );
      setListAdapter(adapter);
      adapter.notifyDataSetChanged();     
    }  
}

代码:ImageCursorAdapter.java

public class ImageCursorAdapter extends SimpleCursorAdapter {
    private Cursor c;
    private Context context;
    byte[] imgssss;
    public ImageCursorAdapter(Context context, int layout,
            Cursor c, String[] from, int[] to) {
        // TODO Auto-generated constructor stub
         super(context, layout, c, from, to);
            this.c = c;
            this.context = context;
    }
    @Override
    public View getView(int pos, View inView, ViewGroup parent) {
        // TODO Auto-generated method stub
         View v = inView;
           if (v == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.samsunglists_entry, null);
           }       
           this.c.moveToPosition(pos);         
           String productname = this.c.getString(this.c.getColumnIndex("productname"));
           imgssss = this.c.getBlob(this.c.getColumnIndex("productimage"));
           ImageView iv = (ImageView) v.findViewById(R.id.imageentry);
           if (imgssss != null) {

               if(imgssss.length > 0)
               {                  
                   iv.setImageBitmap(BitmapFactory.decodeByteArray(imgssss, 0, imgssss.length));
                   } 
               }           
           TextView tv = (TextView) v.findViewById(R.id.samsungtype);
           tv.setText(productname);
        return (v);

}

我将如何使用文本获得相应的图像?

0 个答案:

没有答案