我要做的是: - 要在微调器中显示状态,请从数据库加载它并填充微调器。在选择状态,根据国家,城市必须填补另一个旋转器。
我取得的成就是:
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;
}
}
在上面的源代码中,当我将“list”作为参数传递给适配器时,它工作正常。 但是当动态填充state_name时,setOnItemSelectedListener不起作用..我做错了什么..?
答案 0 :(得分:0)
每当您向适配器添加数据(动态)时,您需要调用Adapter.notifyDataSetChanged()
以便视图自行刷新。
在getStates()
方法
runOnUiThread(new Runnable() {
@Override
public void run() {
state_adapter.notifyDataSetChanged();
}
});