2012-11-08 68 views
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); 
    } 
} 

這只是發生。碎片顯示正常,但自從我在平板電腦視圖中使列表項無法點擊後,它們就不會出現。它有什麼關係?

回答

0

將項目添加到列表中後,您需要將adapter.notifyDataSetChanged()添加到您的代碼中。此外請設置您的適配器一次 - 有沒有必要執行每次用戶點擊按鈕時的呼叫

+0

仍然沒有工作,有趣的是,這隻發生在平板電腦視圖。 – aindurti