0

我的应用 2 微调器,基本上是允许用户选择值的下拉列表。Spinner1 在下拉菜单中有 4 个选项,而 Spinner2 在下拉菜单中有 5 个选项。

如果用户从 spinner1 中选择 option1 并从 spinner2 中选择 option1,则项目符号列表 [在此应用程序的 HTML webapp 版本中,我使用 li 标签] 应显示在视图其余部分的片段中。图像将使事情更清楚。
[1]: http: //imgur.com/qaqHZ 现在,如果我在片段中使用 webview 来显示文本,我如何在 20 个条件下动态地做到这一点[微调器 1 中的 4 个选项和微调器 2 中的 4 个选项,所以4*5]?

我附上代码

package com.temp1.android;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

    private Spinner spinner1, spinner2;
    private Button btnSubmit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addItemsOnSpinner2();
        addListenerOnButton();
        addListenerOnSpinnerItemSelection();

    }

    //add items into spinner dynamically
    public void addItemsOnSpinner2() {

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        List<String> list = new ArrayList<String>();
        list.add("Equipment");
        list.add("Indications");
        list.add("Illustration");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter);
    }

    public void addListenerOnSpinnerItemSelection(){

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

    //get the selected dropdown list value
    public void addListenerOnButton() {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);

        btnSubmit = (Button) findViewById(R.id.btnSubmit);

        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(MyAndroidAppActivity.this,
                        "OnClickListener : " + 
                        "\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
                        "\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
                        Toast.LENGTH_SHORT).show();
            }

        });



    }

}





import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        Toast.makeText(parent.getContext(), 
                "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

目前我正在使用 toast 来调试事件监听器

4

1 回答 1

0

我猜你已经完成了片段部分,所以我假设你Fragment的布局文件R.layout.main中有一个 id R.id.webviewfrag

该片段的类可能是这样的:

public class WebViewerFragment extends Fragment {

    private WebView mWebView;
    // a two dimensional array representing the data to put in the WebView
    private String[][] mData = new String[4][5];// put data in it

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mainView = (View) inflater.inflate(R.layout.frag_layout, container, false);
        mWebView = (WebView) mainView.findViewById(R.id.webView1); 
        return mainView;
    }

    public void updateWebView(int firstSelection, int secondSelection) {
         if (firstSelection == AdapterView.INVALID_POSITION || secondSelection == AdapterView.INVALID_POSITION) {
              return;  
         } else {
              if (mWebView != null) {
                   mWebView.loadData(mData[firstSelection][secondSelection], "text/html", null);
              }
         }
    }
}

R.layout.frag_layout将会:

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

    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

那么你在onClick回调中所要做的就是:

@Override
public void onClick(View v) {
       WebViewerFragment wvf = (WebViewerFragment) getSupportFragmentManager().findFragmentById(R.id.webviewfrag);
       if (wvf != null && wvf.getView() != null) { 
            wvf.updateWebView(spinner1.getSelectedItemPosition(), spinner2.getSelectedItemPosition());
       }
}
于 2012-11-29T18:24:45.817 回答