lundi 27 juin 2016

Android Custom Listview Checkbox Scroll

Hi everyone I work android custom listview with checkbox example. But I did this example. Clicking the List row, the checkbox selected. But my listview scrolls bottom-top, selected checkbox change. Please help me. Thanks everyone. My code

public class YoklamaListAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
public List<MovieYoklama> yoklamaItems;
boolean selected = false;
private RequestQueue mRequestQueue;
private int selectedPosition = -1;
private int selectedStart = 0;

public YoklamaListAdapter(Activity activity, List<MovieYoklama> movieItems) {
    this.activity = activity;
    this.yoklamaItems = movieItems;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(activity);
    }

    return mRequestQueue;
}

@Override
public int getCount() {
    return yoklamaItems.size();
}

@Override
public Object getItem(int location) {
    return yoklamaItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

public boolean isSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.listturyoklama_row, null);

    ImageView thumbNail = (ImageView) convertView
            .findViewById(R.id.thumbnailYoklama);
    TextView title = (TextView) convertView
            .findViewById(R.id.txtYoklamaTitle);
    TextView kisiID = (TextView) convertView
            .findViewById(R.id.txtYoklamaKID);
    TextView kisiTip = (TextView) convertView
            .findViewById(R.id.txtYoklamaKTip);
    CheckBox chk = (CheckBox) convertView.findViewById(R.id.chckYoklama);

    MovieYoklama m = yoklamaItems.get(position);
    if (selectedStart == 0) {
        chk.setChecked(true);
        m.setCheckbox(true);
    } else {
        if (position == selectedPosition) {
            if (!m.isCheckbox()) {
                chk.setChecked(true);
                m.setCheckbox(true);
            } else {
                chk.setChecked(false);
                m.setCheckbox(false);
            }
        }
    }
    thumbNail.setImageResource(m.getThumbnailUrl());

    // title
    title.setText(m.getTitle());
    // ID
    kisiID.setText(m.getID());
    // Tip
    kisiTip.setText(m.getTip());
    return convertView;
}

public void setCheckBox(int position) {
    selectedPosition = position;
    selectedStart = 1;
    notifyDataSetChanged();
}

}

My MovieYoklama Class

public class MovieYoklama {

private String title, ID, Tip;
int thumbnailUrl;
private boolean checkbox;

public MovieYoklama() {
}

public MovieYoklama(String name, int thumbnailUrl, boolean checkbox,
        String ID, String Tip) {
    this.title = name;
    this.thumbnailUrl = thumbnailUrl;
    this.checkbox = checkbox;
    this.ID = ID;
    this.Tip = Tip;
}

public String getTitle() {
    return title;
}

public void setTitle(String name) {
    this.title = name;
}

public String getID() {
    return ID;
}

public void setID(String ID) {
    this.ID = ID;
}

public String getTip() {
    return Tip;
}

public void setTip(String Tip) {
    this.Tip = Tip;
}

public int getThumbnailUrl() {
    return thumbnailUrl;
}

public void setThumbnailUrl(int thumbnailUrl) {
    this.thumbnailUrl = thumbnailUrl;
}

public boolean isCheckbox() {
    return checkbox;
}

public void setCheckbox(boolean checkbox) {
    this.checkbox = checkbox;
}

}

And Then listview click by Activity clases

private YoklamaListAdapter adapteryoklama;
----------
listViewYoklama.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            adapteryoklama.setCheckBox(position);

        }
    });




Aucun commentaire:

Enregistrer un commentaire