Android程序不会在BaseAdapter类中执行getView函数

时间:2014-06-26 06:36:55

标签: java android baseadapter

这是一个使用BaseAdapter使用自定义布局的简单程序。除了getView函数之外,Logcat显示已执行所有其他函数。在运行应用程序时,我只是收到一个空白屏幕。

以下是MainActivity.java中的代码

public class MainActivity extends Activity {

    public final String TAG = "Task:";

    //Objects for the arraylist is created from this class.
    public class codeLearnChapter {

        String chapterName;
        String chapterDescription;
    }

    //Defined Custome Adapter.
    MyAdapter mMyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_simple_list);
        Log.i(TAG, "listview is set as contentview");

        //Initialized custom adapter.
        mMyAdapter = new MyAdapter();
        Log.i(TAG,"New adapter mMyAdapter created");

        ListView codeLearnLessons = (ListView) findViewById(R.id.listId);
        Log.i(TAG,"ListView is inflated");

        codeLearnLessons.setAdapter(mMyAdapter);
        Log.i(TAG, "mMyAdapter is set to ListView codeLearnLessons");
    }

    public class MyAdapter extends BaseAdapter{

        //Data for the arraylist codeLearnChapterList is obtained from getDataForListView() method.
        List<codeLearnChapter> codeLearnChapterList = getDataForListView();

        @Override
        public int getCount() {

            Log.i(TAG, "Arraylist size is sent");
            return codeLearnChapterList.size();

        }

        @Override
        public Object getItem(int arg0) {

            Log.i(TAG, "Object of particular row is sent");
            return codeLearnChapterList.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            if(arg1==null){

                LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                arg1 = inflater.inflate(R.layout.listitem, arg2, false);
                Log.i(TAG, "row of listitem is inflater");
            }

            TextView mChapterName = (TextView) arg1.findViewById(R.id.textView1);
            Log.i(TAG,"main description is referenced to its value");

            TextView mChapterDescription = (TextView) arg1.findViewById(R.id.textView2);
            Log.i(TAG, "sub description is referenced to its value");

            codeLearnChapter mcodeLearnChapter = codeLearnChapterList.get(arg0);
            Log.i(TAG, "particular of object corresponding to row arg0 is passed to object mcodeLearnChapter");

            mChapterName.setText(mcodeLearnChapter.chapterName);
            Log.i(TAG, "chapterName is Set");

            mChapterDescription.setText(mcodeLearnChapter.chapterDescription);
            Log.i(TAG,"chapterDescription is Set");

            return arg1;
        } 

    }

    public List<codeLearnChapter> getDataForListView(){

        List<codeLearnChapter> codeLearnChapterList = new ArrayList<codeLearnChapter>();

        //The arraylist codeLearnChapterList is populated with 3 objects.
        for(int i=10;i<3;i++){

            codeLearnChapter mcodeLearnChapter = new codeLearnChapter();            
            mcodeLearnChapter.chapterName = "Chapter"+ i;
            mcodeLearnChapter.chapterDescription = "Description for Chapter"+i;

            codeLearnChapterList.add(mcodeLearnChapter);
        }

        Log.i(TAG, "Data (List of objects ) populated in arraylist ");
        return codeLearnChapterList;

    }

}

这是logcat输出

06-26 01:41:15.815: I/Task:(1403): listview is set as contentview
06-26 01:41:15.825: I/Task:(1403): Data (List of objects ) populated in arraylist 
06-26 01:41:15.825: I/Task:(1403): New adapter mMyAdapter created
06-26 01:41:15.835: I/Task:(1403): ListView is inflated
06-26 01:41:15.835: I/Task:(1403): Arraylist size is sent
06-26 01:41:15.845: I/Task:(1403): Arraylist size is sent
06-26 01:41:15.845: I/Task:(1403): Arraylist size is sent
06-26 01:41:15.845: I/Task:(1403): mMyAdapter is set to ListView codeLearnLessons
06-26 01:41:16.055: I/Task:(1403): Arraylist size is sent
06-26 01:41:16.055: I/Task:(1403): Arraylist size is sent
06-26 01:41:16.705: I/Task:(1403): Arraylist size is sent
06-26 01:41:16.705: I/Task:(1403): Arraylist size is sent

我不确定这是Java还是Android问题,因为我对编程比较陌生。在我过去几天阅读代码的过程中,澄清一下会非常有帮助。

1 个答案:

答案 0 :(得分:2)

<强>问题:

for(int i=10;i<3;i++){

您的ArrayList未填充对象,因此getView未被调用,因为它是空的。

您在上面的代码中所做的是您将 i 初始化为10,这将立即结束 forloop

<强>溶液

如果要用3个对象填充数组,请将其更改为零。

for(int i=0;i<3;i++){