在位置适配器中获取空值

时间:2015-11-18 18:33:05

标签: android sqlite

我需要修复此代码Objects item = getItem(position); 你可以帮忙告诉我为什么我会得到null 这里显示结果为null

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

请注意,数据库sqlite正在返回数据。

www.i I/System.out﹕ www.i.Objects@c880a09, com.justedhak.www.i.Objects@14f1340e, www.i.Objects@10f6dc3c, com.justedhak.www.i.Objects@29e3dc5, www.i.Objects@1f90831a, com.justedhak.www.i.Objects@3895ee4b, ]

我正在尝试将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,结果为null,其似乎位置为空

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

1 个答案:

答案 0 :(得分:2)

而不是

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

使用

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