AutoCompleteTextView不会显示片段

时间:2016-02-09 12:06:29

标签: android android-fragments autocompletetextview

我正在尝试从片段中显示AutoCompleteTextView。我有三个片段,所以我使用ViewPager来显示MainActivity上的Fragement。我可以输入AutocompleteTextview,但建议没有显示在mainActivity中。 我尝试使用简单的字符串数组,但它没有显示出来。任何人都遇到过这个问题。请帮帮我。 提前谢谢。

这是片段中的代码:

ArrayAdapter<String> adapter= new ArrayAdapter<String> getActivity().getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, somestring);
final AutoCompleteTextView textView = (AutoCompleteTextView) v.findViewById(R.id.listview4);    
textView.setThreshold(1);
textView.setAdapter(adapter);

这是我的mainActivity标注

tabLayout = (TabLayout)findViewById(R.id.tabLayout);
        viewPAger = (ViewPager)findViewById(R.id.viewPager);
        viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());

        viewPagerAdapter.addFragments(new Home() ,"HOME");

        viewPAger.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPAger);

这是我的viewpage适配器

package gt.transit;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;


    public class ViewPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> TabTitles = new ArrayList<>();


        public void addFragments (Fragment fragments, String titles){
            this.fragments.add(fragments);
            this.TabTitles.add(titles);
        }
        public ViewPagerAdapter(FragmentManager fm){
            super(fm);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TabTitles.get(position);
        }




    }

这是我在主要活动中的XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="gt.transit.MainActivity"
    >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/appbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_alignParentTop="true"
            layout="@layout/toolbar_layout" />
        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabLayout"
            app:tabMode="fixed"
            app:tabGravity="fill"
            style="@style/MyCustomTabLayout" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/viewPager"
    android:layout_below="@+id/appbar"
    android:background="#e99d9d"
    >
</android.support.v4.view.ViewPager>

</RelativeLayout>

大家好, 谢谢你的帮助。我能够解决这个问题。 这是我的不好,我没有正确回归。

错误的代码:

 return inflater.inflate(R.layout.fragment_bookmark, container, false

更正后的代码:

return v;

1 个答案:

答案 0 :(得分:0)

这是我的AutoCompleteTextView对话框片段的工作代码

public class MyAutoCompleteFragment extends DialogFragment implements OnEditorActionListener{
private EditDialogListener mCallback;

public static MyAutoCompleteFragment newInstance(Bundle fB){
    MyAutoCompleteFragment eDialog = new MyAutoCompleteFragment();
    eDialog.setArguments(fB);

    return eDialog;
}

public interface EditDialogListener {
    void onFinishEditDialog(String inputText, int flag);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (EditDialogListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement onFinishEditDialog");
    }
    this.setCancelable(false);

}

private AutoCompleteTextView mAutoComp;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String[] lista = getArguments().getStringArray("listagem");

    View view = inflater.inflate(R.layout.autodialog, container);

    mAutoComp = (AutoCompleteTextView)view.findViewById(R.id.autodlg);
    mAutoComp.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_dropdown_item_1line, lista));

    //fill the text view with something already typed
    if(getArguments().getString("digitado").length()>0) mAutoComp.setText(getArguments().getString("digitado"));

    getDialog().setTitle(getArguments().getString("titulo"));

    getDialog().setCancelable(false);

    mAutoComp.requestFocus();
    getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    mAutoComp.setOnEditorActionListener(this);

    view.setMinimumWidth(500);
    return view;
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
     if (EditorInfo.IME_ACTION_DONE == actionId) {
         mCallback.onFinishEditDialog(mAutoComp.getText().toString(), 0);
         mAutoComp.setText("");
         getDialog().dismiss();
         MyAutoCompleteFragment.this.dismiss();
     }
    return false;
}

R.layout.autodialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

   <AutoCompleteTextView
    android:id="@+id/autodlg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:completionThreshold="2"
    android:inputType="textCapCharacters"
    android:maxLines="1"
    />

</LinearLayout>

如何从活动中调用它:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

Bundle args= new Bundle(); args.putStringArray("listagem", list); args.putString("digitado", typed); args.putString("titulo", title);

MyAutoCompleteFragment frag = MyAutoCompleteFragment.newInstance(args);
frag.show(ft, "autodialog");

不要忘记在活动中实现回调:

@Override
public void onFinishEditDialog(String inputText, int flag) {
}

您可以在视图寻呼机中尝试:

@Override
    public Fragment getItem(int position) {
        Bundle args= new Bundle();

        switch (position) {
            case 0:
                args.putStringArray("listagem", listx); args.putString("digitado", typedx); args.putString("titulo", titlex);
                break;
            case 1:
                args.putStringArray("listagem", listy); args.putString("digitado", typedy); args.putString("titulo", titley);
                break;
        }

        return MyAutoCompleteFragment.newInstance(args);
    }

并更改MyAutoCompleteFragment以扩展Fragment。