如何在ListView中更改特定的ImageView

时间:2019-04-10 05:43:20

标签: android android-listview android-arrayadapter

当我从ListView单击它时,我试图更改特定的ImageView(info)值,以使该项目明显是当前项目 我曾尝试处理它,但是自从它用viewHolder制作以来,我找不到自己的解决方案 每当我尝试使用viewHolder.info.setOnClickItem时,我的应用都会崩溃

这是我在MainActivity中的ListView

listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            DataModel dataModel= dataModels.get(position);

            Snackbar.make(view, dataModel.getName()+"\n"+dataModel.getType()+" API: "+dataModel.getVersion_number(), Snackbar.LENGTH_LONG)
                    .setAction("No action", null).show();
            i= Integer.parseInt(dataModel.getVersion_number());
            isPaused = false;

            StartNewSong();

        }
    });

这里是CustomAdapter类

public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{
private MainActivity mainActivity;

private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
    TextView txtName;
    TextView txtType;
    TextView txtVersion;
    ImageView info;


}



public CustomAdapter(ArrayList<DataModel> data, Context context) {
    super(context, R.layout.row_item, data);
    this.dataSet = data;
    this.mContext=context;

}


@Override
public void onClick(View v) {


    int position=(Integer) v.getTag();
    Object object= getItem(position);
    DataModel dataModel=(DataModel)object;




    switch (v.getId())
    {

        case R.id.item_info:
            Snackbar.make(v, "Release date " , Snackbar.LENGTH_LONG)
                    .setAction("No action", null).show();
            break;


    }


}

private int lastPosition = -1;

public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    DataModel dataModel = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag

    final View result;

    if (convertView == null) {


        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.row_item, parent, false);


        viewHolder.txtName = (TextView) convertView.findViewById(R.id.name);

        viewHolder.txtType = (TextView) convertView.findViewById(R.id.type);

        viewHolder.info = (ImageView) convertView.findViewById(R.id.item_info);

        result=convertView;

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

    Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
    result.startAnimation(animation);
    lastPosition = position;


    viewHolder.txtName.setText(dataModel.getName());
    viewHolder.txtType.setText(dataModel.getType());

    viewHolder.info.setOnClickListener(this);
    viewHolder.info.setTag(position);
    // Return the completed view to render on screen
    return convertView;
}


}

0 个答案:

没有答案