我想在消息 框中显示我的用户ID,但是选择了微调器值,然后显示了ID,但ID没有&t不显示。这是我的代码。
主类代码:
public class AgentProgress extends Activity {
Spinner sp1, sp2;
ListView ls1;
ProgressBar pbbar;
ArrayList<String> names = new ArrayList<String>();
ArrayList<AgentID> web;
ArrayList<String> wordlist;
ArrayAdapter<String> adaptername;
ArrayList<HashMap<String, String>> menuItems1 = new ArrayList<HashMap<String, String>>();
static final String KEY_TABLE = "AGENTDATA";
static final String KEY_AMOUNT = "Parent_Agent_Name";
static final String KEY_USER = "Parent_Agent_Id_Pk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agent_progress);
sp1 = (Spinner) findViewById(R.id.spnames);
sp2 = (Spinner) findViewById(R.id.spyear);
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = thisYear; i >= 2011; i--) {
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, years);
sp2.setAdapter(adapter);
pbbar = (ProgressBar) findViewById(R.id.pbbar);
pbbar.setVisibility(View.GONE);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
try {
web = new ArrayList<AgentID>();
wordlist = new ArrayList<String>();
if (connectionAvailable() == true) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
WebServiceCAll com = new WebServiceCAll();
AgentID agent = new AgentID();
// final String string1 = getIntent().getStringExtra("Agentid");
String strXml = com.LoadAgent("LoadAgent");
//messageBox("test", strXml);
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(strXml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_TABLE);
//String[] arList = null;
//String[] arListkey = null;
// int conttotal=0;
// looping through all item nodes <item>
for (int j = 0; j < nl.getLength(); j++) {
// creating new HashMap
Map<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
//map.put(KEY_AMOUNT, parser.getValue(e, KEY_AMOUNT));
//map.put(KEY_USER,parser.getValue(e,KEY_USER));
// adding HashList to ArrayList
//menuItems.add(map);
Element e = (Element) nl.item(j);
//messageBox("test", "fahad");
agent.setID(map.put(KEY_USER, parser.getValue(e, KEY_USER)));
agent.setname(map.put(KEY_AMOUNT, parser.getValue(e, KEY_AMOUNT)));
names.add(map.put(KEY_AMOUNT, parser.getValue(e, KEY_AMOUNT)));
}
} else {
messageBox("ERROR", "No Internet Service");
}
} catch (Exception ex) {
}
adaptername = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, names);
sp1.setAdapter(adaptername);
AgentProgress.this.runOnUiThread(new Runnable() {
@Override
public void run() {
adaptername.notifyDataSetChanged();
}
});
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int agr, long l) {
HashMap<String,Object> obj=(HashMap<String,Object>) adaptername.getItem(agr);
String pr=(String) obj.get(KEY_USER);
messageBox("",pr);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void messageBox(String method, String message) {
AlertDialog.Builder messageBox = new AlertDialog.Builder(this);
messageBox.setTitle(method);
messageBox.setMessage(message);
messageBox.setCancelable(false);
messageBox.setNeutralButton("OK", null);
messageBox.show();
}
private boolean connectionAvailable() {
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_SUPL).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
}
return connected;
}
}
它在哈希映射项目选择的侦听器中显示和错误消息错误是:
incompatible types: String cannot be converted to HashMap<String,Object>
答案 0 :(得分:0)
您尝试获取Item
而未获得Adapter
。
替换
HashMap<String,Object> obj=(HashMap<String,Object>) adaptername.getItem(agr);
与
HashMap<String,Object> obj=(HashMap<String,Object>) adapterView.getAdapter().getItem(agr);