0

为什么我运行我的应用程序时只显示一个列表项?!

这是我的 MainActivity 类的源代码。该应用程序很好地添加了一个列表项,但在那之后,它只是不显示其余的项目。

public class MainActivity extends BaseActivity {

    public static Context appContext;
    private final String TAG = "Main Activity";

    // LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
    ArrayList<String> listItems;

    // DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
    ArrayAdapter<String> adapter;

    Button addItem;
    ListView courseList;
    EditText input2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ActionBar gets initiated
        ActionBar actionbar;
        actionbar = getSupportActionBar();
        // Tell the ActionBar we want to use Tabs.
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // initiating both tabs and set text to it.
        ActionBar.Tab assignTab = actionbar.newTab().setText("Assignments");
        ActionBar.Tab schedTab = actionbar.newTab().setText("Schedule");
        ActionBar.Tab contactTab = actionbar.newTab().setText("Contact");

        // Create three fragments to display content
        Fragment assignFragment = new Assignments();
        Fragment schedFragment = new Schedule();
        Fragment contactFragment = new Contact();

        assignTab.setTabListener(new MyTabsListener(assignFragment));
        schedTab.setTabListener(new MyTabsListener(schedFragment));
        contactTab.setTabListener(new MyTabsListener(contactFragment));

        actionbar.addTab(assignTab);
        actionbar.addTab(schedTab);
        actionbar.addTab(contactTab);
        listItems = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItems);
        addItem = (Button) findViewById(R.id.button1);
        courseList = (ListView) findViewById(R.id.listView1);
        input2 = (EditText) findViewById(R.id.editText1);
        addItem.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (!(input2.getText().toString()).equals("")) {
                    listItems.add(input2.getText().toString());
                    courseList.setAdapter(adapter);
                }
                input2.setVisibility(View.INVISIBLE);
                addItem.setVisibility(View.INVISIBLE);
            }
        });
        final Intent intent = new Intent(this, Detail.class);
        courseList.setOnItemClickListener(new     AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0,     View arg1,
                            int arg2, long arg3) {
                        if (isTablet(MainActivity.this)) {

                        } else {
                            startActivity(intent);
                        }

                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.add_item:
            input2.setVisibility(View.VISIBLE);
            input2.requestFocus();
            addItem.setVisibility(View.VISIBLE);
            Log.i(TAG, "Add Clicked");
            return true;
        case R.id.calendar:
            Log.i(TAG, "Calendar Clicked");
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public boolean isTablet(Context context) {
        boolean xlarge =     ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
        boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        return (xlarge || large);
    }
}

这只发生在我动态添加片段之后。片段显示正常,但自从我在平板电脑视图中使列表项不可点击后,它们就不会出现。它与此有关吗?

4

1 回答 1

0

adapter.notifyDataSetChanged()将项目添加到列表后,您需要添加到代码中。另外请设置一次适配器 - 每次用户单击按钮时都无需执行该调用

于 2012-11-08T04:13:32.690 回答