2012-04-28 52 views
1

我有一個活動(A),它有一個Button,當用戶按下它時,它將添加一個Objectstatic List。我有另一個活動(B),它有一個ListView來顯示靜態列表的內容。在活動(B)中,我想使用存儲在特定行的靜態列表中的對象的屬性。我怎樣才能做到這一點?我的代碼如下所示:使用BaseAdapter從Listview獲取數據

//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; 
    } 


} 
+0

,我不知道如果我明白你的問題,但如果你想傳遞的對象這裏的活動A到活動B是從[如何在Android中將對象從一個活動傳遞到另一個活動]的一種方法(http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-到另一個在Android) – Sam 2012-04-28 18:35:59

回答

0

序列化你的對象,並把它傳遞給活動B同時稱這是通過意向

+1

但我不需要傳輸整個對象,我只需要從arrayList中的特定行獲取數據 謝謝 – Monerah 2012-04-29 17:49:02

+0

你正在使用什麼樣的arrayList?就像是String,int或者你自己的一些類對象? – sachy 2012-04-30 08:25:27

+0

ArrayList的對象,我想檢索對象的名稱,並通過使用tv.setText(object.getName())在列表的行中顯示在側面的TextView中;當我這樣做,它檢索空值! :\ – Monerah 2012-04-30 13:32:30

相關問題