通过按钮添加Listview

时间:2015-12-07 16:15:11

标签: android listview android-fragments

我正在尝试添加一个按钮,单击该按钮会将新组和一些联系人添加到组中。这将在新片段中打开,将新组添加到列表视图中。到目前为止,我有联系人列表和工作,我现在正试图让Button工作。它现在点击时就崩溃了。我试图使用片段

这是我的MainActivity

public class MainActivity extends AppCompatActivity implements CreateGroup.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("STARTED", "App started");

    }


    public void OnItemClicked(String text) {

        GroupList gl = (GroupList)getFragmentManager().findFragmentById(R.id.groupList);

        //TaskList tl = (TaskList)getFragmentManager().findFragmentById(R.id.taskList);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

CreateGroup

public class CreateGroup extends Fragment {

public interface OnItemClickListener{
    void OnItemClicked(String text);
}

public OnItemClickListener listener;
EditText newGroup;
Button createGroup;
Button showContacts;
ListView contactList;
Cursor cursor1;

public CreateGroup() {
    // Required empty public constructor
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState){
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_create_group, container, false);

    newGroup = (EditText) v.findViewById(R.id.groupEditText);
    createGroup = (Button) v.findViewById(R.id.createButton);
    contactList = (ListView) v.findViewById(R.id.contactListView);

    createGroup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createdNewGroup(v);
        }
    });

    showContacts = (Button)v.findViewById(R.id.contactButton);
    showContacts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoadContactsWithPhone(v);
        }
    });

    return v;

}

public Cursor getContactsWithPhone()
{
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };

    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER  + " = ?";

    String[] selectionArgs = new String[]{ String.valueOf(1)};

    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return getActivity().getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
}

public void LoadContactsWithPhone(View v) {

    Cursor cursor = getContactsWithPhone();
    ShowContacts s = new ShowContacts();


    ContactArray[] person =  s.showNewContacts(cursor);
    ContactListAdapter contactInfo = new ContactListAdapter(getActivity(), R.layout.contact_view_items, person);
    contactList.setAdapter(contactInfo);

}

public void createdNewGroup(View v) {

    String ne = newGroup.getText().toString();


}

}

和我的GroupList

public interface OnItemClickListener{
        public void OnItemClicked(String text);
    }

    public OnItemClickListener listener;
    ListView groupList;
    Button groupCreatePage;
    EditText newgroup;

    private ArrayList<String> arrayList;
    private ArrayAdapter<String> adapter;

    public GroupList() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v =  inflater.inflate(R.layout.fragment_group_list, container, false);

        groupList = (ListView)v.findViewById(R.id.groupListView);
        newgroup = (EditText)v.findViewById(R.id.newEditText);

        String [] groups = {""};
        arrayList = new ArrayList<>(Arrays.asList(groups));
        adapter = new ArrayAdapter<String>(getActivity(),R.layout.task_view,arrayList);
        groupList.setAdapter(adapter);


        groupCreatePage = (Button)v.findViewById(R.id.groupButton);

        groupCreatePage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                display(v);
            }
        });


        return v;
    }

    public void display(View v){

        String group = newgroup.getText().toString();
        arrayList.add(group);
        adapter.notifyDataSetChanged();

    }

退出

 Process: amanda.groupsandlists, PID: 697
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at android.widget.AbsListView.obtainView(AbsListView.java:2823)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1290)
            at android.widget.ListView.onMeasure(ListView.java:1202)
            at android.view.View.measure(View.java:18669)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
            at android.view.View.measure(View.java:18669)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3012)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2298)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1345)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1588)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6730)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6117)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at android.widget.AbsListView.obtainView(AbsListView.java:2823)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1290)
            at android.widget.ListView.onMeasure(ListView.java:1202)
            at android.view.View.measure(View.java:18669)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
            at android.view.View.measure(View.java:18669)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3012)
            at android.view.View.measure(View.java:18669)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2298)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1345)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1588)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6730)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6117)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

0 个答案:

没有答案