BindView没有激活所以listview空白

时间:2014-06-17 11:16:10

标签: android listview cursor

我有一个自定义游标适配器,除了bindView方法之外,它似乎大部分都在工作。我在bindView中有Log调用,它们永远不会出现在我的logCat中。我创建的listView正在显示正确的行数,但行是空白的。 onCLickListener也正常工作,logcat显示它已从数据库中读取行。

代码如下 - 在我的活动中:

// build a listView adapter
        Log.i(LOGTAG,"About to create listView...");
        tripList = (ListView) findViewById(android.R.id.list);
        //tripList = getListView();
        Log.i(LOGTAG,"ListView created...");

        Log.i(LOGTAG,"About to create adapter...");
        tripAdapter = new tripListAdapter(this, tripCur);
        //setListAdapter(tripAdapter);
        tripList.setAdapter(tripAdapter);

        tripList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Do something in response to the click
                // Enable other buttons
                togBtns(true);

                // set global variable to show which ID was clicked on
                listTripPosClicked = position;

                // Get details from display, of clicked item
                TextView tmpDate = (TextView) tripList.findViewById(R.id.dateFld);
                TextView tmpDist = (TextView) tripList.findViewById(R.id.distFld);
                TextView tmpMPG = (TextView) tripList.findViewById(R.id.mpgFld);

                // store these for sending to doTripActivity
                clickedDist = tmpDist.getText().toString();
                clickedDate = tmpDate.getText().toString();
                clickedMPG = tmpMPG.getText().toString();

                Log.i(LOGTAG, "Dist = " + clickedDist);
                Log.i(LOGTAG, "Date = " + clickedDate);
                Log.i(LOGTAG, "mpg = " + clickedMPG);

            }
        });

适配器代码是:

// custom list adaptor class
private class tripListAdapter extends CursorAdapter {

    //private final Cursor dataC;
    private final LayoutInflater vi;
    public View theView;

    public tripListAdapter(Context con, Cursor c) {
        // super constructor thingy
        super(con, c);
        //dataC = c;
        vi = LayoutInflater.from(con);

    }

    @Override
    public View getView (int position, View v, ViewGroup parent){

        Log.i(LOGTAG,"In getView...");
        // Create a message handling object as an anonymous class.
        //mTripClickHandler = new OnItemClickListener() {
        v = vi.inflate(R.layout.trip_row, parent, false);

        Log.i(LOGTAG,"Leaving getView...");
        return v;
    }

    @Override
    public void bindView(View v, Context arg1, Cursor dataC) {
        Log.i(LOGTAG,"In bindView...");
        // get data from cursor
        TextView dateTV = (TextView) v.findViewById(R.id.dateFld);
        TextView distTV = (TextView) v.findViewById(R.id.distFld);
        TextView mpgTV = (TextView) v.findViewById(R.id.mpgFld);

        String tmpMPG = dataC.getString(dataC.getColumnIndexOrThrow(dbHistory.TRIP_MPG));
        Double tmpNum = Double.valueOf(tmpMPG);
        tmpMPG = String.format("%.2f", tmpNum);

        dateTV.setText(dataC.getString(dataC.getColumnIndexOrThrow(dbHistory.TRIP_DATE)));
        distTV.setText(dataC.getString(dataC.getColumnIndexOrThrow(dbHistory.TRIP_MILES)));
        mpgTV.setText(tmpMPG);                      

        Log.i(LOGTAG,"Leaving bindView...");
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        Log.i(LOGTAG,"In newView...");
        //theView = vi.inflate(R.layout.trip_row, arg2, false);

        Log.i(LOGTAG,"Leaving newView...");
        return theView;
    }
}

logCat的输出显示了各种日志消息,但没有来自bindView,我想这就是我的listView为空的原因。

06-17 06:40:31.827: I/WIBBLE(866): About to create listView...
06-17 06:40:31.827: I/WIBBLE(866): ListView created...
06-17 06:40:31.838: I/WIBBLE(866): About to create adapter...
06-17 06:40:31.838: I/WIBBLE(866): OnResume with this many rows returned by cursor: 3
06-17 06:40:31.838: I/WIBBLE(866): In the loop, so cursor is not null....
06-17 06:40:31.897: I/WIBBLE(866): In getView...
06-17 06:40:31.917: I/WIBBLE(866): Leaving getView...
06-17 06:40:31.917: I/WIBBLE(866): In getView...
06-17 06:40:31.937: I/WIBBLE(866): Leaving getView...
06-17 06:40:31.957: I/WIBBLE(866): In getView...
06-17 06:40:31.967: I/WIBBLE(866): Leaving getView...
06-17 06:40:32.137: I/WIBBLE(866): In getView...
06-17 06:40:32.217: I/WIBBLE(866): Leaving getView...
06-17 06:40:32.279: I/WIBBLE(866): In getView...
06-17 06:40:32.347: I/WIBBLE(866): Leaving getView...
06-17 06:40:32.417: I/WIBBLE(866): In getView...
06-17 06:40:32.427: I/WIBBLE(866): Leaving getView...
06-17 06:40:32.959: I/Choreographer(866): Skipped 116 frames!  The application may be doing too much work on its main thread.
06-17 06:40:34.056: I/WIBBLE(866): In getView...
06-17 06:40:34.147: I/WIBBLE(866): Leaving getView...
06-17 06:40:34.197: I/WIBBLE(866): In getView...
06-17 06:40:34.267: I/WIBBLE(866): Leaving getView...
06-17 06:40:34.289: I/WIBBLE(866): In getView...
06-17 06:40:34.308: I/WIBBLE(866): Leaving getView...
06-17 06:40:39.486: I/WIBBLE(866): Dist = 
06-17 06:40:39.486: I/WIBBLE(866): Date = 
06-17 06:40:39.496: I/WIBBLE(866): mpg = 

任何想法为什么?

2 个答案:

答案 0 :(得分:0)

这是一个适合我的样本:

private class CurAdapter extends CursorAdapter{


        boolean imageExists;


        public CurAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);

        }


        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView tv = (TextView) view.findViewById(R.id.textView1);
            TextView tv1 = (TextView) view.findViewById(R.id.textView2);
            ImageView iv = (ImageView) view.findViewById(R.id.imageView1);


            //  String isAdhoc = (cursor.getString(cursor.getColumnIndexOrThrow("isAdhoc")));
            String name = (cursor.getString(cursor.getColumnIndexOrThrow("Name")));
            String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
            String manuplate = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("BirthDate")));


            //  if(isAdhoc.equals("1")){

            tv.setText(name);


            if(manuplate.contains("0001")){
                manuplate = manuplate.replace("0001", "");
            }
            tv1.setText((manuplate));
            tv.setTypeface(tf, Typeface.BOLD);
            tv1.setTypeface(tf1); 

            //  System.out.println("Image Uri is:"+image);


            if(image.contains("jpg") || image.contains("png")){
                File f = new File(image);
                Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
                iv.setImageBitmap(bmp); 




            }else{
                Uri IMAGE_URI = null;
                IMAGE_URI = Uri.parse(image);
                IMAGE_URI= Uri.withAppendedPath(IMAGE_URI, Contacts.Photo.CONTENT_DIRECTORY);
                try{
                    iv.setImageURI(IMAGE_URI);
                    iv.setScaleType(ScaleType.CENTER_CROP); 


                }catch (Exception e){
                    imageExists = false; 

                }
            }
            if (!imageExists){

                iv.setBackgroundResource(R.drawable.default_img);
                iv.setScaleType(ScaleType.CENTER_CROP); 
                //      }
            }
        }
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View view = LayoutInflater.from(context).inflate(R.layout.contacts_list, null);

            /*  view.setTag(R.id.textView1, tv);
            view.setTag(R.id.textView2, tv1);
            view.setTag(R.id.imageView1, iv); */
            return view;

        }
}

答案 1 :(得分:0)

对于基于Cursor的适配器,实施getView()方法以调用newView()bindView()方法。如果你像你一样覆盖它,而不调用super()那些方法就不会被调用。因此,请删除已覆盖的getView()方法,并在newView()方法中对行布局进行充气。

此外,对于基于Cursor的适配器,您通常不会覆盖getView()方法,而是使用需要覆盖的两个委托方法。