如何将多个数据从片段传递到Activity

时间:2015-09-27 15:03:22

标签: android android-fragments android-intent

我正在尝试将listView onItemClickItem创建为详细视图。该列表通过使用自定义适配器

来实现

这是我的片段列表

    import android.app.Fragment;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ListView;
    import android.widget.Toast;

    import com.rupomkhondaker.sonalibank.adapter.PhoneListAdapter;
    import com.rupomkhondaker.sonalibank.model.ContactItem;
    import java.io.Serializable;
    import java.util.ArrayList;

    public class GMOFragment extends Fragment {
        public GMOFragment(){}


        private ArrayList<ContactItem> phoneItems;
        private PhoneListAdapter adapters;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

     View rootView = inflater.inflate(R.layout.fragment_gmo, container, false);



            final ArrayList<ContactItem> listData = getListData();

            final ListView listView = (ListView) rootView.findViewById(R.id.gmolistView);
            listView.setAdapter(new PhoneListAdapter(getActivity(), listData));

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                    ContactItem newsData = (ContactItem) listView.getItemAtPosition(position);


   String names = (String) listView.getItemAtPosition(position);
   String phones = (String) listView.getItemAtPosition(position);
   String mobiles = (String) listView.getItemAtPosition(position);
   String emails = (String) listView.getItemAtPosition(position);

   Intent intent = new Intent(getActivity(), ContactDetail.class);

intent.putExtra("key", names);
intent.putExtra("key2", phones);
intent.putExtra("key3", mobiles);
intent.putExtra("key4", emails); 

Toast.makeText(getActivity(), "Selected :" + " " + newsData, Toast.LENGTH_LONG).show();

  startActivity(intent);
 }

});

            return rootView;
        }

      private ArrayList<ContactItem> getListData() {
            ArrayList<ContactItem> listMockData = new ArrayList<ContactItem>();
            String[] names = getResources().getStringArray(R.array.gmo_name_list);
            String[] phones = getResources().getStringArray(R.array.gmo_ph_list);
            String[] mobiles = getResources().getStringArray(R.array.gmo_mob_list);
            String[] emails = getResources().getStringArray(R.array.gmo_email_list);

            for (int i = 0; i < names.length; i++) {
                ContactItem newsData = new ContactItem();
                newsData.setName(names[i]);
                newsData.setPhone(phones[i]);
                newsData.setMobile(mobiles[i]);
                newsData.setEmail(emails[i]);
                listMockData.add(newsData);
            }
            return listMockData;
        }

    }

如果我只尝试Toast,它会显示Toast,其数据与Image相同 但是当我试图将这些数据显示/传递给新活动时,它会显示错误 enter image description here

ContactDetail.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;

public class ContactDetail extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.view_contact);

        TextView txtName = (TextView) findViewById(R.id.conName);
        TextView txtPhone = (TextView) findViewById(R.id.conPhone);
        TextView txtMobile = (TextView) findViewById(R.id.conMobile);
        TextView txtEmail = (TextView) findViewById(R.id.conEmail);


        Intent i = getIntent();
        // getting attached intent data

        // getting attached intent data
        String name =i.getStringExtra("key");
        String phone = i.getStringExtra("key2");
        String mobile = i.getStringExtra("key3");
        String email = i.getStringExtra("key4");

        // displaying selected contact name
        txtName.setText(name);
        txtPhone.setText(phone);
        txtMobile.setText(mobile);
        txtEmail.setText(email);

    }

}

这是日志

09-27 19:05:21.762      629-629/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ClassCastException: com.rupomkhondaker.sonalibank.model.ContactItem cannot be cast to java.lang.String
            at com.rupomkhondaker.sonalibank.GMOFragment$1.onItemClick(GMOFragment.java:49)
            at android.widget.AdapterView.performItemClick(AdapterView.java:298)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
            at android.widget.AbsListView$1.run(AbsListView.java:3533)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
09-27 19:05:21.782      150-517/? W/ActivityManager﹕ Force finishing activity com.rupomkhondaker.sonalibank/.MainActivity
09-27 19:05:21.892      150-517/? D/dalvikvm﹕ GC_FOR_ALLOC freed 93K, 10% free 11568K/12807K, paused 58ms, total 63ms

1 个答案:

答案 0 :(得分:2)

问题出现在这里:

String names = (String) listView.getItemAtPosition(position);

方法getItemAtPosition()返回Object,即存储在适配器中位置的对象。在您的情况下,此对象的动态类型为ContactItem。要检索名称,您可以使用类似的内容(假设您已为名称字段实现了getter):

String names = (String) listView.getItemAtPosition(position).getName();

然而,将整个ContactItem置于意图中会更好。这要求ContactItemserializable。然后你的代码将成为:

@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    ContactItem newsData = (ContactItem) listView.getItemAtPosition(position);

    Intent intent = new Intent(getActivity(), ContactDetail.class);

    intent.putExtra("key", newsData);

    Toast.makeText(getActivity(), "Selected :" + " " + newsData, Toast.LENGTH_LONG).show();

    startActivity(intent);
}

ContactDetail课程中,您可以检索ContactItem的所有字段:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.view_contact);

    TextView txtName = (TextView) findViewById(R.id.conName);
    TextView txtPhone = (TextView) findViewById(R.id.conPhone);
    TextView txtMobile = (TextView) findViewById(R.id.conMobile);
    TextView txtEmail = (TextView) findViewById(R.id.conEmail);

    Intent i = getIntent();
    Bundle b = i.getExtras();

    // getting attached intent data
    ContactItem contact = (ContactItem) b.getSerializable("key");

    // displaying selected contact name
    txtName.setText(contact.getName());
    txtPhone.setText(contact.getPhone());
    txtMobile.setText(contact.getMobile());
    txtEmail.setText(contact.getEmail());

}