以下是适配器
public class AdapterCases extends BaseAdapter{
List<Case> lstCases;
Context mContext;
ViewHolder holder;
LawyersInfo user_info;
public AdapterCases(Context context, int resource,
int textViewResourceId, List<Case> cases, LawyersInfo user_info) {
lstCases = cases;
mContext = context;
holder = new ViewHolder();
this.user_info = user_info;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.row_grid_cli_law, parent,false);
holder.tvPName=(TextView) convertView.findViewById(R.id.tvPersonName);
holder.tvCName=(TextView) convertView.findViewById(R.id.tvCompanyName);
holder.tvCaNo=(TextView) convertView.findViewById(R.id.tvCaseNo);
holder.tvCaType=(TextView) convertView.findViewById(R.id.tvCaseType);
holder.tvCaDate=(TextView) convertView.findViewById(R.id.tvCaseDate);
holder.btnViewDetail=(Button) convertView.findViewById(R.id.btnViewDetail);
holder.ivPic = (ImageView) convertView.findViewById(R.id.ivPic);
holder.llRow = (LinearLayout) convertView.findViewById(R.id.llRow);
holder.tvPName.setVisibility(View.GONE);
holder.ivPic.setVisibility(View.GONE);
holder.tvCaNo.setVisibility(View.VISIBLE);
holder.tvCaType.setVisibility(View.VISIBLE);
holder.tvCaDate.setVisibility(View.VISIBLE);
holder.btnViewDetail.setVisibility(View.VISIBLE);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
final Case p=lstCases.get(position);
/*String name= "";
if((null!=p.first_name)&&(!p.first_name.equalsIgnoreCase("null"))){
name=p.first_name;
}
if((null!=p.last_name)&&(p.last_name.equalsIgnoreCase("null"))){
name = name+ " "+p.last_name;
}*/
holder.tvCaNo.setText(p.case_ref_number);
holder.tvCaType.setText(p.case_title);
holder.tvCaDate.setText(p.case_status);
holder.tvCName.setText(p.case_cost);
holder.btnViewDetail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext, CaseDetails.class);
i.putExtra("case", p);
i.putExtra("info", user_info);
mContext.startActivity(i);
}
});
holder.llRow.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
.setTitle(p.case_title)
.setMessage("Are you sure you want to delete the case permanantly?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
lstCases.remove(position);
// Toast.makeText(mContext, lstCases.size(), Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
notifyDataSetInvalidated();
//lstCases.remove(position);
//notifyDataSetChanged();
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
return false;
}
});
return convertView;
}
private class ViewHolder{
public LinearLayout llRow;
public ImageView ivPic;
public Button btnViewDetail;
public TextView tvCaDate;
public TextView tvCaType;
public TextView tvCaNo;
public TextView tvCName;
public TextView tvPName;
}
@Override
public int getCount() {
return lstCases.size();
}
@Override
public Object getItem(int position) {
return lstCases.get(position);
}
@Override
public long getItemId(int position) {
return lstCases.get(position).hashCode();
}
}
以下是我的gridView
<GridView
android:id="@+id/gvClientsLawyers"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:numColumns="3">
</GridView>
问题在于,当我向下滚动以查看更多项目时,第一行中的项目可以在最后一行中看到,有时也会以相同的顺序显示,有时会被洗牌。当我滚动时,我可以看到一个项目的文本随着我向下滚动而改变,然后当我向上滚动时再次改变。我也应用了持有人模式。仍然没有运气。
答案 0 :(得分:0)
您不希望自己的ViewHolder
成为全局变量。从构造函数中删除它及其实例化。相反,您只希望它成为getView
方法的局部变量。例如:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null) {
holder = new ViewHolder();
//Continue with handling view creation
} else {
holder = (ViewHolder) convertView.getTag();
}
}