Android列表视图编辑所有视图外观

时间:2017-01-10 14:02:12

标签: android listview row android-arrayadapter

首先我要说的是我没有在这里使用视图。我只有5个视图,我通过两次选择点击交换它们。我在网上查看了几个问题,其中大部分问题涉及Recycler和getChildAt等,但它对我来说并不起作用。

我理解适配器是如何工作的,所以我想我必须将我的背景颜色传递到我的适配器中。只是快速了解我想在这里做什么。用户可以随机播放列表项,它们必须与其旁边的表对应。一旦用户认为答案正确,他们就会点击提交按钮。此时,背景颜色应更改为绿色或红色,以确保正确和不正确。

然而,当我想访问所有视图时,我似乎无法掌握它。

获取适配器的数据源

    private ArrayList<Triplet<String, Integer, Integer>> testArray = new ArrayList<>();

public ArrayList<Triplet<String, Integer, Integer>> getDefinitionWordsTriplet(){
    testArray.add(Triplet.with("Trojan", 0, Color.WHITE));
    testArray.add(Triplet.with("Virus", 1, Color.WHITE));
    testArray.add(Triplet.with("Spyware", 2, Color.WHITE));
    testArray.add(Triplet.with("Ransomware", 3, Color.WHITE));
    testArray.add(Triplet.with("Leakware", 4, Color.WHITE));
    return testArray;
}

所以我使用API​​来使用Triplet,原来这是一对但我决定包含颜色Int。

填充适配器

 //TESTING
private ArrayList<Triplet<String, Integer, Integer>> testTripletArray;
private DefintionListViewAdapterTriplet leftTripletAdapter;

private void EstablishTableLists(){
    leftDefinitionLV = (ListView) findViewById(R.id.definitionMainGameLeftLV);
    ListView rightDefinitionLV = (ListView) findViewById(R.id.definitionMainGameRightLV);

    DefinitionWordList wordAndDefinitionList = new DefinitionWordList();
    definitionWordsList = wordAndDefinitionList.getDefinitionWords();
    definitionExplanationsList = wordAndDefinitionList.getDefinitionExplanations();
    tableLength = definitionWordsList.size();

    //TESTING
    testTripletArray = wordAndDefinitionList.getDefinitionWordsTriplet();

    DefintionListViewAdapter rightAdapter = new DefintionListViewAdapter
            (DefinitionGameMainActivity.this, definitionExplanationsList);

    //TESTING
    leftTripletAdapter = new DefintionListViewAdapterTriplet(DefinitionGameMainActivity.this, testTripletArray);

    //leftDefinitionLV.setAdapter(leftAdapter);
    rightDefinitionLV.setAdapter(rightAdapter);

    //TESTING
    leftDefinitionLV.setAdapter(leftTripletAdapter);

    leftDefinitionLV.setOnItemClickListener(leftSideItemListener);

}

所以这是标准的适配器和listView的建立。我将我的数据传递给适配器,然后给listView我的适配器。

适配器

public class DefintionListViewAdapterTriplet extends ArrayAdapter<Triplet<String, Integer, Integer>> {

private Context mContext;
private ArrayList<Triplet<String, Integer, Integer>> mInput;

public DefintionListViewAdapterTriplet(Context c, ArrayList<Triplet<String, Integer, Integer>> input){
    super(c, -1, input);
    mContext = c;
    mInput = input;
    Log.i("Test: ", mInput.get(0).getValue0());
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.definition_table_cell, parent, false);

    TextView textInputTV = (TextView) rowView.findViewById(R.id.definitionCellTV);
    LinearLayout borderColour = (LinearLayout) rowView.findViewById(R.id.definitionBackgroundCell);
    borderColour.setBackgroundColor(mInput.get(position).getValue2());
    textInputTV.setText(mInput.get(position).getValue0());
/*        if(parent.getId() == R.id.definitionTutorialRightTableLV || parent.getId() == R.id.definitionMainGameRightLV){
        borderColour.setBackgroundResource(R.drawable.definition_tutorial_box_border_grey);
    }else{

    }*/
    return rowView;
}

再次没有特别喜欢适配器。我已经注释了边框背景,因为我正在测试颜色分配。因此,当我将背景颜色放在Triplet的第3个参数中时,我认为我能够在主要活动中修改它。

尝试修改每个视图并通知

    private void checkAnswerValues(){
    for(int i = 0; i < tableLength; i++){
        //int leftCell = definitionWordsList.get(i).second;

        //TESTING
        int leftCell = testTripletArray.get(i).getValue1();
        int rightCell = definitionExplanationsList.get(i).second;
        //TODO Doesn't work, doesn;t update the colours. No crashing but unsure
        //TODO Don't think it's getting the right information, need to think how to access a view.
        if(leftCell == rightCell){
            Log.i(LOG_TAG, "Match");
            correctAnswerCount++;
            //TODO CHange border colours in both tables to green
            testTripletArray.get(i).setAt2(Color.GREEN);
        }else{
            Log.i(LOG_TAG, "No Match");
            //TODO Change border colours to red
            testTripletArray.get(i).setAt2(Color.RED);
        }
    }

    leftTripletAdapter.notifyDataSetChanged();

这是我希望用户按下检查答案按钮的阶段,它会遍历所有定义和单词以查看它们是否匹配。它基于一个索引设置为Triple的第二个参数(如果从0开始计数,则为第一个)。

我终于尝试使用setAt2更改三元组的背景颜色int,然后选择颜色。在for循环之后,我通知适配器信息已更改。我当时希望适配器能够更新&#34; getView&#34;并应用color参数中找到的新参数。不幸的是没有更新。

这里的任何建议都会很棒,因为我已经测试了很多不同的方法,但我已经做得很短。

感谢阅读,我知道这是一个很长的问题。

1 个答案:

答案 0 :(得分:1)

notifyDataSetChanged()无法为您工作的主要原因之一是,您的适配器会丢失对您的列表的引用。你有没有在初始化时调用testTripletArray = new ...异常?请始终testTripletArray.clear()然后testTripletArray.addall(theNewObjects)