gpt4 book ai didi

java - 带 ListView 的微调器

转载 作者:行者123 更新时间:2023-12-01 23:38:49 25 4
gpt4 key购买 nike

我想问一下,我正在和旋转器一起工作。我有一个 ListView 和一个微调器。在我的微调器中,我声明“月”,“六月”,“七月”,“八月”,“九月”,“十月”,“十一月”,“十二月”

在 ListView 中,最初是空的。当我点击“August”时,它会在 ListView 中显示八月的数据(august.java),请问有什么办法可以做到吗?

因为现在,我正在上课..意思是,当我单击 Aug 时,它将打开新的 Intent 。但我想将它合并到一个类中,这样当我单击“八月”时,它会在 ListView 中发生变化,而不是打开新的 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));

}

我得到的数据代码。八月.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);

}
});


}

}

最佳答案

使用下面的代码我已经尝试过它工作正常

 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

}

}

关于java - 带 ListView 的微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18266824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com