更改片段中文本的适配器不起作用

Adapter to change text in fragment not working

我一整天都在尝试从片段中将文本设置为文本视图。我尝试在没有适配器的情况下进行,但现在我正在尝试使用适配器。此时它崩溃并在下一行 FragmentManager fm = ((NieuwToevoegen)context).getFragmentManager(); 上给我一个 NullPointerException,但我很确定这不会是唯一的问题。在我的应用程序中,我想根据您的输入放置不同的 frameLayouts。根据 frameLayouts 的数量,我得到了不同的文本视图字符串。这是我第一次使用片段。

如果有人知道更好的方法,请告诉我。

我的activity

public class NieuwToevoegen extends AppCompatActivity {
    Button buttonVerstuur;
    FragmentManager fm = getFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nieuw_toevoegen);
        buttonVerstuur = (Button) findViewById(R.id.buttonVerstuur);

        buttonVerstuur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView aantalTxt = (TextView) findViewById(R.id.txtAantal);
                aantalTxt.setEnabled(false);
                int aantal = Integer.parseInt(aantalTxt.getText() + "");
                Log.d("onclickButtonVerstuur", aantal + "");
               tekstFragmentAdapter adapter = new tekstFragmentAdapter(NieuwToevoegen.this,aantal);

            }});}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                goBack();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void goBack(){
        super.onBackPressed();
        Intent i = new Intent(NieuwToevoegen.this, startScherm.class);
        Log.d("backbutton", i + "");
        startActivity(i);
    }

    @Override
    public void onBackPressed() {
        goBack();
    }

    public void addFragment(int id) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(id, new fragmentItem());
        Log.d("addfragment", ft+"");
        ft.commit();
    }

我的适配器

public class tekstFragmentAdapter extends ArrayAdapter<tekstFragment> {
    private Context context;
    private Integer aantal;

        public tekstFragmentAdapter(Context context, int aantalFr) {
            super(context, 0, aantalFr);
            aantal=aantalFr;
        }
        FragmentManager fm = ((NieuwToevoegen)context).getFragmentManager();
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_item_layout, parent, false);
            }

            TextView tekst1 = (TextView) convertView.findViewById(R.id.textView1);
            TextView tekst2 = (TextView) convertView.findViewById(R.id.textView2);
            TextView tekst3 = (TextView) convertView.findViewById(R.id.textView3);

            switch(aantal) {
                case 1:
                    tekst1.setText("@string/een_item1_string");
                    addFragment(R.id.frameLayout1);
                    break;
                case 2:
                    tekst1.setText("@string/twee_item1_string");
                    tekst2.setText("@string/twee_item2_string");
                    addFragment(R.id.frameLayout1);
                    addFragment(R.id.frameLayout2);
                    break;
                case 3:
                    tekst1.setText("@string/drie_item1_string");
                    tekst2.setText("@string/drie_item2_string");
                    tekst3.setText("@string/drie_item3_string");
                    addFragment(R.id.frameLayout1);
                    addFragment(R.id.frameLayout2);
                    addFragment(R.id.frameLayout3);
                    break;
                default:
                    TextView aantalIngevuld = (TextView) convertView.findViewById(R.id.txtAantal);
                    aantalIngevuld.setEnabled(true);

                    break;}

            return convertView;
        }
    public void addFragment(int id) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(id, new fragmentItem());
        Log.d("addfragment", ft + "");
        ft.commit();
    }
    }

片段xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="fill_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:id="@+id/frameLayout1">

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:id="@+id/imageButton1"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/gallery_icon"
        android:background="#00000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textView1" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:id="@+id/frameLayout2">

        <ImageButton
            android:layout_width="120dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:id="@+id/imageButton2"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/gallery_icon"
            android:background="#00000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textView2" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:id="@+id/frameLayout3">

        <ImageButton
            android:layout_width="120dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:id="@+id/imageButton3"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/gallery_icon"
            android:background="#00000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textView3" />
    </FrameLayout>


</LinearLayout>

tekstFragment 仅包含以下内容

public class tekstFragment {
    private String tekst;

    public tekstFragment( String tekst) {
        this.tekst = tekst;
    }


    public String getTekst() {
        return tekst;
    }
}

我要添加的字符串

<string name="drie_item1_string">Voeg een foto toe van een pull/sweater/jas</string>
<string name="drie_item2_string">Voeg een foto toe van een t-shirt/top</string>
<string name="drie_item3_string">Voeg een foto toe van een rok/short/broek</string>
<string name="twee_item1_string">Voeg een foto toe van een pull/sweater/jas</string>
<string name="twee_item2_string">Voeg een foto toe van een kleedje/broekpak/playsuit</string>
<string name="een_item1_string">Voeg een foto toe van een kleedje/broekpak/playsuit</string>

修正空指针异常问题

public class tekstFragmentAdapter extends ArrayAdapter<tekstFragment> {
    private Context context;
    private Integer aantal;
    FragmentManager fm;
        public tekstFragmentAdapter(Context context, int aantalFr) {
            super(context, 0, aantalFr);
            aantal=aantalFr;
            this.context= context
            fm = ((NieuwToevoegen)context).getFragmentManager();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_item_layout, parent, false);
            }

            TextView tekst1 = (TextView) convertView.findViewById(R.id.textView1);
            TextView tekst2 = (TextView) convertView.findViewById(R.id.textView2);
            TextView tekst3 = (TextView) convertView.findViewById(R.id.textView3);

            switch(aantal) {
                case 1:
                    tekst1.setText("@string/een_item1_string");
                    addFragment(R.id.frameLayout1);
                    break;
                case 2:
                    tekst1.setText("@string/twee_item1_string");
                    tekst2.setText("@string/twee_item2_string");
                    addFragment(R.id.frameLayout1);
                    addFragment(R.id.frameLayout2);
                    break;
                case 3:
                    tekst1.setText("@string/drie_item1_string");
                    tekst2.setText("@string/drie_item2_string");
                    tekst3.setText("@string/drie_item3_string");
                    addFragment(R.id.frameLayout1);
                    addFragment(R.id.frameLayout2);
                    addFragment(R.id.frameLayout3);
                    break;
                default:
                    TextView aantalIngevuld = (TextView) convertView.findViewById(R.id.txtAantal);
                    aantalIngevuld.setEnabled(true);

                    break;}

            return convertView;
        }
    public void addFragment(int id) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(id, new fragmentItem());
        Log.d("addfragment", ft + "");
        ft.commit();
    }
    }

但是你为什么要用 Activtiy NieuwToevoegen 投射你的上下文,只需将上下文作为参数传递给你的适配器

我更喜欢 post 另一个答案,因为它不会与第一个答案相关,我将放置一个结构代码并根据您的需要继续:

package my.game.androidstarter;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class NieuwToevoegen extends Activity {

    Button buttonVerstuur;
    EditText editTextInput; // so user could insert a text from 1 to 3 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nieuw_toevoegen);
        buttonVerstuur = (Button) findViewById(R.id.buttonVerstuur);
        editTextInput  = (EditText) findViewById(R.id.yourEditText); // add your edit text in your xml layout "nieuw_toevoegen"
        editTextInput.setEnabled(false);

        buttonVerstuur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int aantal = Integer.parseInt(editTextInput.getText().toString()); // get the number input inserted by user
                Log.d("onclickButtonVerstuur", aantal + "");
                callFragment(aantal);

            }});}

    private void callFragment(int aantal) {

            switch (aantal) {
            case 1:
                YourCustomFragment1 fragment = new YourCustomFragment1(); // if 1 --> your custom fragment one

                break;
            case 2:
                YourCustomFragment2 fragment = new YourCustomFragment2(); // if 2 --> your custom fragment two

                break;
            case 3:
                YourCustomFragment3 fragment = new YourCustomFragment3(); // if 2 --> your custom fragment three

                break;
            default:
                // TODO if user insert a number that is not 1 or 2 or 3 (and he can do thant)
                break;

            }

            // charge the fragment that was created depending on  input number
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit(); // You should have frame_container (frame layout) defined in your nieuw_toevoegen.xml
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                goBack();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void goBack(){
        super.onBackPressed();
        Intent i = new Intent(NieuwToevoegen.this, startScherm.class);
        Log.d("backbutton", i + "");
        startActivity(i);
    }

    @Override
    public void onBackPressed() {
        goBack();
    }

现在您所要做的就是创建三个不同的自定义片段(class 扩展 Fragment),然后使用不同的布局自定义每个片段, 浏览量, 按钮, 图片 ....

希望这已经够清楚了!