2015-11-18 104 views
0

我需要修复这些代码Objects item = getItem(position); 可以请你帮忙告诉我为什么我有越来越null 这里它的显示结果为空获得位置适配器空值

System.out﹕ item.getUrl() 
System.out﹕ null 

请注意数据库sqlite是返回数据。

www.i I/System.out﹕ [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], ] 

我试图显示从sqlite的值,以网格视图适配器

List<Objects> Objects = db.getAllObjects(); 
Log.d("he","tttttt"); 
System.out.println(Objects); 
DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects); 

adapter.notifyDataSetChanged(); 
mGridView.setAdapter(adapter); 

我添加了和是System.out.print的结果为空,它的位置似乎正在空

Objects item = getItem(position); 
System.out.println("item.getUrl() "); 
System.out.println(item.getUrl()); 
Picasso.with(mcontext).setIndicatorsEnabled(true); 
//holder.imageTitle.setText(item.getId()); 
holder.imageTitle.setText(String.valueOf(item.getId())); 
Picasso. 
     with(mcontext). 
     load(item.getUrl()) 
     .placeholder(R.drawable.logo) 
     .fit() 
     .noFade() 
     .into(holder.imageView); 

这是完整的代码适配器

public class DBadapter extends ArrayAdapter<Objects> { 
    private static Uri[] mUrls = null; 
    private static String[] strUrls = null; 
    private String[] mNames = null; 
    private Cursor cc = null; 
    private Context mcontext; 
    private int layoutResourceId; 
    private List<?> listitems; 

    public DBadapter(Context context, int layoutResourceId, List<Objects> listitem) { 
     super(context, layoutResourceId, listitem); 
     this.layoutResourceId = layoutResourceId; 
     this.mcontext = context; 
     this.listitems = listitem; 
     System.out.println("entering adapter"); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     System.out.println("entering adapter1"); 

     View row = convertView; 
     final ViewHolder holder; 

     if (row == null) { 
      LayoutInflater inflater = LayoutInflater.from(mcontext); 
      row = inflater.inflate(layoutResourceId, parent, false); 
      holder = new ViewHolder(); 
      holder.imageTitle = (TextView) row.findViewById(R.id.Nameview); 
      holder.imageView = (ImageView) row.findViewById(R.id.imageView); 
      row.setTag(holder); 
     } else { 
      holder = (ViewHolder) row.getTag(); 
     } 
     Objects item = getItem(position); 
     System.out.println("item.getUrl() "); 
     System.out.println(item.getUrl()); 
     Picasso.with(mcontext).setIndicatorsEnabled(true); 
     //holder.imageTitle.setText(item.getId()); 
     holder.imageTitle.setText(String.valueOf(item.getId())); 
     Picasso. 
       with(mcontext). 
       load(item.getUrl()) 
       .placeholder(R.drawable.logo) 
       .fit() 
       .noFade() 
       .into(holder.imageView); 

     holder.imageView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Log.d("OnImageButton", "Clicked"); 
       Intent intnt = new Intent(mcontext, SingleViewActivity.class); 
       intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       //Bitmap imageID=holder.imageView; 
       //intnt.putExtra("ImageId", imageID); 
       mcontext.startActivity(intnt); 


       Toast.makeText(mcontext, "intent", 
         Toast.LENGTH_LONG).show(); 
      } 
     }); 


     return row; 
    } 

    static class ViewHolder { 
     TextView imageTitle; 
     ImageView imageView; 
    } 
} 

编辑

ContentValues values = new ContentValues(); 

     values.put(OBJECT_ID,"10"); // OBJECT Name 
     values.put(OBJECT_NAME, "H"); // OBJECT Name 
     values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL 
     values.put(OBJECT_TYPE, "image"); // Contact type 
     values.put(OBJECT_CATEGORY, "funny"); // Contact category 

     // Inserting Row 
     db.insert(TABLE_OBJECTS, null, values); 
     db.close(); // Closing database connection 
    } 
    // Objects object=new Objects(String); 

    // Getting single contact 
    Objects geturl(String name) { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.query(TABLE_OBJECTS, new String[] {OBJECT_URL, 
         OBJECT_NAME , OBJECT_CATEGORY, OBJECT_TYPE }, OBJECT_NAME + "=?", 
       new String[] { String.valueOf(name) }, null, null, null, null); 

getallobjects

// Getting All Contacts 
public List<Objects> getAllObjects() { 
    List<Objects> Objectslist = new ArrayList<Objects>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_OBJECTS; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Objects object = new Objects(); 
      object.setId(Integer.parseInt(cursor.getString(0))); 
      object.setName(cursor.getString(1)); 
      object.setUrl(cursor.getString(2)); 
      // Adding contact to list 
      Objectslist.add(object); 
     } while (cursor.moveToNext()); 
    } 

    // return contact list 
    return Objectslist; 
} 
+0

覆盖toString方法内容。我怀疑getUrl是否返回null。 –

+0

或者可能只是把日志getAllObjects方法从数据库中获取数据时检查url是否为空? –

+0

@DhavalPatel我确实增加了登录getallobjects ...'List Objects = db.getAllObjects(); Log.d(“he”,“tttttt”);我的问题中提到的输出是www.i I/System.out:[email protected],[email protected], [email protected],[email protected],[email protected],[email protected],] – Moudiz

回答

2

而不是

object.setId(Integer.parseInt(cursor.getString(0))); 
object.setName(cursor.getString(1)); 
object.setUrl(cursor.getString(2)); 

使用的对象类和打印Objects.toString(),以检查它的

object.setId(cursor.getInt(cursor.getColumnIndex(OBJECT_ID))); 
object.setName(cursor.getString(cursor.getColumnIndex(OBJECT_NAME))); 
object.setUrl(cursor.getString(cursor.getColumnIndex(OBJECT_URL))); 
+0

'c'是指什么? – Moudiz

+0

ohh它的错字..它是相同的光标对象.. –

+0

它确定..但仍然变空'[对象{id = 10,name ='H',url ='null',type ='null',category =' null'},' – Moudiz