为什么我运行应用时只显示一个列表项??
以下是我的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 :(得分:0)
将项目添加到列表后,您需要将adapter.notifyDataSetChanged()
添加到代码中。另外,请设置适配器一次 - 每次用户单击按钮