我正在努力点击用户点击列表视图中的项目,它将在项目中显示公交时间。
我不确定出了什么问题。单击时,它会在列表视图中显示一个项目。但是当我滚动列表视图时,它将显示与前一项相同的总线时间。我从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 Class
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);
答案 0 :(得分:0)
try like this
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 Listener(holder.firstBusTime,map));
return convertView;
}
class Listener implements View.OnClickListener
{
BusRoute Data;
TextView timeView;
Listener(TextView timeView,BusRoute Data)
{
this.Data = Data;
this.timeView = timeView;
}
@Override
public void onClick(View view) {
// Set your test here use dataobject like
// timeView.firstBusTime.setText(data.getBustTIme());
timeView.firstBusTime.setText("Yo");
}
}
答案 1 :(得分:0)
按以下方式更改您的getView()方法
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
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);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final BusRoute map = busList.get(position);
holder.buscode.setText(""+map.getBusStopCode());
holder.businfo.setText(map.getRoad() + "\n" + map.getDescription());
holder.firstBusTime.setText(map.getTime());
//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");
map.setTime("Yo");
notifyDataSetChanged();
}
});
return convertView;
}
问题是您在textview.settext()
内调用onclick
,而应该更新模型项内容值。
在您的情况下,您可以在BusRoute
班级中设置新字段,例如String time;
您将创建getters
和setters
。然后像上面一样设置textview值。
希望它能奏效。