0

我已经创建了 android 应用程序来使用 ArrayList 中的 BaseAdapter 检索 Listview,但在 Listview 中没有显示任何内容。我的应用程序的描述是我想使用 BaseAdapter 检索列表视图。当我的应用程序在模拟器上运行时,单击模拟器上的菜单按钮时屏幕上不显示任何内容,仅在单击添加启动该活动中的新活动后添加按钮显示在屏幕上,我创建了两个editText。提交后,返回上一个活动并显示列表视图。

请任何人都可以帮我找出这个错误

以下是适配器类

package com.oj2.exlistview;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TimeTrackAdapter extends BaseAdapter {

    ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();
    public Context cntxt;
    public TimeTrackAdapter(Context cnt,ArrayList<TimeRecord> list2) {
        super();
        cntxt = cnt;
        times = list2;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return new TimeTracker().getList1().size(); 
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null){
            System.out.println("in getView");
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.time_list_item, parent,false);
            TimeRecord time = times.get(position);

            TextView timeTextView = (TextView) convertView.findViewById(R.id.timeView);
            timeTextView.setText(time.getTimes());


            TextView noteTextView = (TextView) convertView.findViewById(R.id.noteView);
            noteTextView.setText(time.getNotes());

        }
        return convertView;
    }

}

以下是活动类

 package com.oj2.exlistview;

    import java.util.ArrayList;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.ListView;

    public class TimeTracker extends Activity{
        TimeTrackAdapter timeTrackAdapter;
        ListView listView;
        TimeRecord timerecord;
        public ArrayList<TimeRecord> list1 = new ArrayList<TimeRecord>();

        public static final int TIME_ENTRY_REQUEST_CODE = 1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list);
            System.out.println("in onCreate()");

        }

        public ArrayList<TimeRecord> getList1() {
            return list1;
        }

        public void setList1(ArrayList<TimeRecord> list1) {
            this.list1 = list1;
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // TODO Auto-generated method stub
            super.onCreateOptionsMenu(menu);
            System.out.println("in onCreateOptionsMenu");
            MenuInflater mI = getMenuInflater();

            mI.inflate(R.menu.time_list_menu,menu);
            return true;
        }
        @Override   
        public boolean onMenuItemSelected(int featureId, MenuItem item) {
            // TODO Auto-generated method stub
            System.out.println("in onMenuItemSelected");
            if (item.getItemId()==R.id.add_time_menu_item) {
                Intent intent = new Intent(this, AddTimeActivity.class);
                startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);

                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            System.out.println("in onActivityResult");
            if (requestCode==TIME_ENTRY_REQUEST_CODE) {
                if (resultCode==RESULT_OK) {
                    String Time = data.getStringExtra("time");
                    String Note = data.getStringExtra("note");

                    list1.add(new TimeRecord(Time, Note));

                    timeTrackAdapter = new TimeTrackAdapter(this,list1);
                    listView = (ListView) findViewById(R.id.timeListView);
                    listView.setAdapter(timeTrackAdapter);
                    listView.getAdapter();
                    /* timeTrackAdapter.addTimeRecord(new TimeRecord(Time, Note));*/
                    timeTrackAdapter.notifyDataSetChanged();
                }
            }
        }

    }

以下是 AddTimeActivity

 package com.oj2.exlistview;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;

    public class AddTimeActivity extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_time);
        }

        public void onCancle(View view) {
            finish();
        }
        public void onSave(View view) {
            Intent intent = getIntent();

            EditText TimeView = (EditText) findViewById(R.id.editText1);
            intent.putExtra("time", TimeView.getText().toString());

            EditText Noteview = (EditText)findViewById(R.id.editText2);
            intent.putExtra("note", Noteview.getText().toString());

            this.setResult(RESULT_OK, intent);
            finish();
        }
    }

感谢您

4

1 回答 1

1

首先,不要ActivityAdapter. Activity对象是Android自己创建的,用来展示Views。它与适配器无关。

您要在其中显示数据的布局必须包含一个<ListView ... />带有android:id="@+id/whatEverId"属性的标签。

然后你要做的是使用它的idListView在你的 TimeTracker 活动中检索对象:

ListView lv = (ListView) findViewById(R.id.whatEverId);

然后创建您的适配器并告诉您的 ListView 您希望它显示的视图将由您的自定义适配器创建:

TimeTrackerAdapter adapter = new TimeTrackerAdapter(this, myListOfData);
lv.setAdapter(adapter);

myListOfData是您ArrayList包含的数据。其余的是明确的,应该完成这项工作。

于 2013-01-15T09:13:37.330 回答