回收android中的listview

时间:2015-03-07 14:44:15

标签: android listview android-listview android-recyclerview

我在抽屉布局中有listview。 Listview包含15个问题。如果用户回答问题,我想更新我的列表视图。我有回收listview的问题。如果用户在第一个问题上回答,我的适配器会更新第一,第八和第五个问题。我知道我的适配器有回收问题,我试图在stackoverflow上找到答案,但没有。有谁知道如何解决这个问题。

这是我的适配器

public class NavigationListAdapter extends ArrayAdapter<NavDrawerItem>{

private LayoutInflater inflater;

public NavigationListAdapter(Context context, int textViewResourceId,
        ArrayList<NavDrawerItem> objects) {
    super(context, textViewResourceId, objects);
    this.inflater = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    NavDrawerItem menuItem = this.getItem(position);
    if (menuItem.getType() == NavigationItem.ITEM_TYPE) {
        //view = getItemView(convertView, parent, menuItem);
    } 
    else if (menuItem.getType() == NavigationPitanja.PITANJA_TYPE){
        view = getPitanjaView(convertView, parent, menuItem);
    }
    else {
        //view = getSectionView(convertView, parent, menuItem);
    }
    return view;
}

public View getPitanjaView(View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem){
    Log.d("get view", convertView +"    " + parentView);
    NavigationPitanja pitanjaSection = (NavigationPitanja) navDrawerItem;
    NavMenuPitanjaHolder navMenuPitanjaHolder;
    if (convertView == null){
        convertView = inflater.inflate(R.layout.drawer_list_pitanja, parentView, false);
        navMenuPitanjaHolder = new NavMenuPitanjaHolder(); 
        navMenuPitanjaHolder.naslovPitanja = (TextView) convertView.findViewById(R.id.naslovPitanja);
        navMenuPitanjaHolder.textPitanja = (TextView) convertView.findViewById(R.id.textPitanja);
        navMenuPitanjaHolder.image = (ImageView) convertView.findViewById(R.id.tocno_netocno_imageView);

        convertView.setTag(navMenuPitanjaHolder);

    } else {
        navMenuPitanjaHolder = (NavMenuPitanjaHolder)convertView.getTag();
    }

    navMenuPitanjaHolder.naslovPitanja.setText(pitanjaSection.getNaslov().toString());
    if (pitanjaSection.getText().toString().length() < 50)
        navMenuPitanjaHolder.textPitanja.setText(pitanjaSection.getText().toString());
    else
        navMenuPitanjaHolder.textPitanja.setText(pitanjaSection.getText().substring(0, 50).toString() + "...");
    if (pitanjaSection.isTocno()){
        navMenuPitanjaHolder.image.setImageResource(R.drawable.ikona_tocno);
    } else if (pitanjaSection.isNetocno()){
        navMenuPitanjaHolder.image.setImageResource(R.drawable.gumb_za_zatvaranje);
    }
    return convertView;

}

@Override
public int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    return this.getItem(position).getType();
}

@Override
public boolean isEnabled(int position) {
    return getItem(position).isEnabled();
}
@Override
public NavDrawerItem getItem(int position) {
    return super.getItem(position);
}

private static class NavMenuPitanjaHolder {
    private TextView naslovPitanja, textPitanja;
    private ImageView image;
}


}

我不想编写getSectionView()和getItemView()函数,couse问题在于getPitanjaView()函数。我的功能我需要回收我的清单。

以下是我想更新listview的方法。

private void postaviPitanje(int z) {
    NavigationListAdapter adapte = new NavigationListAdapter(this, R.layout.drawer_item, mArrayList);
    tocno = true;
    Log.i("Postavljam pitanje ", "Postavljam pitanje");
    for (int i = 0; i < odgovorList.size(); i++) {
        Odgovor odgovor = odgovorList.get(i);
        if (Integer.parseInt(odgovor.getIs_correct()) == 0
                && odgovor.isSelected()
                || Integer.parseInt(odgovor.getIs_correct()) == 1
                && !odgovor.isSelected()) {
            tocno = false;
        }

    }
    int i=z+5;
    if (tocno){
        getAllPitanja.get(z).setTocno(true);
        //Toast.makeText(getActivity().getApplicationContext(), "Tocno ste odgovorili na pitanje cij je id " + (getAllPitanja.get(z).getIdPitanja() + 200), Toast.LENGTH_SHORT).show();
        Log.i("Sljedeci mjenjeam", String.valueOf(200+getAllPitanja.get(z).getIdPitanja()));

        ((NavigationPitanja) adapte.getItem(i)).setTocno(true);
        //adapte.notifyDataSetChanged();
        Log.i("Ovo je adapter koji saljem", ((NavigationPitanja) adapte.getItem(z+5)).getText());
        Log.i("Ovoliki je i", String.valueOf(i));
        //Log.i("Sljedeci se update", String.valueOf(m.get(5+z+1).getId()));

        //adapte.iconiziraj(getAllPitanja.get(z).getIdPitanja());
    }
    else {
        getAllPitanja.get(z).setNetocno(true);
        ((NavigationPitanja) adapte.getItem(i)).setNetocno(true);
        //adapte.notifyDataSetChanged();
        Log.i("Ovoliki je i", String.valueOf(i));
        //Toast.makeText(getActivity().getApplicationContext(), "Niste tocno odgovorili", Toast.LENGTH_SHORT).show();
    }
    adapte.notifyDataSetChanged();

}

这是我的NavigationPitanja课程,以防万一有人不理解其中的方法

public class NavigationPitanja implements NavDrawerItem{


public static final int PITANJA_TYPE = 2;
private int id;
private String naslov, text;
private int icon;
private boolean tocno, netocno;

private NavigationPitanja(){

}

public static NavigationPitanja create(int id, String naslov, String text, Context context){

    NavigationPitanja pitanja = new NavigationPitanja();
    pitanja.setId(id);
    pitanja.setNaslov(naslov);
    pitanja.setText(text);
    return pitanja;

}

public String getNaslov() {
    return naslov;
}

public void setNaslov(String naslov) {
    this.naslov = naslov;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public void setId(int id) {
    this.id = id;
}

@Override
public int getId() {
    // TODO Auto-generated method stub
    return id;
}

@Override
public String getLabel() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int getType() {
    // TODO Auto-generated method stub
    return PITANJA_TYPE;
}

@Override
public boolean isEnabled() {
    // TODO Auto-generated method stub
    return true;
}

public int getIcon() {
    return icon;
}

public void setIcon(int icon) {
    this.icon = icon;
}

public boolean isTocno() {
    return tocno;
}

public void setTocno(boolean tocno) {
    this.tocno = tocno;
}

public boolean isNetocno() {
    return netocno;
}

public void setNetocno(boolean netocno) {
    this.netocno = netocno;
}


}

我不想写所有代码,但大多数人都不会读我的问题。如果有人知道答案,我将不胜感激。 非常感谢你。)

0 个答案:

没有答案