开发者

Saving the contents of the ListView after drag and drop

开发者 https://www.devze.com 2023-03-31 03:20 出处:网络
Initially I am having a simple ListView Activity that consists of few formulae and also a Button at the top.Whenever I click that Button it takes me to another Activity,where in I am dragging and drop

Initially I am having a simple ListView Activity that consists of few formulae and also a Button at the top.Whenever I click that Button it takes me to another Activity,where in I am dragging and dropping the contents of the ListView.This Activity also consists a Button and whenever I click this Button the order of the contents which I have changed in my drag and drop Activity has to be saved in my initial Activity.How do I save the contents of ListView which I have changed in my drag and drop to my initial Activity?

Code:My Initial Activity and my next Activity is drag and drop.

   public class FormulaActivity extends Activity implements OnClickListener {

private ListView listview;
private ArrayList<ListContents> mListItem;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);
    ImageButton btn=(ImageButton)findViewById(R.id.imageButton00);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i=new Intent(FormulaActivity.this,DragNDropListActivity.class);
            startActivity(i);


        }
    });
    listview = (ListView) findViewById(R.id.list_view);
    mListItem = ListContents.getItems();
    listview.setAdapter(new ListAdapter(FormulaActivity.this, R.id.list_view,
            mListItem));

}

@Override
public void onClick(View v) {

}

// ***ListAdapter***
private class ListAdapter extends ArrayAdapter<ListContents> { 
    private ArrayList<ListContents> mList; 
    public ListAdapter(Context context, int textViewResourceId,
            ArrayList<ListContents> list) { 
        super(context, textViewResourceId, list);
        this.mList = list;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        try {
            if (view == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.list, null); 
            }
            final ListContents listItem = mList.get(position); 
            if (listItem != null) {
                // setting list_item views
                ((TextView) view.findViewById(R.id.tv_name))
                        .setText(listItem.getName());
                view.setOnClickListener(new OnClickListener() {


        public void onClick(View arg0) { // --clickOnListItem
                        Intent myIntent = new Intent(FormulaActivity.this,
                                Activity2.class);
                        myIntent.putExtra("NAME", listItem.getName());
                        startActivity(myIntent);
                        //finish();
                    }
                });
            }
        } catch (Exception e) {
            Log.i(FormulaActivity.ListAdapter.class.toString(), e.getMessage());
        }
        return view;
    }
}

}


public class DragNDropListActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.dragndroplistview);
    Button mbtn=(Button)findViewById(R.id.button12);
    mbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent i=new Intent(DragNDropListActivity.this,FormulaActivity.class);
        startActivity(i);
        finish();
        }
    });
    ArrayList<String> content = new ArrayList<String>(mListContent.length);
    for (int i=0; i < m开发者_如何学编程ListContent.length; i++) {
        content.add(mListContent[i]);
    }

    setListAdapter(new DragNDropAdapter(this, new int[]{R.layout.dragitem}, new int[]{R.id.TextView01}, content));//new DragNDropAdapter(this,content)
    ListView listView = getListView();

    if (listView instanceof DragNDropListView) {
        ((DragNDropListView) listView).setDropListener(mDropListener);
        ((DragNDropListView) listView).setRemoveListener(mRemoveListener);
        ((DragNDropListView) listView).setDragListener(mDragListener);
    }
}

private DropListener mDropListener = 
    new DropListener() {
    public void onDrop(int from, int to) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            ((DragNDropAdapter)adapter).onDrop(from, to);
            getListView().invalidateViews();
        }
    }
};

private RemoveListener mRemoveListener =
    new RemoveListener() {
    public void onRemove(int which) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            ((DragNDropAdapter)adapter).onRemove(which);
            getListView().invalidateViews();
        }
    }
};

private DragListener mDragListener =
    new DragListener() {

    int backgroundColor = 0xe0103000;

        public void onDrag(int x, int y, ListView listView) {
            // TODO Auto-generated method stub
        }

        public void onStartDrag(View itemView) {
            itemView.setVisibility(View.INVISIBLE);
            itemView.setBackgroundColor(backgroundColor);
            ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
            if (iv != null) iv.setVisibility(View.VISIBLE);
        }

        public void onStopDrag(View itemView) {
            itemView.setVisibility(View.VISIBLE);
            itemView.setBackgroundColor(backgroundColor);
            ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
            if (iv != null) iv.setVisibility(View.VISIBLE);
        }

};

private static String[] mListContent={"BMI", "Infuusberekening: druppels per minuut", "Infuusberekening: resterende tijd","Injecteren: IE-aanduiding", "Injecteren: mg/ml-aanduiding", "Injecteren: %-aanduiding", 
                                    "Lichaamsoppervlakte", "Medicatieberekening voor gewicht",
                                    "Oplossen: Hoeveelheid percentage",
                                    "Oplossen: Hoeveelheid promillage",
                                    "Oplossen: Percentage in oplossing",
                                    "Oplossen: Promillage in oplossing",
                                    "PCA-pomp",
                                    "Procenten: delen",
                                    "Procenten: percentage",
                                    "Promillage: delen",
                                    "Promillage: percentage",
                                    "Spuitenpomp",
                                    "Verdunnen",
                                    "Voedingspomp: ml per uur",
                                    "Voedingspomp: resterende tijd",
                                    "Zuurstofberekening"};

}


Here is your solution :

Declare new String array as

public static String[] mNewPositions; 

Add some codes in your mDropListener

    private DropListener mDropListener = 
        new DropListener() {
    public void onDrop(int from, int to) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            ((DragNDropAdapter)adapter).onDrop(from, to);
            getListView().invalidateViews();

            mNewPositions = new String[adapter.getCount()]; //Initialize your new items storage

                for(int i=0; i < adapter.getCount(); i++) {
                    //Implement here your logic for save positions
                    mNewPositions[i] = adapter.getItem(i).toString();
                }               
        }
    }
};

Now you have mNewPositions with new positioned data. Just access it and use when you want to use that.

Hope it will clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号