Android Open Source - TymyApp Site List Fragment






From Project

Back to project page TymyApp.

License

The source code is released under:

GNU General Public License

If you think the Android project TymyApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cz.tymy.api.tymyapp;
/*  w w w  .  j ava2s. c  o  m*/
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * List Sites from database.
 */
public class SiteListFragment extends ListFragment {
    private static final int MENU_EDIT_ID = 0;
    private static final int MENU_DELETE_ID = 1;
    OnSiteClickedListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_site_list, container, false);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerForContextMenu(getListView());
        updateList();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        listener.onSiteClicked(id);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (OnSiteClickedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnSiteClickedListener");
        }
    }

    public void updateList() {
        Context ctx = getActivity();
        Sites sites = new Sites(ctx);

        String[] from = { Sites.COLUMN_NAME };
        int[] to = { android.R.id.text1 };

        ListAdapter adapter = new SimpleCursorAdapter(ctx,
                android.R.layout.simple_list_item_1, sites.getSites(), from,
                to, 0);

        setListAdapter(adapter);

        sites.close();
    }


    // Create Context menu -> Long Click on list item
    @Override
    public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, MENU_EDIT_ID, 0, R.string.edit);
        menu.add(0, MENU_DELETE_ID, 0, R.string.delete);
    }

    // Process Context menu selected
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case MENU_DELETE_ID:
                deleteSite(info.id);
                return true;
            case MENU_EDIT_ID:
                listener.onSiteEdit(info.id);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

    /**
     * Delete Site from Database
     * @param id db _id
     */
    private void deleteSite(long id){
        Context ctx = getActivity();
        Sites notes = new Sites(ctx);

        if(notes.deleteSite(id)){
            Toast.makeText(ctx, R.string.site_deleted, Toast.LENGTH_SHORT).show();
            updateList();
        } else{
            Toast.makeText(ctx, R.string.site_not_deleted, Toast.LENGTH_SHORT).show();
        }
    }

    // Interface for list Item click, implemented in parent Activity
    public static interface OnSiteClickedListener {
        public void onSiteClicked(long id);
        public void onSiteEdit(long id);
    }
}




Java Source Code List

cz.tymy.api.tymyapp.AddSiteActivity.java
cz.tymy.api.tymyapp.AddSiteFragment.java
cz.tymy.api.tymyapp.ApiLoader.java
cz.tymy.api.tymyapp.ApplicationTest.java
cz.tymy.api.tymyapp.DiscussionAdapter.java
cz.tymy.api.tymyapp.DiscussionDetailActivity.java
cz.tymy.api.tymyapp.DiscussionDetailFragment.java
cz.tymy.api.tymyapp.DiscussionListActivity.java
cz.tymy.api.tymyapp.DiscussionListFragment.java
cz.tymy.api.tymyapp.HttpClient.java
cz.tymy.api.tymyapp.PostAdapter.java
cz.tymy.api.tymyapp.SiteListActivity.java
cz.tymy.api.tymyapp.SiteListFragment.java
cz.tymy.api.tymyapp.Sites.java
cz.tymy.api.tymyapp.TymyApplication.java
cz.tymy.api.tymyapp.apimodel.ApiDsPost.java
cz.tymy.api.tymyapp.apimodel.ApiDs.java
cz.tymy.api.tymyapp.apimodel.ApiException.java
cz.tymy.api.tymyapp.apimodel.ApiMsg.java
cz.tymy.api.tymyapp.apimodel.Api.java
cz.tymy.api.tymyapp.apimodel.DsDetail.java
cz.tymy.api.tymyapp.apireaders.ApiReader.java