我的导航抽屉反应不正确

my navigation drawer doesn't react correctly

我的导航抽屉有问题。当用户单击一个按钮时,我想更改用户单击的元素的图标。有谁能够帮我。 这是我的导航列表适配器

public class NavigationListAdapter extends ArrayAdapter<NavDrawerItem>{

private LayoutInflater inflater;
private NavMenuPitanjaHolder navMenuPitanjaHolder;

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(position, convertView, parent, menuItem);
    } 
    else if (menuItem.getType() == NavigationPitanja.PITANJA_TYPE){
        view = getPitanjaView(position, convertView, parent, menuItem);
    }
    else {
        view = getSectionView(position, convertView, parent, menuItem);
    }
    return view;
}

public View getItemView(int position, View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavigationItem menuItem = (NavigationItem) navDrawerItem;
    NavMenuItemHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.drawer_list_item, parentView,
                false);
        TextView labelView = (TextView) convertView
                .findViewById(R.id.navmenuitem_label);
        ImageView i = (ImageView) convertView
                .findViewById(R.id.navmenuitem_icon);

        navMenuItemHolder = new NavMenuItemHolder();
        navMenuItemHolder.labelView = labelView;
        navMenuItemHolder.image = i;

        convertView.setTag(navMenuItemHolder);
    }

    if (navMenuItemHolder == null) {
        navMenuItemHolder = (NavMenuItemHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuItem.getLabel().toString());
    navMenuItemHolder.image.setImageResource(menuItem.getIcon());

    return convertView;
}

public View getSectionView(int position, View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavigationSection menuSection = (NavigationSection) navDrawerItem;
    NavMenuSectionHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.darwer_list_section,
                parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById(R.id.navmenusection_label);

        navMenuItemHolder = new NavMenuSectionHolder();
        navMenuItemHolder.labelView = labelView;
        convertView.setTag(navMenuItemHolder);
    }

    if (navMenuItemHolder == null) {
        navMenuItemHolder = (NavMenuSectionHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuSection.getLabel().toString());

    return convertView;
}

public View getPitanjaView(int position, View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem){

    NavigationPitanja pitanjaSection = (NavigationPitanja) navDrawerItem;
    navMenuPitanjaHolder = null;
    if (convertView == null){
        convertView = inflater.inflate(R.layout.drawer_list_pitanja, parentView, false);
        TextView naslovView = (TextView) convertView.findViewById(R.id.naslovPitanja);
        TextView textView = (TextView) convertView.findViewById(R.id.textPitanja);
        ImageView i = (ImageView) convertView.findViewById(R.id.tocno_netocno_imageView);

        navMenuPitanjaHolder = new NavMenuPitanjaHolder();
        navMenuPitanjaHolder.naslovPitanja = naslovView;
        navMenuPitanjaHolder.textPitanja = textView;
        navMenuPitanjaHolder.image = i;
        convertView.setTag(navMenuPitanjaHolder);

    }

    if (navMenuPitanjaHolder == null) {
        navMenuPitanjaHolder = (NavMenuPitanjaHolder) convertView.getTag();
    }

    navMenuPitanjaHolder.naslovPitanja.setText(pitanjaSection.getNaslov().toString());
    if (pitanjaSection.getText().toString().length() < 20)
        navMenuPitanjaHolder.textPitanja.setText(pitanjaSection.getText().toString());
    else
        navMenuPitanjaHolder.textPitanja.setText(pitanjaSection.getText().substring(0, 20).toString() + "...");

    return convertView;

}

public void iconized(Pitanja item, int id){
    if (item.getIdPitanja() == id){
        navMenuPitanjaHolder.image.setImageResource(R.drawable.correct);
    }
}

@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();
}


private static class NavMenuItemHolder {
    private TextView labelView;
    private ImageView image;
}

private static class NavMenuSectionHolder {
    private TextView labelView;
}

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


}

这是我的主要内容activity

@SuppressLint("UseSparseArrays")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    previous.setOnClickListener(this);
    informacije.setOnClickListener(this);
    odgovori.setOnClickListener(this);
    next.setOnClickListener(this);
    //uputa = (RelativeLayout) findViewById(R.id.relativeLayoutUputa1);

    getAllImages = db.getAllPitanjaImages();
    odgBrojPitanja = new HashMap<Integer, List<Integer>>();

    updateDisplay(broj);

}

@Override
protected void onNavItemSelected(int id) {

    if ( id > 200){
        Toast.makeText(getApplicationContext(), "kliknuo si na pitanje" + id + " toliki je id", Toast.LENGTH_SHORT).show();
        updateDisplay(id-200-1);

    } else {
        Toast.makeText(getApplicationContext(), "kliknuo si na izbornik" + id, Toast.LENGTH_SHORT).show();
    }

}

@Override
protected NavDrawerActivityConfiguration getNavDrawerConfiguration() {

    intent = getIntent();
    getAllPitanja = db
            .getAllPitanja(intent.getStringExtra("id_kategorije"));

    m = new ArrayList<NavDrawerItem>();
    m.add(NavigationItem.create(100, "Profil", R.drawable.ikona_profil_bez_okvira_01_01, this));
    m.add(NavigationItem.create(200, "Teme", R.drawable.ikona_kategorije_bez_okvira_01_01,this));
    m.add(NavigationItem.create(300, "Ispiti", R.drawable.ikona_ispit_bez_okvira_01, this));
    m.add(NavigationItem.create(400, "Znakovi", R.drawable.ikona_znakovi_bez_okvira_01, this));
    m.add(new NavigationSection(200, "PITANJA"));
    for (int i=0;i<getAllPitanja.size();i++){
        m.add(NavigationPitanja.create((200+i+1), "Pitanje " + (i+1), getAllPitanja.get(i).getTextPitanja(), this));
    }


    NavDrawerActivityConfiguration navDrawerActivityCOnfiguration = new NavDrawerActivityConfiguration();
    navDrawerActivityCOnfiguration.setLayout(R.layout.activity_pitanje);
    navDrawerActivityCOnfiguration.setDrawerLayoutId(R.id.drawer_layout_pitanja);
    navDrawerActivityCOnfiguration.setLeftDrawer(R.id.listaDesnoPitanja);
    navDrawerActivityCOnfiguration.setNavigationItem(m);
    adapte = new NavigationListAdapter(this, R.layout.drawer_list_item, m);
    navDrawerActivityCOnfiguration.setBaseAdapter(adapte);
    return navDrawerActivityCOnfiguration;

}

@SuppressLint("SdCardPath")
public void updateDisplay(int z) {

    odgovorList = db.getAllOdgovore(Integer.toString(getAllPitanja.get(z)
            .getIdPitanja()));
    adapter = new PitanjaAdapter(this,
            R.layout.pitanja_entry, odgovorList);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.previousButton:
            broj--;
            if (broj < 0) {
                broj = getAllPitanja.size() - 1;
            }
            updateDisplay(broj);
        break;
    case R.id.odgovoriButton:
        postaviPitanje(broj);
        break;
    case R.id.nextButton:           
            broj++;
            if (broj > (getAllPitanja.size() - 1)) {
                broj = 0;
            }
            updateDisplay(broj);
        break;

    }
}

private void postaviPitanje(int z) {

    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;
        }

    }
    if (tocno){
        getAllPitanja.get(z).setTocno(true);
        Toast.makeText(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()));
        adapte.iconized(getAllPitanja.get(z), getAllPitanja.get(z).getIdPitanja());
        Log.i("Sljedeci item sam iconizirao", getAllPitanja.get(z).getTextPitanja());
    }
    else {
        getAllPitanja.get(z).setNetocno(true);
        Toast.makeText(getApplicationContext(), "Niste tocno odgovorili", Toast.LENGTH_SHORT).show();
    }

}

}

有人知道为什么我的适配器中的 public 方法每隔七张图片就会更改抽屉布局中的图片。如果我有 37 个项目。每七个项目图标更改一次。任何人都有解决方案。你能帮帮我吗

问题是您在适配器中使用了 navMenuPitanjaHolder 的单个实例,这通常是一种不好的做法。您尝试实现的 ViewHolder 模式的想法是为每个不同的 convertView 提供一个 ViewHolder class 的实例(在您的情况下 NavMenuPitanjaHolder),但您在 getPitanjaView().

通过 iconized() 设置图像后,您就是在为 navMenuPitanjaHolder 设置图像,因此所有使用它的视图都将获得该图像。它是每个 X 项目,因为 Android 正在尝试尽可能多地重用视图。简而言之 - 这种方法在概念上是错误的,你应该删除它。

更好的方法是在 NavigationPitanja class 中添加一个变量,它将保存答案正确或错误的信息。然后在 getPitanjaView() 你会得到类似的东西:

if (pitanjaSection.isAnswerCorrect()) {
    navMenuPitanjaHolder.image.setImageDrawable(R.drawable.correct);
} else {
    navMenuPitanjaHolder.image.setImageDrawable(R.drawable.false);
}

希望这能为您指明解决问题的正确方向!