带有列表视图的微调器

时间:2013-08-16 06:05:55

标签: java android spinner

我想问一下, 我正在和微调器一起工作。 我有一个列表视图和一个微调器。 在我的微调器中,我宣布“月”,“六月”,“七月”,“八月”,“九月”,“八月”,“新手”,“十二月”

在列表视图中,最初是空的。 当我点击“八月”时,它会在列表视图中显示八月(august.java)的数据, 我可以知道有什么办法吗?

因为现在,我正在上课.. 意思是,当我点击8月时,它会打开新的意图。 但我想将它合并到一个类中,因此当我单击“August”时,它将在列表视图中更改而不会打开新的intent。 有办法吗?

这是我的代码

String [] months = {
                "June",
                "July",
                "August",
                "Septemeber",
                "November",
                "December",

我的date.java

的一部分
//SpinnerView
            s1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, months);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                //Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show();
            }
            public void onNothingSelected(AdapterView<?>arg0) {}
            });


    if ( s1.equals("August")) {

        startActivity(new Intent(date.this,MainActivity.class));
    }
    else{
        startActivity(new Intent(date.this,august
                .class));

    } 

我得到的数据代码。 august.java

public class august extends ListActivity {

    String URL = "http://api.eventful.com/rest/events/search?app_key=42t54cX7RbrDFczc&location=singapore&page_size=20&date=august";

    // XML node keys
    static final String KEY_EVENT = "event"; // parent node
    static final String KEY_TITLE = "title";
    static final String KEY_URL = "url";
    static final String KEY_DESC = "description";
    static final String KEY_START_TIME = "start_time";
    static final String KEY_STOP_TIME = "stop_time";
    static final String KEY_TIME = "combine_time";
    static final String KEY_VENUE_NAME = "venue_name";
    static final String KEY_COUNTRY_NAME = "country_name";
    static final String KEY_VENUE_ADDRESS = "venue_address";
    static final String KEY_VENUE = "venue";
    static final String KEY_LATITUDE = "latitude";
    static final String KEY_LONGITUDE = "longitude";

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

        final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_EVENT);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_URL, parser.getValue(e, KEY_URL));

            map.put(KEY_DESC, "Description: " + parser.getValue(e, KEY_DESC));
            map.put(KEY_START_TIME, parser.getValue(e, KEY_START_TIME));
            map.put(KEY_STOP_TIME, parser.getValue(e, KEY_STOP_TIME));
            if (parser.getValue(e, KEY_STOP_TIME) != "") {
                map.put(KEY_TIME, parser.getValue(e, KEY_START_TIME) + "-"
                        + parser.getValue(e, KEY_STOP_TIME));
            } else {
                map.put(KEY_TIME, parser.getValue(e, KEY_START_TIME));
            }
            map.put(KEY_VENUE_NAME, parser.getValue(e, KEY_VENUE_NAME));
            map.put(KEY_COUNTRY_NAME, parser.getValue(e, KEY_COUNTRY_NAME));
            map.put(KEY_VENUE_ADDRESS, parser.getValue(e, KEY_VENUE_ADDRESS));
            map.put(KEY_LATITUDE, parser.getValue(e, KEY_LATITUDE));
            map.put(KEY_LONGITUDE, parser.getValue(e, KEY_LONGITUDE));
            map.put(KEY_VENUE, parser.getValue(e, KEY_VENUE_NAME) + ", "
                    + parser.getValue(e, KEY_VENUE_ADDRESS));

            // adding HashList to ArrayList

            menuItems.add(map);

        }

        // Adding menuItems to ListView

        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC,
                        KEY_COUNTRY_NAME, KEY_VENUE, KEY_LATITUDE,
                        KEY_LONGITUDE, KEY_TIME, }, new int[] { R.id.title,
                        R.id.description, R.id.countryName, R.id.venueName,
                        R.id.lat, R.id.lng, R.id.startTime });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String title = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();
                String description = ((TextView) view
                        .findViewById(R.id.description)).getText().toString();

                String venue = ((TextView) view.findViewById(R.id.venueName))
                        .getText().toString();
                String lat = ((TextView) view.findViewById(R.id.lat)).getText()
                        .toString();

                String lng = ((TextView) view.findViewById(R.id.lng)).getText()
                        .toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        SingleMenuItemActivity.class);
                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_DESC, description);
                in.putExtra(KEY_VENUE, venue);
                in.putExtra(KEY_LATITUDE, lat);
                in.putExtra(KEY_LONGITUDE, lng);
                startActivity(in);

            }
        });


    }

}

4 个答案:

答案 0 :(得分:2)

使用以下代码我试过它工作正常

 import java.util.ArrayList;
    import java.util.List;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemSelectedListener;

    public class MainActivity extends Activity implements OnItemSelectedListener {
        private Spinner sp;

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

        }

        public void uI() {
            sp = (Spinner) findViewById(R.id.mydsp);
            sp.setOnItemSelectedListener(this);
        }

        public void spElements() {
            List<String> months = new ArrayList<String>();
            months.add("June");
            months.add("July");
            months.add("August");
            months.add("September");
            months.add("October");
            months.add("November");
            months.add("December");

            // Creating adapter for spinner
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, months);

            // Drop down layout style - list view with radio button
            dataAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            // attaching data adapter to spinner
            sp.setAdapter(dataAdapter);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public void onItemSelected(AdapterView<?> main, View view, int position,
                long Id) {

            String item = main.getItemAtPosition(position).toString();

  int index = item.getSelectedItemPosition();
            Toast.makeText(main.getContext(), "You selected Month is: " + item,
                    Toast.LENGTH_LONG).show();

        }

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

        }

    }

答案 1 :(得分:1)

ListView中显示自定义数据实际上并不复杂。涉及几个步骤。

展示什么 这很容易。当用户从您的微调器中选择一个项目时,您就知道要显示什么;他选择的月份。现在,由于您的月份是实际课程,我建议您实施一个变量,让您知道所选的内容。该变量将被其他类用于实际显示内容。

如何展示 这是更棘手的地方。您的ListView会显示一个列表。列表中包含的项目需要由您设置。您需要定义您实际想要显示的月份课程中的数据。这是你的设计有点短暂的地方。您想要展示的项目理想情况下需要是同质的。因此,从一个月开始,可以更容易地显示月份对象列表以及几个不同的项目。我建议您将实施从ListView切换为TextView内的简单ScrollView

显示数据 通过上述修改,每次进行选择时,都可以将其可视化。现在,您可以在这里应用一个小技巧。只需在您的对象中实现一个toString()方法,该方法将以String的形式返回给用户。通过这种方式,您可以控制用户看到的内容,并对使用月份对象显示的内容负责,这通常是一个很好的设计选择。

我希望你会考虑这个新设计。我相信你在尝试使用ListViewArrayAdapter和ViewHolder模式时没有必要。此外,您需要预加载所有月份对象,或者使用David说明AsyncTask来检索数据。切勿使用网络操作阻止UI线程。这不仅是一种不好的做法,更新版本的Android会引发异常。

答案 2 :(得分:0)

以下是您的一个例子

 spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
                {
                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) 
                    {
                        String A = arg0.getSelectedItem().toString();
                        new abc(Context, A).excute();


                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) 
                    {
                    }
                });

    private class abc extends AsyncTask<Void, Void, HashMap<String, String>> 
    {
        ProgressDialog pd;
        HashMap<String, String> map;
        String Month;

        abc(Context context, String a)
                {
            pd = new ProgressDialog(context);
            map = new HashMap<String, String>();
Month = a;
                } 
        protected void onPreExecute() 
        {
            pd.setTitle("Please Wait...");
            pd.setMessage("Saving...");
            pd.setCancelable(false);
            pd.show();
        } 
        protected void onPostExecute(HashMap<String, String> result)
        {
            // Get value from map and sent it to your adapter class where you can set value to textview in your listview

            String title = result.get(KEY_TITLE);

             ListAdapter adapter = new SimpleAdapter(this, menuItems,
                        R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC,
                                KEY_COUNTRY_NAME, KEY_VENUE, KEY_LATITUDE,
                                KEY_LONGITUDE, KEY_TIME, }, new int[] { R.id.title,
                                R.id.description, R.id.countryName, R.id.venueName,
                                R.id.lat, R.id.lng, R.id.startTime });

                setListAdapter(adapter);

                // selecting single ListView item
                ListView lv = getListView();
            if(pd.isShowing()) pd.dismiss();
        } 

        @Override
        protected HashMap<String , String> doInBackground(Void... params) 
        {
             XMLParser parser = new XMLParser();
                String xml = parser.getXmlFromUrl(URL); // getting XML
                Document doc = parser.getDomElement(xml); // getting DOM element

                NodeList nl = doc.getElementsByTagName(KEY_EVENT);
                // looping through all item nodes <item>
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                    map.put(KEY_URL, parser.getValue(e, KEY_URL));

                    map.put(KEY_DESC, "Description: " + parser.getValue(e, KEY_DESC));
                    map.put(KEY_START_TIME, parser.getValue(e, KEY_START_TIME));
                    map.put(KEY_STOP_TIME, parser.getValue(e, KEY_STOP_TIME));
                    if (parser.getValue(e, KEY_STOP_TIME) != "") {
                        map.put(KEY_TIME, parser.getValue(e, KEY_START_TIME) + "-"
                                + parser.getValue(e, KEY_STOP_TIME));
                    } else {
                        map.put(KEY_TIME, parser.getValue(e, KEY_START_TIME));
                    }
                    map.put(KEY_VENUE_NAME, parser.getValue(e, KEY_VENUE_NAME));
                    map.put(KEY_COUNTRY_NAME, parser.getValue(e, KEY_COUNTRY_NAME));
                    map.put(KEY_VENUE_ADDRESS, parser.getValue(e, KEY_VENUE_ADDRESS));
                    map.put(KEY_LATITUDE, parser.getValue(e, KEY_LATITUDE));
                    map.put(KEY_LONGITUDE, parser.getValue(e, KEY_LONGITUDE));
                    map.put(KEY_VENUE, parser.getValue(e, KEY_VENUE_NAME) + ", "
                            + parser.getValue(e, KEY_VENUE_ADDRESS));

                    // adding HashList to ArrayList

                    menuItems.add(map); 
                    }
                return map;
        }
    }

希望它对你有所帮助。

答案 3 :(得分:0)

试试这个:

选择任何月份后,尝试为Listview设置适配器。像这样:

s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                if(index>0)
                {
                             String Month = MonthArray[index];

                         // get data as per your month here.
                        //set your adapter for listview here according to your month data.


                }
            }
            public void onNothingSelected(AdapterView<?>arg0) {}
        });

如果您想从服务器加载数据然后想要更改适配器,那么我建议您使用AsyncTask来加载数据。并在完成Listview的数据加载集适配器。

希望它能帮助!!