Android Open Source - SpeechWriter Drag N Drop Adapter






From Project

Back to project page SpeechWriter.

License

The source code is released under:

MIT License

If you think the Android project SpeechWriter 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

/*
 * Copyright (C) 2010 Eric Harlow/*from  ww  w.ja  va  2s.  c  om*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * THE ORIGINAL FILE WAS MODIFIED
 */

package com.ericharlow.DragNDrop;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public final class DragNDropAdapter<E> extends BaseAdapter implements RemoveListener, DropListener{

  private int[] mIds;
    private int[] mLayouts;
    private LayoutInflater mInflater;
    private List<E> mContent;

    public DragNDropAdapter(Context context, List<E> content) {
        init(context,new int[]{android.R.layout.simple_list_item_1},new int[]{android.R.id.text1}, content);
    }
    
    public DragNDropAdapter(Context context, int[] itemLayouts, int[] itemIDs, List<E> content) {
      init(context,itemLayouts,itemIDs, content);
    }

    private void init(Context context, int[] layouts, int[] ids, List<E> content) {
      // Cache the LayoutInflate to avoid asking for a new one each time.
      mInflater = LayoutInflater.from(context);
      mIds = ids;
      mLayouts = layouts;
      mContent = content;
    }
    
    /**
     * The number of items in the list
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
        return mContent.size();
    }

    /**
     * Since the data comes from an array, just returning the index is
     * sufficient to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     *
     * @see android.widget.ListAdapter#getItem(int)
     */
    public E getItem(int position) {
        return mContent.get(position);
    }

    /**
     * Use the array index as a unique id.
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a view to hold each row.
     *
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(mLayouts[0], null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(mIds[0]);

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.text.setText(mContent.get(position).toString());

        return convertView;
    }

    static class ViewHolder {
        TextView text;
    }

  public void onRemove(int which) {
    if (which < 0 || which > mContent.size()) return;    
    mContent.remove(which);
  }

  public void onDrop(int from, int to) {
    E temp = mContent.get(from);
    mContent.remove(from);
    mContent.add(to,temp);
  }

    public void remove(E element) {
        mContent.remove(element);
    }

    public void add(E element) {
        mContent.add(element);
    }
}




Java Source Code List

com.ericharlow.DragNDrop.DragListener.java
com.ericharlow.DragNDrop.DragNDropAdapter.java
com.ericharlow.DragNDrop.DragNDropListView.java
com.ericharlow.DragNDrop.DropListener.java
com.ericharlow.DragNDrop.RemoveListener.java
edu.psu.rcy5017.speechwriter.DatabaseHelper.java
edu.psu.rcy5017.speechwriter.activity.EditTextActivity.java
edu.psu.rcy5017.speechwriter.activity.MainActivity.java
edu.psu.rcy5017.speechwriter.activity.NoteCardListActivity.java
edu.psu.rcy5017.speechwriter.activity.NoteListActivity.java
edu.psu.rcy5017.speechwriter.activity.OptionsActivity.java
edu.psu.rcy5017.speechwriter.activity.SpeechListActivity.java
edu.psu.rcy5017.speechwriter.activity.SpeechRecordingListActivity.java
edu.psu.rcy5017.speechwriter.activity.SplashScreenActivity.java
edu.psu.rcy5017.speechwriter.adapter.TabsPagerAdapter.java
edu.psu.rcy5017.speechwriter.constant.DefaultValues.java
edu.psu.rcy5017.speechwriter.constant.MiscConstants.java
edu.psu.rcy5017.speechwriter.constant.RequestCodes.java
edu.psu.rcy5017.speechwriter.controller.AudioCntl.java
edu.psu.rcy5017.speechwriter.controller.OptionsCntl.java
edu.psu.rcy5017.speechwriter.datasource.DataSource.java
edu.psu.rcy5017.speechwriter.datasource.NoteCardDataSource.java
edu.psu.rcy5017.speechwriter.datasource.NoteDataSource.java
edu.psu.rcy5017.speechwriter.datasource.SpeechDataSource.java
edu.psu.rcy5017.speechwriter.datasource.SpeechRecordingDataSource.java
edu.psu.rcy5017.speechwriter.fragment.NoteCardFragement.java
edu.psu.rcy5017.speechwriter.listener.ChangeFontSizeListener.java
edu.psu.rcy5017.speechwriter.listener.DragListenerImpl.java
edu.psu.rcy5017.speechwriter.listener.DropListenerImpl.java
edu.psu.rcy5017.speechwriter.listener.DropReorderListener.java
edu.psu.rcy5017.speechwriter.listener.RemoveListenerImpl.java
edu.psu.rcy5017.speechwriter.model.NoteCard.java
edu.psu.rcy5017.speechwriter.model.Note.java
edu.psu.rcy5017.speechwriter.model.SpeechRecording.java
edu.psu.rcy5017.speechwriter.model.Speech.java
edu.psu.rcy5017.speechwriter.task.ChangeNoteTextTask.java
edu.psu.rcy5017.speechwriter.task.CreateNoteCardTask.java
edu.psu.rcy5017.speechwriter.task.CreateNoteTask.java
edu.psu.rcy5017.speechwriter.task.CreateSpeechRecordingTask.java
edu.psu.rcy5017.speechwriter.task.CreateSpeechTask.java
edu.psu.rcy5017.speechwriter.task.DeleteTask.java
edu.psu.rcy5017.speechwriter.task.GetAllTask.java
edu.psu.rcy5017.speechwriter.task.NoteCardTask.java
edu.psu.rcy5017.speechwriter.task.NoteTask.java
edu.psu.rcy5017.speechwriter.task.RenameNoteCardTask.java
edu.psu.rcy5017.speechwriter.task.RenameSpeechRecordingTask.java
edu.psu.rcy5017.speechwriter.task.RenameSpeechTask.java
edu.psu.rcy5017.speechwriter.task.SpeechRecordingTask.java
edu.psu.rcy5017.speechwriter.task.SpeechTask.java
edu.psu.rcy5017.speechwriter.task.UpdateOrderTask.java