2016-07-05 11 views
-1

Ich möchte TextInputLayouts basierend auf dem Element auffüllen, das ein Benutzer aus einem Spinner auswählt. Unten ist mein XML (die ID mit "CreatePollAnswer" ist die Ansicht, die ich dynamisch auffüllen möchten):Dynamische Hinzufügen von TextInputLayout-Ansichten

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/create_poll_linearlayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.troychuinard.fanpolls.Fragment.CreateFragment"> 

<FrameLayout 
    android:background="@drawable/image_border" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight=".525"> 

    <Button 
     android:id="@+id/add_image_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Click to Add Image" /> 
</FrameLayout> 

<LinearLayout 
    android:orientation="vertical" 
    android:padding="4dp" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight=".475"> 

    <android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
     <EditText 
      android:id="@+id/create_poll_question_editText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:imeOptions="actionDone" 
      android:singleLine="true" 
      android:hint="@string/create_poll_question" /> 
    </android.support.design.widget.TextInputLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/how_many_answers_textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/how_many_answers_text" 
      android:textColor="@color/black" 
      android:textSize="16sp" /> 

     <Spinner 
      android:id="@+id/number_of_answers_spinner" 
      android:layout_width="wrap_content" 
      android:layout_gravity="bottom" 
      android:layout_height="24dp" 
      android:background="@android:drawable/btn_dropdown" /> 

    </LinearLayout> 


    <android.support.design.widget.TextInputLayout 
     android:id="@+id/create_poll_answer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
     <EditText 
      android:id="@+id/create_poll_answer_editText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:imeOptions="actionDone" 
      android:singleLine="true" 
      /> 
    </android.support.design.widget.TextInputLayout> 

</LinearLayout> 

Code:

public class CreateFragment extends Fragment { 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private static final String ARG_PARAM1 = "param1"; 
private static final String ARG_PARAM2 = "param2"; 

private String mParam1; 
private String mParam2; 

private Button mAddImageButton; 

private Spinner mSelectNumberofPollAnswers; 
private String mSpinnerPosition; 

private EditText mCreatePollQuestion; 
private OnFragmentInteractionListener mListener; 

private View mRootView; 

private TextInputLayout mCreatePollAnswer; 
private EditText mCreatePollAnswerEditText; 

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

/** 
* Use this factory method to create a new instance of 
* this fragment using the provided parameters. 
* 
* @param param1 Parameter 1. 
* @param param2 Parameter 2. 
* @return A new instance of fragment CreateFragment. 
*/ 
// TODO: Rename and change types and number of parameters 
public static CreateFragment newInstance(String param1, String param2) { 
    CreateFragment fragment = new CreateFragment(); 
    Bundle args = new Bundle(); 
    args.putString(ARG_PARAM1, param1); 
    args.putString(ARG_PARAM2, param2); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     mParam1 = getArguments().getString(ARG_PARAM1); 
     mParam2 = getArguments().getString(ARG_PARAM2); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    mRootView = inflater.inflate(R.layout.fragment_create, container, false); 
    mAddImageButton = (Button) mRootView.findViewById(R.id.add_image_button); 
    mSelectNumberofPollAnswers = (Spinner) mRootView.findViewById(R.id.number_of_answers_spinner); 
    mCreatePollQuestion = (EditText) mRootView.findViewById(R.id.create_poll_question_editText); 
    mCreatePollAnswer = (TextInputLayout) mRootView.findViewById(R.id.create_poll_answer); 
    mCreatePollAnswerEditText = (EditText) mRootView.findViewById(R.id.create_poll_answer_editText); 
    // Inflate the layout for this fragment 

    // Create an ArrayAdapter using the string array and a default spinner layout 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), 
      R.array.number_of_poll_answers, android.R.layout.simple_spinner_item); 
    // Specify the layout to use when the list of choices appears 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    // Apply the adapter to the spinner 
    mSelectNumberofPollAnswers.setAdapter(adapter); 
    mSelectNumberofPollAnswers.setOnItemSelectedListener(new YourItemSelectedListener()); 



    return mRootView; 
} 

// TODO: Rename method, update argument and hook method into UI event 
public void onButtonPressed(Uri uri) { 
    if (mListener != null) { 
     mListener.onFragmentInteraction(uri); 
    } 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    if (context instanceof OnFragmentInteractionListener) { 
     mListener = (OnFragmentInteractionListener) context; 
    } else { 
     throw new RuntimeException(context.toString() 
       + " must implement OnFragmentInteractionListener"); 
    } 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    mListener = null; 
} 

/** 
* This interface must be implemented by activities that contain this 
* fragment to allow an interaction in this fragment to be communicated 
* to the activity and potentially other fragments contained in that 
* activity. 
* <p/> 
* See the Android Training lesson <a href= 
* "http://developer.android.com/training/basics/fragments/communicating.html" 
* >Communicating with Other Fragments</a> for more information. 
*/ 
public interface OnFragmentInteractionListener { 
    // TODO: Update argument type and name 
    void onFragmentInteraction(Uri uri); 
} 

public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener { 

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
     String selected = parent.getItemAtPosition(pos).toString(); 
     Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show(); 
     for (int i = 0; i < Integer.parseInt(selected); i++) { 
      ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout); 
      TextInputLayout newAnswer = new TextInputLayout(getActivity().getApplicationContext()); 
      newAnswer = (TextInputLayout) mRootView.findViewById(R.id.create_poll_answer); 
      newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
      layout.addView(newAnswer); 

     } 

    } 

    public void onNothingSelected(AdapterView parent) { 
     // Do nothing. 
    } 
    } 
} 

Hier ist der aktuelle Fehler Ich erhalte:

java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library. 
                       at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36) 
                       at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:139) 
                       at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:132) 
                       at android.support.design.widget.TextInputLayout.<init>(TextInputLayout.java:128) 
                       at com.troychuinard.fanpolls.Fragment.CreateFragment$YourItemSelectedListener.onItemSelected(CreateFragment.java:153) 
                       at android.widget.AdapterView.fireOnSelected(AdapterView.java:924) 
                       at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913) 
                       at android.widget.AdapterView.access$300(AdapterView.java:51) 
                       at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5527) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

Styles:

Manifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.troychuinard.fanpolls"> 

<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:name=".FanPollsApplication" 
    android:allowBackup="true" 
    android:icon="@drawable/fan_polls_logo" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme.NoActionBar"> <!-- ADD THIS LINE --> 
    > 
    <activity 
     android:name=".SignupActivity" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:windowSoftInputMode="stateHidden|adjustResize" /> 
    <activity 
     android:name="com.facebook.FacebookActivity" 
     android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
     android:label="@string/app_name" 
     android:theme="@style/Theme.AppCompat" /> 

    <meta-data 
     android:name="com.facebook.sdk.ApplicationId" 
     android:value="@string/facebook_app_id" /> 
    <meta-data 
     android:name="com.firebase.ui.GoogleClientId" 
     android:value="@string/google_client_id" /> 

    <activity android:name="com.firebase.ui.auth.twitter.TwitterPromptActivity" /> 

    <meta-data 
     android:name="com.firebase.ui.TwitterKey" 
     android:value="@string/twitter_app_key" /> 
    <meta-data 
     android:name="com.firebase.ui.TwitterSecret" 
     android:value="@string/twitter_app_secret" /> 

    <activity 
     android:name=".PollActivity" 
     android:label="@string/title_activity_poll" 
     android:launchMode="singleTask" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".LoadingActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".Discussion_Activity" 
     android:label="@string/title_activity_discussion_" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity android:name=".HomeActivity" /> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <activity android:name=".CreateActivity" 
     android:theme="@style/Theme.AppCompat.Light" 
     > 
    </activity> 
</application> 

+0

Entfernen Sie die 'getApplicationContext()' rufen in der Konstruktoraufruf für das 'TextInputLayout'. Verwenden Sie einfach "getActivity()". –

+0

können Sie Ihre 'styles.xml' Dateien posten? scheint mir, es gibt etwas in ** AppTheme ** wie es sagt 'Sie müssen ein Theme.AppCompat Thema (oder Nachkommen)' in LogCat –

+0

Ich habe den Code aktualisiert. Ich bevorzuge das Thema NoActionBar, das von einem appCompat-Thema erbt. Ich habe das Thema in meiner CreateActivity aktualisiert und erhalte immer noch den Fehler. – tccpg288

Antwort

0

In AndroidManifest.xml Datei fügen Sie den folgenden Code innerhalb der Anwendung Tag

android:theme="@style/Theme.AppCompat.Light" 
+0

Was ist, wenn ich das Thema nicht ändern möchte? Ich habe bereits ein benutzerdefiniertes Design eingerichtet, das ich verwenden möchte. – tccpg288

+0

Ich habe den Code aktualisiert. Ich bevorzuge das Thema NoActionBar, das von einem appCompat-Thema erbt. Ich habe das Thema in meiner CreateActivity aktualisiert und erhalte immer noch den Fehler. – tccpg288

Verwandte Themen