日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了根據微調按鈕選擇顯示其他數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個XML文件,其中我在微調控制框中顯示了第一個標記(寶藏名稱)中的文本,以供用戶選擇。一旦選擇,我需要能夠訪問存儲在XML文檔(存儲在設備上的數據文件中)中的與所選寶藏名稱相關聯的其他數據。我已經解析了XML文件,并將每一項存儲在單獨的數組列表中。當用戶點擊一個按鈕(獲取線索)時,我需要能夠顯示與該寶藏相關的第一條線索。這個項目今天午夜前要交,這是我看起來拿不到的最后一件作品。任何幫助都將不勝感激。我添加了代碼以嘗試執行此操作:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
//import java.lang.reflect.Array;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.Bundle;
import android.app.Activity;
//import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class PlayGeoTreasureActivity extends Activity implements OnItemSelectedListener {

    ArrayList<String> treasureList=new ArrayList<String>();
    ArrayList<String>clue1List=new ArrayList<String>();
    ArrayList<String> clue2List=new ArrayList<String>();
    ArrayList<String> clue3List=new ArrayList<String>();
    ArrayList<String> answerList=new ArrayList<String>();
    ArrayList<String> locationList=new ArrayList<String>();
    ArrayList<String> pointValueList=new ArrayList<String>();


    XmlPullParserFactory parser;
    XmlPullParser xpp;
    TextView selected;

    Spinner spinnerTreasures;
    //Spinner spinnerTreasures = new Spinner(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_geo_treasure);

        spinnerTreasures = (Spinner)findViewById(R.id.treasuresSpinner);


        //get the xml file
        File filename = new File(getFilesDir(), "treasure.xml");

        //check to see if file exists.  If it does, read it.

    try {
        if(filename.exists())
        {       
            readXML(filename);
        }
        else
        {
            Toast.makeText(null, "File not Found", Toast.LENGTH_LONG).show();
        }  
        }catch (FileNotFoundException e) {
            //String errorMessage=(e.getMessage()==null)?"Message is Empty":e.getMessage();
            //Log.e("GeoTreasureGameLog",errorMessage);
            Log.e("GeoTreasureGameLog", (e.toString()));
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            //String errMessage=(e.getMessage()==null)?"Message is Empty":e.getMessage();
            //Log.e("GeoTreasureGameLog",errMessage);
            Log.e("GeoTreasureGameLog", (e.toString()));
            e.printStackTrace();
        }

    }


    private void readXML(File filename) throws XmlPullParserException, FileNotFoundException {
        // pull parser to read xml file
                    parser = XmlPullParserFactory.newInstance();
                    xpp = parser.newPullParser();

                    // point the xml parser to file
                    xpp.setInput(new FileReader(filename)); 

                    // get start and end tags
                    int eventType = xpp.getEventType();

                    // set current tag
                    String currentTag="";

                    // current value of the tag's element

                    String currentElement="";

                    //int counter = 0;
                    try{
                    // parse the entire xml file until done
                    while (eventType != XmlPullParser.END_DOCUMENT)
                    { 
                        // look for start tags
                        if(eventType == XmlPullParser.START_TAG)
                        {
                            // get the name of the start tag
                            currentTag = xpp.getName();

                            if (currentTag.equals("TreasureName"))
                            {
                                currentElement = xpp.nextText();
                                treasureList.add(currentElement);
                            }
                                else if (currentTag.equals("ClueOne"))
                                {
                                    currentElement = xpp.nextText();
                                    clue1List.add(currentElement);
                                }
                                else if (currentTag.equals("ClueTwo"))
                                {
                                    currentElement = xpp.nextText();
                                    clue2List.add(currentElement);
                                }
                                else if (currentTag.equals("ClueThree"))
                                {
                                    currentElement = xpp.nextText();
                                    clue3List.add(currentElement);
                                }
                                else if (currentTag.equals("Answer"))
                                {
                                    currentElement = xpp.nextText();
                                    answerList.add(currentElement);
                                }
                                else if (currentTag.equals("TreasureLocation"))
                                {
                                    currentElement = xpp.nextText();
                                    locationList.add(currentElement);
                                }
                                else if (currentTag.equals("PointValue"))
                                {
                                    currentElement = xpp.nextText();
                                    pointValueList.add(currentElement);
                                }
                        }
                        eventType = xpp.next();
                        }
                    } catch (Exception e)

                    {
                        //Log.e("GeoTreasureGameLog", e.getMessage());
                        Log.e("GeoTreasureGameLog", (e.toString())); 

                    }

                    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,treasureList);

                    selected=(TextView) findViewById(R.id.itemSelected);
                    spinnerTreasures.setOnItemSelectedListener(PlayGeoTreasureActivity.this);
                    spinnerTreasures.setAdapter(spinnerArrayAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.play_geo_treasure, menu);
        return true;
    }




@Override
    public void onItemSelected(AdapterView<?> parent,
            View v, int position, long id) {
        selected.setText(treasureList.get(position));
        int i=treasureList.indexOf(selected);
        clueOne=clue1List.get(i).toString();
        clueTwo=clue2List.get(i).toString();
        clueThree=clue3List.get(i).toString();
        Button getClue = (Button)findViewById(R.id.getClueBtn);
        getClue.setOnClickListener((OnClickListener) this);

        }
        public void OnClick(View v) throws IOException{
            int buttonClicks = 3;
            TextView clue1=(TextView)findViewById(R.id.clue1TxtView);
            TextView clue2 = (TextView)findViewById(R.id.clue2TextView);
            TextView clue3=(TextView)findViewById(R.id.clue3TextView);
            if (buttonClicks == 3){
                clue1.setText(clueOne);
                buttonClicks=2;
            }
            else if (buttonClicks==2){
                clue2.setText(clueTwo);
                buttonClicks=1;
            }
            else if (buttonClicks==1){
                clue3.setText(clueThree);
                buttonClicks=0;
            }
            else if (buttonClicks==0)
                Toast.makeText(getBaseContext(), "No more clues are available", Toast.LENGTH_LONG).show();

    }


    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        selected.setText("Please choose a treasure");

    }


}

感謝我能得到的任何輸入!

好的,我嘗試了這樣做,但收到錯誤:

09-15 16:33:47.439: E/AndroidRuntime(1440): FATAL EXCEPTION: main
09-15 16:33:47.439: E/AndroidRuntime(1440): java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
09-15 16:33:47.439: E/AndroidRuntime(1440):     at java.util.ArrayList.get(ArrayList.java:310)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at com.rasmussen.geotreasuresgame.PlayGeoTreasureActivity.onItemSelected(PlayGeoTreasureActivity.java:182)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at android.widget.AdapterView.access$200(AdapterView.java:49)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at android.os.Handler.handleCallback(Handler.java:730)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at android.os.Looper.loop(Looper.java:137)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at java.lang.reflect.Method.invokeNative(Native Method)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at java.lang.reflect.Method.invoke(Method.java:525)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-15 16:33:47.439: E/AndroidRuntime(1440):     at dalvik.system.NativeStart.main(Native Method)

推薦答案

我在使用微調工具時也遇到了相同的錯誤(java.lang.ArrayIndexOutOf邊界異常:長度=12;索引=-1)。在我的例子中,我試圖在UI線程外部的微調函數上設置Adapter。在將相關代碼包裝在runOnUiThread中之后,問題就解決了。

這篇關于根據微調按鈕選擇顯示其他數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:微調 按鈕 數據 顯示 選擇
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定