我已经实现了一个Listener for tab,但是每当我尝试为其他目的实现一个监听器时,对ListView
来说。听众似乎没有回应。
我在主要活动中使用OnTabChangeListener
,此处我还想使用OnClicklistener
或OnItemSelectedListener
作为Listview
。
public void settings(String response) {
JSONArray stationarray;
JSONObject JstationsTemp,stationobj;
yourcitypreflist =getSharedPreferences(YourCityPref,Activity.MODE_PRIVATE);
stationcheck = (CheckBox) findViewById(R.id.station_checkbox);
stationname = (TextView) findViewById(R.id.tv_checkstation);
try
{
String strJson = yourcitypreflist.getString("WeatherInfo","NA");
stationobj = new JSONObject(strJson);
stationarray = stationobj.getJSONArray("W_stationsTemp");
final ListView liststations = (ListView) findViewById(R.id.settinglist);
liststations.setVisibility(View.VISIBLE);
liststations.setCacheColorHint(Color.TRANSPARENT);
for(int i=0; i<stationarray.length();i++)
{
settinglist stationholder = new settinglist();
JstationsTemp = stationarray.getJSONObject(i);
stationholder.cityname = JstationsTemp.getString("Station");
checklistarray.add(stationholder);
}
ListAdapter listadap = new ListAdapter();
liststations.setAdapter(listadap);
listadap.notifyDataSetChanged();
liststations.setOnItemClickListener(new ListView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
System.out.println ("User checked boxes");
System.out.println("city=="+stationname.getText());
showToast("You selected"+stationname.getText());
}
});
}
catch (Exception e) {
}
}
class ListAdapter extends ArrayAdapter<settinglist> implements android.view.View.OnClickListener{
public ListAdapter() {
super(Main.this, R.layout.settingslistview,checklistarray);
}
public View getView(int position, View convertView, ViewGroup parent)
{
ListHolder holder;
if (convertView== null)
{
LayoutInflater inflater= getLayoutInflater();
convertView = inflater.inflate(R.layout.settingslistview, parent, false);
}
else
{
holder = (ListHolder)convertView.getTag();
}
holder = new ListHolder(convertView);
convertView.setTag(holder);
holder.populateFrom(checklistarray.get(position));
return (convertView);
}
@Override
public settinglist getItem(int position) {
return checklistarray.get(position);
}
class ListHolder
{
ListHolder (View vw)
{
stationcheck = (CheckBox) vw.findViewById(R.id.station_checkbox);
stationname = (TextView) vw.findViewById(R.id.tv_checkstation);
}
void populateFrom(settinglist c)
{
stationname.setText(c.cityname);
stationcheck.setTag(c.cityname);
}
}
上面的代码对我有用,但现在我遇到的问题是我无法获取所选项目的名称,它只显示第一个或最后一个项目名称作为选定项目。