1

我有一个 Activity(A) ,Button当用户按下它时,它会添加一个Objectstatic List. 我有另一个ListView显示静态列表内容的活动(B)。在活动(B)中,我想使用存储在特定行的静态列表中的对象的属性。我怎样才能做到这一点?我的代码如下所示:

//Activity (A):
Send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
...
//  Add to mycaseList
All_Static.MyCaseList.add(case1);
...
}});

//==============================================================================

//Activity (B):

public class MyPage extends Activity {

     boolean returnvar ;
    private Activity mContext;
    Button createForm;
    Button ConfExpandRegion, Cancelb;
    String ExpandMsg, CancelMsg;
    boolean b;
    MyCaseClass mycase;
    TextView tvCase;
    AlertDialog alertDialog;

    //for list
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage);


        // Moving to another activity
        createForm = (Button) findViewById(R.id.creat_new_formbtn);
        createForm.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent j = new Intent(MyPage.this, CreateNewForm.class);
                startActivity(j);

            }
        });

        // ============================================================================================
        // for list


            list = (ListView) findViewById(R.id.mypage_list);
            list.setClickable(true);

            final List<MyCaseClass> listOfMyCases = new ArrayList<MyCaseClass>();

            MyCasesListAdapter adapter = new MyCasesListAdapter(this, listOfMyCases, MyPage.this);

            for (MyCaseClass m : All_Static.getMyCaseList())
                adapter.add(new MyCaseClass(m));

            // after fill the adapter.. assign the list to the adapter
            list.setAdapter(adapter);


            list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {


                }
            });

        // ========================================================================================

    }



    public void sendSMS(String number, String msg) throws Exception {
        if (!b) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, msg, null, null);
        }
        b = true;
    }

    // ========================================================================

    public void ShowingDialogExpand(){
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Conformation");
        alertDialog.setMessage("Are you sure you want to Expand Report Region?");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {

                   ExpandMsg = "Case_ID expand";
                   if (!b) {
                        try {
                            // Should write server number here + the chatting must be pushed above 
                            sendSMS("0000", ExpandMsg);
                            Toast.makeText(MyPage.this, "Request Sent", Toast.LENGTH_LONG)
                                    .show();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Toast.makeText(MyPage.this, e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                   //ConfExpandRegion.setEnabled(false);
               }

            });

            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
                // Do nothing 




               }
            });

            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog.show();
    }

    public void  ShowingDialogCancel(int i){
        final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
        alertDialog2.setTitle("Conformation?");
        alertDialog2.setMessage("Are you sure you want to cancel x cases?");

        String str = "ID:" + i;
        Toast.makeText(MyPage.this, str , 0)
        .show();
        alertDialog2.setButton("Yes", new DialogInterface.OnClickListener() {
               public  void onClick(DialogInterface dialog, int which) {

                     CancelMsg = "Case_ID cancel";
                     returnvar=true;

                   if (!b) {
                        try {
                            // Should write server number here + the chatting must be pushed above 
                            sendSMS("0000", CancelMsg);
                            Toast.makeText(MyPage.this, "Cancelation request sent", Toast.LENGTH_LONG)
                                    .show();

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Toast.makeText(MyPage.this, e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                   }
            });

            alertDialog2.setButton2("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
                // Do nothing 
                   returnvar = false;
                   Toast.makeText(MyPage.this, "inside No", 0)
                    .show();


               }
            });

            alertDialog2.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog2.show();

    }

}


//=================================================================================

// My BaseAdapter 
public class MyCasesListAdapter extends BaseAdapter {
    private MyPage myPage;
    private List<MyCaseClass> listOfCases;
    private Activity parentActivity;

    // TODO test
    MyCaseClass entry;

    // TODO delete it not imp.
    public MyCasesListAdapter() {

        super();

    }

    public MyCasesListAdapter(MyPage mypage, List<MyCaseClass> listOfCaseParameter, Activity parentActivity) {
        this.myPage = mypage;
        this.listOfCases = listOfCaseParameter;
        this.parentActivity = parentActivity;
    }

    public int getCount() {
        return listOfCases.size();
    }

    public Object getItem(int position) {
        return listOfCases.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {

         entry = listOfCases.get(position);
         //this.getitem(position)
        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) myPage
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.mypage_row, null);
          }


        // this is row items..
        // Set the onClick Listener on this button
        Button ConfExpandRegion = (Button) convertView.findViewById(R.id.expand);
        Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase);
        TextView tvCase = (TextView) convertView.findViewById(R.id.mypage_name);

        // To be a clickable button
        ConfExpandRegion.setFocusableInTouchMode(false);
        ConfExpandRegion.setFocusable(false);


        ConfExpandRegion.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                myPage.ShowingDialogExpand();
            }
        });

        // To be a clickable button
        Cancelb.setFocusableInTouchMode(false);
        Cancelb.setFocusable(false);
        Cancelb.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                entry = (MyCaseClass) v.getTag();
                int caseid= entry.getID();
                myPage.ShowingDialogCancel(caseid);

                Toast.makeText(myPage, "inside calling", 0).show();

                //MyCaseClass entry = (MyCaseClass) v.getTag();
                //listOfCases.remove(entry);

                // listPhonebook.remove(view.getId());
                notifyDataSetChanged();
            }
        });

        // Set the entry, so that you can capture which item was clicked and
        // then remove it
        // As an alternative, you can use the id/position of the item to capture
        // the item
        // that was clicked.
        ConfExpandRegion.setTag(entry);
        Cancelb.setTag(entry);

        // btnRemove.setId(position);

        return convertView;
    }

    public void onClick(View view) {
        MyCaseClass entry = (MyCaseClass) view.getTag();
        listOfCases.remove(entry);
        // listPhonebook.remove(view.getId());
        notifyDataSetChanged();

    }

    private void showDialog(MyCaseClass entry) {
        // Create and show your dialog
        // Depending on the Dialogs button clicks delete it or do nothing
    }

    public void add(MyCaseClass myCaseClass) {
        // TODO Auto-generated method stub
        listOfCases.add(myCaseClass);
    }



}
//===========================================================================
// StaticClass
public class All_Static {

    public static List<MyCaseClass> MyCaseList = new ArrayList<MyCaseClass>();

    public static List<MyCaseClass> getMyCaseList() {
        return MyCaseList;
    }

    public static void setMyCaseList(List<MyCaseClass> myCaseList) {
        MyCaseList = myCaseList;
    }

    //===========================================================================
    public static List<HelpersClass> HelperList = new ArrayList<HelpersClass>();

    public static List<HelpersClass> getHelperList() {
        return HelperList;
    }

    public static void setHelperList(List<HelpersClass> helperList) {
        HelperList = helperList;
    }


}
4

1 回答 1

0

序列化您的对象并将其传递给活动 B,同时通过意图调用它

于 2012-04-29T17:30:28.773 回答