2012-05-10 145 views
0

我正在處理一個應用程序,其中我想從ListView的數據發送到另一個Activity點擊ImageView。代碼如下所示:如何將ListView數據從一個活動轉移到另一個活動

package com.gabriel.utility; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 

import android.content.Context; 
import android.content.Intent; 
import android.graphics.Typeface; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

import com.gabriel.activity.ContactInfoActivity; 
import com.gabriel.activity.ContactListActivity; 
import com.gabriel.activity.GabrielApplication; 
import com.gabriel.activity.R; 
import com.gabriel.database.ContactCore; 
import com.gabriel.database.WebMessageCore; 

public class WebMessageAdapter extends ArrayAdapter<WebMessageCore>{ 

    Context _Context; 
    ArrayList<WebMessageCore> _WebMsgList; 
    WebMessageCore _WebMsgCore; 
    TextView _MsgContent; 
    private LayoutInflater mInflater; 
    ImageButton Arrow; 
    GabrielApplication application; 
    public WebMessageAdapter(Context context, int resource, 
      ArrayList<WebMessageCore> contactList) { 

     super(context, resource, contactList); 
     _Context = context; 
     _WebMsgList = contactList; 
     mInflater = LayoutInflater.from(context); 
    } 

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

    public WebMessageCore getItem(int position) { 
     return _WebMsgList.get(position); 
    } 

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


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.custom_row_view, null); 
      holder = new ViewHolder(); 
      holder.txtName = (TextView) convertView.findViewById(R.id.name); 
      holder.txtmessage = (TextView) convertView 
        .findViewById(R.id.meassage); 
      holder.txtPhone = (TextView) convertView.findViewById(R.id.phone); 
      holder.txtdate= (TextView) convertView.findViewById(R.id.datetime); 
      holder.Arrow = (ImageView)convertView.findViewById(R.id.settingArrow); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     _WebMsgCore=_WebMsgList.get(position); 
     holder.txtName.setText(_WebMsgCore.getName()); 
     holder.txtmessage.setText(_WebMsgCore.getMessage()); 
     holder.txtPhone.setText(_WebMsgCore.getMobileNo()); 
     holder.Arrow.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
     Intent newActivity = new Intent(_Context, ContactInfoActivity.class); 

     _Context.startActivity(newActivity); 

     }  

     }); 

     String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date()); 
     holder.txtdate.setText(currentDateTimeString); 
     return convertView; 
     /* SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS a"); 
     Date resultdate = new Date((new Date()).getTime()); 
     String currentDateTimeString = sdf.format(resultdate); 
     holder.txtdate.setText(currentDateTimeString);*/ 


    } 



     /*tdate = (TextView)findViewById(R.id.datetime); 
     Arrow = (ImageButton)findViewById(R.id.settingArrow); 
     String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date()); 
     tdate.setText(currentDateTimeString); 
     Arrow.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       Intent newActivity = new Intent(context, ContactInfoActivity.class); 
       startActivity(newActivity); 
      } 
     }); 
      */ 


     //View contactView = convertView; 
     /*if (contactView == null) { 
      contactView = LayoutInflater.from(_Context).inflate(
        R.layout.web_message_item, null); 
     } 
     _MsgContent = (TextView) contactView.findViewById(R.id.messageContent); 

     //To set text as bold 
     //===================== 
     _MsgContent.setTypeface(null, Typeface.BOLD); 

     _WebMsgCore = _WebMsgList.get(position); 
     _MsgContent.setText(_WebMsgCore.getMessage()); 
     return contactView; 
    } 
    */ 

    static class ViewHolder { 
     ImageView Arrow; 
     TextView txtName; 
     TextView txtmessage; 
     TextView txtPhone; 
     TextView txtdate; 

    } 
}` 

然後我想要將數據傳遞給另一個活動。請給我一些想法。

+0

從你在哪裏調用此適配器列表視圖?在此處顯示該代碼 –

回答

0

List是可序列化的,所以你可以通過Intent將它們傳遞給另一個Activity。請參閱the doc

確保您的WebMessageCore也實現了Serializable。

0

試試這個(笑):

使用方法如下:

活動1:MySingletonClass.getInstance().setData(yourData);

活動2:MySingletonClass.getInstance().getData();

如果這樣做不工作:嘗試(超過100)上StackOverFlow.com搜索複製/替代解決方案

0

上只設置一個onclick監聽你的第一個活動(您的列表)

私人OnItemClickListener myListener的=新OnItemClickListener(){ @覆蓋公共無效onItemClick(適配器視圖父母,視圖v, INT I,長L){

 Log.v(TAG, "onItemClick fired!" + i); 

    ObjectClass mObjectClass = (ObjectClass) >parent.getItemAtPosition(i);   

     //get what where ever you need from mObjectClass then pass it with putExtra >      //if it's a complex object use Parcelable 

    } 

不要在你的onCreate

yourList.setOnItemClickListener(myListener); 

忘記這在次活動得到了什麼你已經通過,並用它

相關問題