Search Results

Search found 202 results on 9 pages for 'arrayadapter'.

Page 9/9 | < Previous Page | 5 6 7 8 9 

  • How to save/retrieve words to/from SQlite database?

    - by user998032
    Sorry if I repeat my question but I have still had no clues of what to do and how to deal with the question. My app is a dictionary. I assume that users will need to add words that they want to memorise to a Favourite list. Thus, I created a Favorite button that works on two phases: short-click to save the currently-view word into the Favourite list; and long-click to view the Favourite list so that users can click on any words to look them up again. I go for using a SQlite database to store the favourite words but I wonder how I can do this task. Specifically, my questions are: Should I use the current dictionary SQLite database or create a new SQLite database to favorite words? In each case, what codes do I have to write to cope with the mentioned task? Could anyone there kindly help? Here is the dictionary code: package mydict.app; import java.util.ArrayList; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.util.Log; public class DictionaryEngine { static final private String SQL_TAG = "[MyAppName - DictionaryEngine]"; private SQLiteDatabase mDB = null; private String mDBName; private String mDBPath; //private String mDBExtension; public ArrayList<String> lstCurrentWord = null; public ArrayList<String> lstCurrentContent = null; //public ArrayAdapter<String> adapter = null; public DictionaryEngine() { lstCurrentContent = new ArrayList<String>(); lstCurrentWord = new ArrayList<String>(); } public DictionaryEngine(String basePath, String dbName, String dbExtension) { //mDBExtension = getResources().getString(R.string.dbExtension); //mDBExtension = dbExtension; lstCurrentContent = new ArrayList<String>(); lstCurrentWord = new ArrayList<String>(); this.setDatabaseFile(basePath, dbName, dbExtension); } public boolean setDatabaseFile(String basePath, String dbName, String dbExtension) { if (mDB != null) { if (mDB.isOpen() == true) // Database is already opened { if (basePath.equals(mDBPath) && dbName.equals(mDBName)) // the opened database has the same name and path -> do nothing { Log.i(SQL_TAG, "Database is already opened!"); return true; } else { mDB.close(); } } } String fullDbPath=""; try { fullDbPath = basePath + dbName + "/" + dbName + dbExtension; mDB = SQLiteDatabase.openDatabase(fullDbPath, null, SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.NO_LOCALIZED_COLLATORS); } catch (SQLiteException ex) { ex.printStackTrace(); Log.i(SQL_TAG, "There is no valid dictionary database " + dbName +" at path " + basePath); return false; } if (mDB == null) { return false; } this.mDBName = dbName; this.mDBPath = basePath; Log.i(SQL_TAG,"Database " + dbName + " is opened!"); return true; } public void getWordList(String word) { String query; // encode input String wordEncode = Utility.encodeContent(word); if (word.equals("") || word == null) { query = "SELECT id,word FROM " + mDBName + " LIMIT 0,15" ; } else { query = "SELECT id,word FROM " + mDBName + " WHERE word >= '"+wordEncode+"' LIMIT 0,15"; } //Log.i(SQL_TAG, "query = " + query); Cursor result = mDB.rawQuery(query,null); int indexWordColumn = result.getColumnIndex("Word"); int indexContentColumn = result.getColumnIndex("Content"); if (result != null) { int countRow=result.getCount(); Log.i(SQL_TAG, "countRow = " + countRow); lstCurrentWord.clear(); lstCurrentContent.clear(); if (countRow >= 1) { result.moveToFirst(); String strWord = Utility.decodeContent(result.getString(indexWordColumn)); String strContent = Utility.decodeContent(result.getString(indexContentColumn)); lstCurrentWord.add(0,strWord); lstCurrentContent.add(0,strContent); int i = 0; while (result.moveToNext()) { strWord = Utility.decodeContent(result.getString(indexWordColumn)); strContent = Utility.decodeContent(result.getString(indexContentColumn)); lstCurrentWord.add(i,strWord); lstCurrentContent.add(i,strContent); i++; } } result.close(); } } public Cursor getCursorWordList(String word) { String query; // encode input String wordEncode = Utility.encodeContent(word); if (word.equals("") || word == null) { query = "SELECT id,word FROM " + mDBName + " LIMIT 0,15" ; } else { query = "SELECT id,content,word FROM " + mDBName + " WHERE word >= '"+wordEncode+"' LIMIT 0,15"; } //Log.i(SQL_TAG, "query = " + query); Cursor result = mDB.rawQuery(query,null); return result; } public Cursor getCursorContentFromId(int wordId) { String query; // encode input if (wordId <= 0) { return null; } else { query = "SELECT id,content,word FROM " + mDBName + " WHERE Id = " + wordId ; } //Log.i(SQL_TAG, "query = " + query); Cursor result = mDB.rawQuery(query,null); return result; } public Cursor getCursorContentFromWord(String word) { String query; // encode input if (word == null || word.equals("")) { return null; } else { query = "SELECT id,content,word FROM " + mDBName + " WHERE word = '" + word + "' LIMIT 0,1"; } //Log.i(SQL_TAG, "query = " + query); Cursor result = mDB.rawQuery(query,null); return result; } public void closeDatabase() { mDB.close(); } public boolean isOpen() { return mDB.isOpen(); } public boolean isReadOnly() { return mDB.isReadOnly(); } } And here is the code below the Favourite button to save to and load the Favourite list: btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite); btnAddFavourite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Add code here to save the favourite, e.g. in the db. Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT); toast.show(); } }); btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // Open the favourite Activity, which in turn will fetch the saved favourites, to show them. Intent intent = new Intent(getApplicationContext(), FavViewFavourite.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(intent); return false; } }); }

    Read the article

  • How do I search the MediaStore for a specific directory instead of entire external storage?

    - by Nick Lopez
    In my app I have an option that allows users to browse for audio files on their phone to add to the app. I am having trouble however with creating a faster way of processing the query code. Currently it searches the entire external storage and causes the phone to prompt a force close/wait warning. I would like to take the code I have posted below and make it more efficient by either searching in a specific folder on the phone or by streamlining the process to make the file search quicker. I am not sure how to do this however. Thanks! public class BrowseActivity extends DashboardActivity implements OnClickListener, OnItemClickListener { private List<Sound> soundsInDevice = new ArrayList<Sound>(); private List<Sound> checkedList; private ListView browsedList; private BrowserSoundAdapter adapter; private long categoryId; private Category category; private String currentCategoryName; private String description; // private Category newCategory ; private Button doneButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_browse); checkedList = new ArrayList<Sound>(); browsedList = (ListView) findViewById(android.R.id.list); doneButton = (Button) findViewById(R.id.doneButton); soundsInDevice = getMediaSounds(); if (soundsInDevice.size() > 0) { adapter = new BrowserSoundAdapter(this, R.id.browseSoundName, soundsInDevice); } else { Toast.makeText(getApplicationContext(), getString(R.string.no_sounds_available), Toast.LENGTH_SHORT) .show(); } browsedList.setAdapter(adapter); browsedList.setOnItemClickListener(this); doneButton.setOnClickListener(this); } private List<Sound> getMediaSounds() { List<Sound> mediaSoundList = new ArrayList<Sound>(); ContentResolver cr = getContentResolver(); String[] projection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DURATION}; final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Log.v("MediaStore.Audio.Media.EXTERNAL_CONTENT_URI", "" + uri); final Cursor cursor = cr.query(uri, projection, null, null, null); int n = cursor.getCount(); Log.v("count", "" + n); if (cursor.moveToFirst()) { do { String soundName = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); Log.v("soundName", "" + soundName); String title = cursor .getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); Log.v("title", "" + title); String path = cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); Log.v("path", "" + path); Sound browsedSound = new Sound(title, path, false, false, false, false, 0); Log.v("browsedSound", "" + browsedSound); mediaSoundList.add(browsedSound); Log.v("mediaSoundList", "" + mediaSoundList.toString()); } while (cursor.moveToNext()); } return mediaSoundList; } public class BrowserSoundAdapter extends ArrayAdapter<Sound> { public BrowserSoundAdapter(Context context, int textViewResourceId, List<Sound> objects) { super(context, textViewResourceId, objects); } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; View view = convertView; LayoutInflater inflater = getLayoutInflater(); if (view == null) { view = inflater.inflate(R.layout.list_item_browse, null); viewHolder = new ViewHolder(); viewHolder.soundNameTextView = (TextView) view .findViewById(R.id.browseSoundName); viewHolder.pathTextView = (TextView) view .findViewById(R.id.browseSoundPath); viewHolder.checkToAddSound = (CheckBox) view .findViewById(R.id.browse_checkbox); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } final Sound sound = soundsInDevice.get(position); if (sound.isCheckedState()) { viewHolder.checkToAddSound.setChecked(true); } else { viewHolder.checkToAddSound.setChecked(false); } viewHolder.soundNameTextView.setText(sound.getName()); viewHolder.pathTextView.setText(sound.getUri()); viewHolder.checkToAddSound .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { CheckBox cb = (CheckBox) v .findViewById(R.id.browse_checkbox); boolean checked = cb.isChecked(); boolean newValue = checked; updateView(position, newValue); doneButtonStatus(checkedList.size()); } }); return view; } } // Adapter view holder class private class ViewHolder { private TextView soundNameTextView; private TextView pathTextView; private CheckBox checkToAddSound; } // done button On Click @Override public void onClick(View view) { boolean status = getIntent().getBooleanExtra("FromAddCat", false); Log.v("for add category","enters in if"); if(status){ Log.v("for add category","enters in if1"); currentCategoryName = getIntent().getStringExtra("categoryName"); description = getIntent().getStringExtra("description"); boolean existCategory = SQLiteHelper.getCategoryStatus(currentCategoryName); if (!existCategory) { category = new Category(currentCategoryName, description, false); category.insert(); category.update(); Log.v("for add category","enters in if2"); } }else{ categoryId = getIntent().getLongExtra("categoryId",-1); category = SQLiteHelper.getCategory(categoryId); } for (Sound checkedsound : checkedList) { checkedsound.setCheckedState(false); checkedsound.insert(); category.getSounds().add(checkedsound); final Intent intent = new Intent(this, CategoriesActivity.class); finish(); startActivity(intent); } } @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { boolean checked = true; boolean newValue = false; CheckBox cb = (CheckBox) view.findViewById(R.id.browse_checkbox); if (cb.isChecked()) { cb.setChecked(!checked); newValue = !checked; } else { cb.setChecked(checked); newValue = checked; } updateView(position, newValue); doneButtonStatus(checkedList.size()); } private void doneButtonStatus(int size) { if (size > 0) { doneButton.setEnabled(true); doneButton.setBackgroundResource(R.drawable.done_button_drawable); } else { doneButton.setEnabled(false); doneButton.setBackgroundResource(R.drawable.done_btn_disabled); } } private void updateView(int index, boolean newValue) { System.out.println(newValue); Sound sound = soundsInDevice.get(index); if (newValue == true) { checkedList.add(sound); sound.setCheckedState(newValue); } else { checkedList.remove(sound); sound.setCheckedState(newValue); } } }

    Read the article

< Previous Page | 5 6 7 8 9