如何基于对话框片段中的项目选择和主要活动中的文本值来设置文本到文本视图,是否可以?

时间:2016-05-13 07:50:52

标签: java android android-fragments textview android-dialogfragment

这里我试图在我的活动中将文本设置为textview。 根据活动中文本视图的文本和自定义对话框片段中的选定项目,将文本设置为文本视图。

对话框片段由回收者视图和按钮组成。当从我的活动的文本视图的片段和文本中选择项目时,以及两种情况下将settext设置为textview。我尝试了不同的代码,但我没有得到如何实现它任何人给我解决方案。有没有办法搞定它。

通过使用文本和所选项目的值,将数学和条件添加到其中。 settext到textview。我是编码的初学者,任何人都可以帮助我,请我尝试从5天开始实现它。请提前帮助我

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="YOUR DAILY TARGET"
        android:layout_gravity="center"
        android:id="@+id/textView7"
        android:layout_marginTop="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:layout_gravity="center"
        android:id="@+id/res"
        android:layout_marginTop="20dp"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Select a glass"
        android:layout_marginLeft="10dp"
        android:id="@+id/textView6"
        android:layout_gravity="center" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="65dp"
        android:src="@android:drawable/ic_input_add" />
</LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="0"
            android:background="@drawable/circle"
            android:gravity="center"
            android:id="@+id/glasses"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="31dp"
            android:layout_marginEnd="31dp"
            android:layout_marginBottom="121dp" />
    </RelativeLayout>


</LinearLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

    private Listen selectedListen;

    protected void setDataFromMyDialog(Listen listen) {
        this.selectedListen = listen;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
                .getBoolean("isFirstRun", true);

        if (isFirstRun) {
            //show start activity

            startActivity(new Intent(MainActivity.this, Page.class));
            Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
                    .show();
        }


        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("isFirstRun", false).commit();

        TextView textView = (TextView)findViewById(R.id.res);
        Intent r = getIntent();
        double res = r.getDoubleExtra("res", 0);
        textView.setText(+res + "L"); // with this text value 

        double ram = Double.parseDouble(textView.getText().toString());

        TextView gls = (TextView) findViewById(R.id.glasses);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiaFragment df = new DiaFragment();
                df.show(MainActivity.this.getSupportFragmentManager(), "Frag");
            }
        });

    }

使用上面的textview文本和我的对话框片段中的选定项目,使用TextView眼镜的设置文本的值。这可能是这样的

DiaFrag:

public class DiaFragment extends DialogFragment {

    ListAdapter listAdapter;

    public DiaFragment() {
        // 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_dialog, container, false);
        setCancelable(false);
        Button ok = (Button) v.findViewById(R.id.submit);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false);
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.list);
        listAdapter = new ListAdapter(getContext(),getData());
        recyclerView.setAdapter(listAdapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity)getActivity()).setDataFromMyDialog(listAdapter.getSelectedData());
                dismiss();
            }
        });

        return v;
    }

    public static List<Listen> getData()
    {
        List<Listen> data = new ArrayList<>();
        int[] images = {R.drawable.bottle,R.drawable.lotion,R.drawable.soap,R.drawable.soda,R.drawable.sodaa};
        String[] texts = {"250ml","300ml","500ml","750ml","1ltr"};
        for (int i=0;i<texts.length && i<images.length;i++){
            Listen current = new Listen();
            current.img = images[i];
            current.text= texts[i];
            data.add(current);
        }
        return data;
    }
}

0 个答案:

没有答案