0

我正在开发一个应用程序,我希望通过单击将数据从一个ListView发送到另一个应用程序。代码如下所示:ActivityImageView

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;

    }
}`

然后我想将数据获取到另一个活动。请给我一些想法。

4

4 回答 4

0

在您的第一个活动中,只需设置一个 onclick 侦听器(在您的列表中)

私有 OnItemClickListener myListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View v, int i, long 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);

在第二个活动中,获取您通过的内容并使用它

于 2012-05-10T07:13:38.813 回答
0

使用适配器 getView() 函数代码中的列表视图的setTag()getTag()属性来存储和检索数据。

还要检查这个答案:在 android listview 中将元数据从一个活动传递到下一个活动

于 2012-05-10T07:08:51.137 回答
0

List 是可序列化的,因此您可以通过 Intent 将它们传递给另一个 Activity。查看文档

确保您的 WebMessageCore 也实现了 Serializable。

于 2012-05-10T07:07:06.320 回答
0

试试这个(笑):

class MySingletonClass {

    private static MySingletonClass instance;
    public static MySingletonClass getInstance() {
    if(instance == null) instance = new MySingletonClass();
        return instance;
    }
    private MySingletonClass() {
    }
    Data data; //this is your data

    public void setData(Data data) {
        this.data = data;
    }
    public Data getData() {
        return data;
    }

}

像这样使用它:

活动一: MySingletonClass.getInstance().setData(yourData);

活动二: MySingletonClass.getInstance().getData();

如果这不起作用:尝试在 StackOverFlow.com 上搜索(超过 100 个)重复/替代解决方案

于 2012-05-10T07:07:18.437 回答