Java tutorial
/** * Copyright (C) 2016 Zakoota@github.com * <p> * Licensed under the * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 * International License Version 2.0 (the "License") * <p> * You are free to: * <p> * 1: Share copy and redistribute the material in any medium or format * 2: Adapt remix, transform, and build upon the material * <p> * The licensor cannot revoke these freedoms as long as you follow the license terms. * <p> * 1: Attribution You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. * 2: NonCommercial You may not use the material for commercial purposes. * 3: ShareAlike If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. * 4: No additional restrictions You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. * <p> * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * https://creativecommons.org/licenses/by-nc-sa/4.0/ */ package com.raza.betternts.activities; import android.app.AlertDialog; import android.content.ClipData; import android.content.ClipboardManager; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.raza.betternts.R; import com.raza.betternts.contentprovider.NTSPostsContract; import com.raza.betternts.database.DbSchema; import com.raza.betternts.declarations.CustomCursorAdapter; /** * Ignored tabs where app stores removed entries by user * Created by Raza on 23-Nov-16. */ public class TabFragment2 extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { private final String list[] = { "Open in browser", "Copy", "Share", "Restore" }; private CustomCursorAdapter adapter; private ListView lvPosts; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab_fragment_ignored, container, false); lvPosts = (ListView) view.findViewById(R.id.lv_posts_ignored); fillData(); { lvPosts.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) { /** * This is magic right here wow, thanks stackexchange */ final Cursor c = (Cursor) parent.getAdapter().getItem(position); String name = c.getString(c.getColumnIndex(DbSchema.COL_NAME)); //Show dialog for choices AlertDialog.Builder inputDialog = new AlertDialog.Builder(getContext()); inputDialog.setTitle(name).setItems(list, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { menuOption(c, which); } }); inputDialog.show(); } }); lvPosts.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { /** * This is magic right here wow, thanks stackexchange */ final Cursor c = (Cursor) parent.getAdapter().getItem(position); String name = c.getString(c.getColumnIndex(DbSchema.COL_NAME)); //Show dialog for choices AlertDialog.Builder inputDialog = new AlertDialog.Builder(getContext()); inputDialog.setTitle(name).setItems(list, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { menuOption(c, which); } }); inputDialog.show(); return true; } }); } return view; } private void fillData() { getLoaderManager().initLoader(0, null, this); adapter = new CustomCursorAdapter(getContext(), null); lvPosts.setAdapter(adapter); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { CursorLoader cursorLoader = new CursorLoader(TabFragment2.this.getContext(), NTSPostsContract.Ignored.CONTENT_URI, NTSPostsContract.Ignored.PROJECTION_ALL, null, null, NTSPostsContract.Ignored.SORT_ORDER); return cursorLoader; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { adapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { adapter.swapCursor(null); } private void menuOption(Cursor cursor, int selectedOption) { String name = cursor.getString(cursor.getColumnIndex(DbSchema.COL_NAME)); String url = cursor.getString(cursor.getColumnIndex(DbSchema.COL_URL)); switch (selectedOption) { //Open in browser case 0: { Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browse); break; } //Copy to clipboard case 1: { ClipboardManager clipboard = (ClipboardManager) getActivity() .getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText(name, url); clipboard.setPrimaryClip(clip); Toast.makeText(getContext(), "Link copied to clipboard", Toast.LENGTH_SHORT).show(); break; } //Share case 2: { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, url); sendIntent.setType("text/plain"); startActivity(sendIntent); break; } case 3: { moveToMain(cursor); Toast.makeText(getContext(), name + "\nrestored to Vacancies tab", Toast.LENGTH_SHORT).show(); } } } private void moveToMain(Cursor cursor) { int id = cursor.getInt(cursor.getColumnIndex(DbSchema.COL_ID)); String name = cursor.getString(cursor.getColumnIndex(DbSchema.COL_NAME)); String url = cursor.getString(cursor.getColumnIndex(DbSchema.COL_URL)); String lastDate = cursor.getString(cursor.getColumnIndex(DbSchema.COL_LAST_DATE)); String creationDate = cursor.getString(cursor.getColumnIndex(DbSchema.COL_CREATION_DATE)); ContentValues values = new ContentValues(); values.put(DbSchema.COL_NAME, name); values.put(DbSchema.COL_URL, url); values.put(DbSchema.COL_LAST_DATE, lastDate); values.put(DbSchema.COL_CREATION_DATE, creationDate); int i = getActivity().getContentResolver().delete(NTSPostsContract.Ignored.CONTENT_URI, "_id = ?", new String[] { id + "", }); if (i > 0) { getActivity().getContentResolver().insert(NTSPostsContract.Posts.CONTENT_URI, values); } } }