我正在試圖做的是: - 要顯示微調器中的狀態,請從數據庫加載並填充微調器。在選擇狀態下,根據狀態,城市必須填充另一個微調。setOnItemSelectedListener在spinner中動態填充項目時不起作用
我所取得的是: 微調器得到填充數據庫的值,但setOnItemSelectedListener
不起作用。
setOnItemSelectedListener
方法工作時,我靜靜地將數據放入微調器。
我的資料來源:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner sp_state,sp_city,sp_area;
Button ok;
int state_position;
String s_data,c_data,a_data,selected_state;
ArrayList<Satae_Pojo> new_data;
Network_Class net=new Network_Class();
ArrayList<Satae_Pojo> state=new ArrayList<Satae_Pojo>();
ArrayList<Integer> state_id=new ArrayList<Integer>();
ArrayList<String> state_name=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search_for_area);
sp_state=(Spinner)findViewById(R.id.spinner_state);
sp_city=(Spinner)findViewById(R.id.spinner_city);
sp_area=(Spinner)findViewById(R.id.spinner_area);
ok=(Button)findViewById(R.id.btn_ok);
List<String> list=new ArrayList<String>();
list.add("Please Select Area");
list.add("Phulewadi");
list.add("Vikramnagar");
list.add("Kadamwadi");
list.add("Rajarampuri");
getStates();
sp_state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3)
{
System.out.println("This is spinner");
//String id=parent.getItemAtPosition(pos).toString();
//System.out.println("This is selected id "+id);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
ArrayAdapter<String> state_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, state_name);
state_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_state.setAdapter(state_adapter);
}
public void getStates()
{
new Thread(new Runnable()
{
@Override
public void run()
{
s_data=net.getState();
ArrayList<Satae_Pojo> statedata = statepojo(s_data);
Iterator<Satae_Pojo> itr = statedata.iterator();
while(itr.hasNext())
{
Satae_Pojo s_pojo=(Satae_Pojo)itr.next();
int id=(Integer)s_pojo.getId();
String name=(String)s_pojo.getState();
state_id.add(id);
state_name.add(name);
}
}
}).start();
}
public ArrayList<Satae_Pojo> statepojo(String result)
{
ArrayList<Satae_Pojo> states=new ArrayList<Satae_Pojo>();
try
{
JSONArray jsonarray = new JSONArray(result);
for(int i=0; i<jsonarray.length(); i++)
{
JSONObject jsonobject=jsonarray.getJSONObject(i);
Satae_Pojo statepojo=new Satae_Pojo();
statepojo.setId(jsonobject.getInt("id"));
statepojo.setState(jsonobject.getString("s_name"));
states.add(statepojo);
}
}
catch(JSONException e)
{
Log.e("Log_tag", "Error Parsing in State....."+e.toString());
Log.e("Log_Answers","Second Error Parsing in State....."+result);
}
return states;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
在上面的源代碼,當我通過「清單」作爲參數傳遞給適配器,它工作正常。 但當動態填充state_name傳遞setOnItemSelectedListener不工作..我做錯了什麼..?
嘗試字符串ID = parent.getAdapter()的getItem(POS)的ToString() –
我有試過...這不起作用。請參閱下面的註釋System.out.println(「This is spinner」);即使這個System.out.println(「這是微調」);不要在其中打印值。 –
你試過的是parent.getItemAtPosition(pos)我寫的是parent.getAdapter()。getItem(pos) –