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

时间:2016-10-01 21:17:08

标签: java android listview android-fragments android-listfragment

我自己在学习Android。我有使用Arrayadapter使用listView的知识。现在我要去学习片段。

是否可以使用ArrayAdapter和listView创建片段? 此处列表中的第一个片段应该是人员姓名点击,右侧的片段显示该人员的详细联系人详细信息。点击该详细联系地址后,它会打开电子邮件应用程序以向该联系人发送电子邮件。

我有这个问题,因为当我试图在xml上为片段添加ListView时,它显示的错误表明使用了无效的属性 联系活动(这是托管片段的活动)

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);



    }

纯Java类

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();
    }
}

联系活动的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>

1 个答案:

答案 0 :(得分:0)

是的,在这种情况下,可以使用ListFragment。这是怎么做的

&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

他们在我们需要实施的活动中

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

}