【问题标题】:getting null value in position adapter在位置适配器中获取空值
【发布时间】:2015-11-19 03:47:42
【问题描述】:

我需要修复此代码Objects item = getItem(position); 你能帮我告诉我为什么我得到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,它的似乎位置为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);

获取所有对象

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

【问题讨论】:

  • 重写 Objects 类中的 toString 方法并打印 Objects.toString() 以检查其内容。我怀疑 getUrl 是否返回 null。
  • 或者可能只是在getAllObjects方法中输入log来检查url是否为空,同时从数据库获取数据?
  • @DhavalPatel 我确实添加了登录 getallobjects ...List&lt;Objects&gt; Objects = db.getAllObjects(); Log.d("he","tttttt"); System.out.println(Objects); 并且我在问题中提到的输出是 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 , ]
  • 就是打印object hash-code,重写toString方法查看Objects类的内容。
  • 如果您使用的是android studio然后按Alt+Insert并在Objects类中选择toString,IDE将为您生成toString方法。

标签: android sqlite


【解决方案1】:

代替

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

【讨论】:

  • c 指的是什么?
  • 没关系..但仍然为空[Objects{id=10, name='H', url='null', type='null', category='null'},
  • 为什么你认为我的 url 为空,但我正在为它设置值? values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL
  • 检查db.insert(TABLE_OBJECTS, null, values);的值,同时插入数据。它将返回新插入行的行 ID,如果发生错误,则返回 -1。
  • 您之前是否尝试过插入相同的数据而没有 url。
猜你喜欢
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 2023-03-05
  • 2019-02-12
相关资源
最近更新 更多