gpt4 book ai didi

android - 在我调试的自定义适配器中未调用 Getview,我发现位置返回为 -1

转载 作者:行者123 更新时间:2023-11-30 01:27:55 25 4
gpt4 key购买 nike

下面是我的自定义适配器的代码。在我调试的自定义适配器中没有调用 Getview,我发现位置在方法中返回为 -1

@Override
public Object getItem(int position) {

return result.get(position);
}

ArrayList <searchObjects> result ;
String [] title;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(searchresults mainActivity,
ArrayList<searchObjects> prgmNameList, int[] prgmImages) {
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;

inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我像这样调用自定义适配器

 lv = (ListView) findViewById(R.id.listView);

cst= new CustomAdapter(this, objects, prgmImages);
lv.setAdapter(cst);

我的完整自定义适配器:

public class CustomAdapter extends BaseAdapter {
ArrayList <searchObjects> result ;
String [] title;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(searchresults mainActivity, ArrayList<searchObjects> prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;

inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.d("Custom get Item1", String.valueOf(result.size()));
return result.size();
//return 2;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
Log.d("Custom get Item", String.valueOf(result.size()));
return result.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public class Holder
{
TextView tv;
TextView tv1;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

Log.d("My custome adapter 1", "in view");
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.searchlist, null);
holder.tv1=(TextView) rowView.findViewById(R.id.txtvwsrchlistitemname);
holder.img=(ImageView) rowView.findViewById(R.id.icon);
holder.img.setImageResource(imageId[0]);


holder.tv1.setText(result.get(position).tmpSearchtitle);

return rowView;
}

}

主要 Activity 代码

AsyncCallWS task = new AsyncCallWS();
task.execute();


lv = (ListView) findViewById(R.id.listView);

cst= new CustomAdapter(this, objects, prgmImages);
lv.setAdapter(cst);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = lv.getItemAtPosition(position);
Log.d(" Search results", " search results link cliked");
Intent browser = new Intent(searchresults.this, locationdetails.class);
startActivity(browser);

}
});

最佳答案

public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;

LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout. searchlist, null);
holder = new Holder();
holder.txtDesc = (TextView) convertView.findViewById(R.id. txtvwsrchlistitemname);

holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}

searchObjects rowItem = (searchObjects) getItem(position);

holder.txtDesc.setText(rowItem.getDesc());

holder.imageView.setImageResource(rowItem.getImageId());

return convertView;
}

/** 你的 Pogo 类 **/

public class searchObjects {
private int imageId;

private String desc;

public searchObjects(int imageId,String desc) {
this.imageId = imageId;

this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}

}

关于android - 在我调试的自定义适配器中未调用 Getview,我发现位置返回为 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com