Android的主/从流量 - 在活动操作细节片段浏览 [英] Android Master/Detail flow - manipulating Detail fragment Views in Activity

查看:129
本文介绍了Android的主/从流量 - 在活动操作细节片段浏览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我是新Android开发,并试图环绕主/详细信息流设计我的头。因此,我使用的是默认的Eclipse类为您创建 - 所以 ScreenDetailActivity ScreenDetailFragment ScreenListActivity ScreenListFragment 。我的一个详细的片段使用包含一系列检查和字段用于输入数据的布局,并且被认为使用该片段以使活动类一个按钮,数据传递给计算器类,然后执行与一些基本的计算数据。例如,在由有关详细信息片段之一使用了 calculation_layout.xml 文件中找到:

So I am new to Android development, and trying to wrap my head around the Master/Detail flow design. As such, I'm using the default classes Eclipse creates for you - so ScreenDetailActivity, ScreenDetailFragment, ScreenListActivity, and ScreenListFragment. One of my Detail fragments uses a layout that contains a series of checks and fields for entering data, and a button that is supposed to make the Activity class using the fragment pass that data to a calculator class, which then performs some basic calculations with the data. For example, found in the calculation_layout.xml file used by one of the Detail fragment in question:

<EditText android:id="@+id/edit_value"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="numberDecimal|numberSigned"
      android:hint="@string/background_value" />

<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/button_singleCalc"
    android:onClick="calculate" />

我觉得我有按钮的计算函数的工作(即应用程序时的计算为空或做琐碎的事情不会崩溃);它在这两个实施 ScreenListActivity ScreenDetailActivity ,因为无论是可以使用的片段。

I think I have the the Button's calculate function working (i.e. the app doesn't crash when the calculate is empty or does trivial things); it's implemented in both ScreenListActivity and ScreenDetailActivity, since either could be using the fragment.

然而,每当我尝试访问的EditText对象的详细信息片段,应用程序崩溃。我想是这样的:

However, whenever I try to access the EditText objects in the Detail fragment, the app crashes. I'm trying something like this:

public void calculate(View view){
    //Retrieve all the information, and set the values in the Calculator
    EditText editText = (EditText) findViewById(R.id.edit_value);
    String number = editText.getText().toString();
    double angle = Double.parseDouble(number);

    Calculator.longCalc();
}

在我ScreenDetailFragment这样膨胀的布局,这与Eclipse生成的默认方法是如何工作的(其中MITEM基本上就是一个包含信息的小类哪些片段要displazed的一个实例):

And inflating the layout in my ScreenDetailFragment like this, not unlike how the default method generated by Eclipse works (where mItem is basically an instance of a little class containing information on which fragment should be displazed):

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // The returned view
        View rootView;
        // If mItem is non-null...
        if (mItem != null) {
            if (mItem.title == "Calculation") {
                // If the title is Calculation, use the calculation layout
                rootView = inflater.inflate(R.layout.calculation_layout, container, false);
            } else {
                // Otherwise, show the dummy content as text in a TextView
                rootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
                ((TextView) rootView.findViewById(R.id.screen_detail)).setText(mItem.title);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
        }

        return rootView;
    }

结果,正如前面所说的,是一个崩溃。

The result, as said earlier, is a crash.

我认为我所应该做的就是以某种方式获得 rootView 从活动,但我真的不知道该怎么办,安全和有效。

I assume that what I am supposed to do is somehow access rootView from the Activity, but I don't really know how to do that safely and effectively.

有人可以给我一些指点吗?

Can someone give me some pointers here?

更新:

我曾尝试实现OnClickListener,设置它这样,当特定的布局充气:

I have tried implementing OnClickListener, setting it up as such when that particular layout is inflated:

((Button)rootView.findViewById(R.id.button_calc)).setOnClickListener(this);

和实施的onClick(视图)功能这样:

and implementing the onClick(View) function as such:

public void onClick(View view) {
        //Retrieve all the information, and set the values in the Calculator
        view = (View) view.getParent();
        EditText editText = (EditText) layout.findViewById(R.id.edit_phiD);
        String number = editText.getText().toString();

        Calculator.angle = Double.parseDouble(number) * 2.0 * Math.PI/360.0;

        Calculator.longCalc();
    }

然而,错误仍然存​​在。它也仍然存在,如果我重铸 ViewParent 的LinearLayout ,一个的ViewGroup ,或者如果我使用视图直的是来了。需要明确的是,我试图让在被点击的按钮的父布局,这样我就可以去回落到该布局的其他子查看和访问他们的状态

However, the error persists. It also persists if I recast the ViewParent to a LinearLayout, a ViewGroup, or if I use view straight as is comes. To be clear, I am trying to get at the parent layout of the button that was clicked, so that I can go back down into that layout's other child Views and access their states.

推荐答案

您不需要通过你的活动,以实现这一目标。删除的onclick线和布局添加一个ID为按钮:

You don't need to go through your activity in order to achieve this. Remove the onclick line and add an id for the button in your layout:

<Button android:id="@+id/calc_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="@string/button_singleCalc" />

然后,只需您的片段中添加一个OnClickListener到你的按钮。这样的事情:

Then, simply add an OnClickListener to your button within your Fragment. Something like that:

private View mRootView;
private  EditText mEditText;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // If mItem is non-null...
    if (mItem != null) {
        if (mItem.title == "Calculation") {
            // If the title is Calculation, use the calculation layout
            mRootView = inflater.inflate(R.layout.calculation_layout, container, false);
            mEditText = (EditText) mRootView.findViewById(R.id.edit_phiD);    
            ((Button)mRootView.findViewById(R.id.calc_button)).setOnClickListener(this);         
        } else {
            // Otherwise, show the dummy content as text in a TextView
            mRootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
            ((TextView) mRootView .findViewById(R.id.screen_detail)).setText(mItem.title);
        }
    } else {
        mRootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
    }

    return mRootView;
}

(你的片段需要实现OnClickListener此)。然后你会得到你的片段的回调,和这里的,你在那里做什么:

(Your fragment needs to implement OnClickListener for this). Then you'll get a callback on your Fragment, and here's what you do there:

public void onClick(View v){
    //Retrieve all the information, and set the values in the Calculator
    String number = mEditText.getText().toString();
    double angle = Double.parseDouble(number);

    Calculator.longCalc();
}

这篇关于Android的主/从流量 - 在活动操作细节片段浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆