保存的ListView的内容拖放后 [英] Saving the contents of the ListView after drag and drop

查看:95
本文介绍了保存的ListView的内容拖放后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初我有一个简单的ListView活动,由几个公式,并在top.Whenever我点击它带我到另一个活动,凡在我拖放ListView.This的内容,该按钮一个按钮的活动还包括一个按钮,每当我点击这个按钮的,我在我的拖放活动已经改变了内容的顺序必须被保存在我最初的Activity.How我保存的ListView的,我在我的拖累已更改的内容拖放到我最初的活动?

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:我最初的活动,我的下一个活动就是拖放。

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 < mListContent.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"};

}

推荐答案

下面是你的解决方案:

声明新的String数组

public static String[] mNewPositions; 

添加一些codeS在 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();
                }               
        }
    }
};

现在你有 mNewPositions 新定位数据。刚刚接触它,使用时要使用它。

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

希望它会清除。

这篇关于保存的ListView的内容拖放后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆