Back to project page Android-Apps.
The source code is released under:
Apache License
If you think the Android project Android-Apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.kniezrec.xbmcgear.presentation; /*from w w w .j av a 2s . c o m*/ import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.ListView; import com.kniezrec.xbmcgear.R; import com.kniezrec.xbmcgear.preferences.Host; import com.kniezrec.xbmcgear.preferences.HostsDataSource; import com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil; public class InstanceActivity extends ActionBarActivity implements AdapterView.OnItemClickListener { private ListView list; private ArrayAdapter<Host> adapter = null; private HostsDataSource dataSource = null; private int activeRow = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_instance); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); if (toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } dataSource = HostsDataSource.getInstance(); adapter = new ArrayAdapter<Host>(this, android.R.layout.simple_list_item_single_choice, dataSource.getDbMirror()); list = (ListView) findViewById(R.id.listInstances); list.setAdapter(adapter); if (adapter.getCount() == 1) { activeRow = 0; } else if (adapter.getCount() > 1) { activeRow = dataSource.getPosition(SharedPreferencesUtil .getActiveID()); } list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); list.setItemChecked(activeRow, true); list.setOnItemClickListener(this); registerForContextMenu(list); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_activity_instance, menu); return true; } @Override public void onResume() { super.onResume(); adapter.notifyDataSetChanged(); if (adapter.getCount() == 1) { activeRow = 0; Host h = adapter.getItem(0); SharedPreferencesUtil.changePreferences(h); list.setItemChecked(activeRow, true); } else if (adapter.getCount() == 0) { SharedPreferencesUtil.clear(); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { if (v.getId() == R.id.listInstances) { getMenuInflater().inflate(R.menu.context_menu, menu); } } @Override public boolean onOptionsItemSelected(MenuItem item) { Intent intent; switch (item.getItemId()) { case android.R.id.home: super.onBackPressed(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); break; case R.id.mi_new_host: intent = new Intent(InstanceActivity.this, HostSetActivity.class); InstanceActivity.this.startActivity(intent); this.overridePendingTransition(R.anim.slide, R.anim.sout); return true; case R.id.mi_search_nsd: intent = new Intent(InstanceActivity.this, AutoConfigurationActivity.class); intent.putExtra(AutoConfigurationActivity.SHOW_HOME, true); InstanceActivity.this.startActivity(intent); InstanceActivity.this.overridePendingTransition(R.anim.slide, R.anim.sout); break; default: break; } return super.onOptionsItemSelected(item); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { super.onBackPressed(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); switch (item.getItemId()) { case R.id.setActive: activeRow = info.position; Host h = adapter.getItem(activeRow); SharedPreferencesUtil.changePreferences(h); break; case R.id.deleteHost: dataSource.removeHost(adapter.getItem(info.position)); adapter.notifyDataSetChanged(); if (activeRow == info.position) { if (adapter.getCount() == 0) { SharedPreferencesUtil.clear(); } else { SharedPreferencesUtil.changePreferences(adapter .getItem(0)); } } activeRow = 0; break; default: return super.onContextItemSelected(item); } list.setItemChecked(activeRow, true); return true; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (activeRow != position) { list.setItemChecked(activeRow, true); } Intent intent = new Intent(this, HostSetActivity.class); Host hostToSend = adapter.getItem(position); intent.putExtra(HostSetActivity.INSTANCE_NAME, hostToSend); startActivity(intent); overridePendingTransition(R.anim.slide, R.anim.sout); } }