访问ListView项

时间:2015-06-05 13:44:42

标签: android listview android-listview

我有一个自定义ListView我填充了AnswerButton

这是listView的行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fbutton="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:gravity="center">

    <com.app.Ui.AnswerButton
        android:layout_centerInParent="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/answerRow"
        android:textColor="#fff"
        fbutton:buttonColor="@color/fbutton_color_asbestos"
        />


</LinearLayout>

列表适配器:

public class AnswerListAdapter extends BaseAdapter {

    Context ctx;
    private List<GameAnswerModel> places = new ArrayList<>();
    private LayoutInflater mInflater;

    public AnswerListAdapter(Context ctx){
       this.ctx = ctx;
       mInflater = (LayoutInflater)this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void add(GameAnswerModel object) {
        this.places.add(object);
    }

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

    @Override
    public GameAnswerModel getItem(int position) {
        return this.places.get(position);
    }

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

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

        AnswerHolder answerHolder = null;

        final GameAnswerModel obj = getItem(position);

        if (convertView == null) {
            answerHolder = new AnswerHolder();
            convertView = mInflater.inflate(R.layout.row_question_answer,  null);
            answerHolder.answerText = (AnswerButton)convertView.findViewById(R.id.answerRow);
            convertView.setTag(answerHolder);
        }

       else
            answerHolder = (AnswerHolder)convertView.getTag();

        answerHolder.answerText.setText(obj.getAnswerText());
        final AnswerHolder finalAnswerHolder = answerHolder;


        return convertView;
    }

    public static class AnswerHolder {
        public AnswerButton answerText;
    }

}

我尝试使用索引访问该列表中的项目。但它没有用。

LinearLayout parent = (LinearLayout) answerListAdapter.getView(indexNum, null, parent);

AnswerButton answer = (AnswerButton) parent.getChildAt(0); //get child in linearLay
answer.setVisibility(View.INVISIBLE); //for test, not working and not giving error

answerListAdapter.notifyDataSetChanged();

如何在listView 中访问该按钮,以便我可以使用它的方法? (这是一个多人游戏。)

AnswerButton:

public class AnswerButton extends FButton {


    public AnswerButton(Context context) {
        super(context);
    }

    public AnswerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AnswerButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setCorrectColor(){
        this.setButtonColor(getResources().getColor(R.color.fbutton_color_green_sea));
    }
    public void setWrongColor(){
        this.setButtonColor(getResources().getColor(R.color.fbutton_color_pomegranate));
    }
   //..
}

2 个答案:

答案 0 :(得分:2)

问题:从不相关的事件更改ListView中行内的视图状态。 我会更改数据集,然后调用adapter.notifyItemChanged(position),如果它的RecyclerView。如果它是ListView然后notifyDataChanged()。在适配器上你需要相应地绑定视图。

答案 1 :(得分:1)

为什么 GameAnswerModel 中没有布尔字段正确,表明此项目是否正确。

然后,如果模型的对象方法 isCorrect()返回true或false,则可以检入 getView() - 设置按钮的正确背景颜色。然后你应该在你的适配器中有一个方法,你将设置正确的答案:

public void setCorrectAnswer(int index) {
    places.get(index).setCorrect();
    notifyDataSetChange();
}

这样它会在你的位置ArrayList中设置正确的答案并重新加载适配器。