我正在用戶點擊列表視圖中的項目時,它會顯示項目內部的公交時間。Listview顯示重複的項目點擊時
我不確定哪裏出了問題。當它點擊時,它會在列表視圖中顯示一個項目。但是當我滾動列表視圖時,它將顯示與前一項目相同的總線時間。我從stackoverflow讀了幾個解決方案,但是我沒有解決這個問題。
我希望能得到一些建議來解決這個問題。
The text will appear when being clicked
public class Adapter extends ArrayAdapter
{
ArrayList<BusRoute>busList ;
private int resource;
private LayoutInflater inflater;
public Adapter(Context context, int resource, ArrayList objects) {
super(context, resource, objects);
busList=objects;
this.resource=resource;
inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
class ViewHolder {
TextView buscode;
TextView businfo;
TextView bookmark;
TextView firstBusTime;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null)
{
convertView=inflater.inflate(R.layout.bus_route_activity_listview,null);
holder = new ViewHolder();
holder.buscode = (TextView) convertView.findViewById(R.id.buscode);
holder.businfo = (TextView) convertView.findViewById(R.id.businfo);
holder.bookmark = (TextView)convertView.findViewById(R.id.bookmark);
holder.firstBusTime = (TextView)convertView.findViewById(R.id.firstBusTime);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
final BusRoute map = busList.get(position);
holder.buscode.setText(map.getBusStopCode());
holder.businfo.setText(map.getRoad() + "\n" + map.getDescription());
//when user select on the particular item in the listview
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.firstBusTime.setText("Yo");
}
});
return convertView;
}
}
BusRoute類
public class BusRoute {
private String BusStopCode;
private String Road;
private String Description;
public String getBusStopCode() {
return BusStopCode;
}
public void setBusStopCode(String busStopCode) {
BusStopCode = busStopCode;
}
public String getRoad() {
return Road;
}
public void setRoad(String road) {
Road = road;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
適配器正被調用
public class BusRouteTask1 extends AsyncTask<String, String, ArrayList<BusRoute>> {
@Override
protected ArrayList<BusRoute> doInBackground(String... params) {
}
@Override
protected void onPostExecute(ArrayList<BusRoute> result) {
super.onPostExecute(result);
Adapter a = new Adapter(getApplicationContext(), R.layout.activity_bus_route, result);
lv.setAdapter(a);
請發佈您的BusRoute類。 –
'最終BusRoute map = busList.get(position); holder.buscode.setText(map.getBusStopCode()); ......'在你的'if(convertView == null)'部分添加這個。 –