是否可以使用 ArrayAdapter 和 ListView 创建片段?

Is it possible to use ArrayAdapter and ListView to create fragments?

我正在学习 Android 自己。我知道如何使用 Arrayadapter 使用 listView。现在我要去学习 Fragments。

是否可以使用 ArrayAdapter 和 listView 创建片段? 这里列表中的第一个片段应该是人名,点击它右侧的片段会出现详细的联系方式 person.On 点击详细的联系地址,它会打开电子邮件应用程序向该联系人发送电子邮件。

我有这个问题,因为当我尝试在片段的 xml 上添加 ListView 时,它显示错误,指出使用了无效属性 联系 Activity(这是 activity 寄存片段)

public class ContactActivity extends Activity {
    ListView listView;
    //TextView textView;
    ArrayAdapter<Contact> cAdapter;
    private Contact[] myContact = {
            new Contact("Rabin", "Awal", "+405-315-2027", "rawal@yahoo.com"),
            new Contact("David", "Gilmour", "+455-315-2827", "david@gilmour.com"),
            new Contact("James", "Hetfield", "+455-315-0026", "james@metallica.com"),
            new Contact("Kirk", "Hammet", "+445-315-2227", "kirk@metallica.com"),
            new Contact("Tom", "Morello", "+415-315-2497", "tom@tommorello.com"),
            new Contact("Ron", "Thal", "+405-315-2007", "ron@ronthal.com")

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        //listView = (ListView) findViewById(R.id.contact_list_fragment_container);
        //textView = (TextView) findViewById(R.id.contact_frag_list)
        cAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.contact_list, myContact);
        listView.setAdapter(cAdapter);



    }

纯粹JavaClass

public class Contact {

    private String fname, lname, phone, email;

    public Contact(String fname, String lname, String phone, String email){
        this.fname = fname;
        this.lname = lname;
        this.email = email;
        this.phone = phone;
    }

    public String getFname() {
        return fname;
    }

    public String getLname() {
        return lname;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

联系activity的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_contact"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="edu.uco.rawal.p5rabina.ContactActivity">

    <FrameLayout
        android:id="@+id/contact_list_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <FrameLayout
        android:id="@+id/contact_detail_fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent" />



</RelativeLayout>

是的,在这种情况下可以使用 ListFragment。方法如下

public class ContactListFragment extends ListFragment {

    ArrayAdapter<Contact> cAdapter;
    //ArrayAdapter<String> cAdapter;


    interface ContactListListener{
        void itemClicked(long id);
    }
    //add listener to fragment
    private ContactListListener listener;

    public ContactListFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

    cAdapter = new ArrayAdapter<>(inflater.getContext(), simple_list_item_1, myContact);  
.....
setListAdapter(cAdapter);
        cAdapter.notifyDataSetChanged();

        return super.onCreateView(inflater, container, savedInstanceState);
    }

}

它们在 activity 我们需要实现它

public class ContactActivity extends Activity implements ContactListFragment.ContactListListener{

     @Override
    public void itemClicked(long id){
        //method also defined in the listener

        View fragmentContainer = findViewById(R.id.fragment_detail_container);
   //here instanciate the fragment and commit


    }

}