代码之家  ›  专栏  ›  技术社区  ›  Lectem

Android上屏幕旋转后TableRow文本视图内容错误

  •  0
  • Lectem  · 技术社区  · 10 年前

    我在一个片段中有一个TableLayout,一旦通过以下代码创建了片段,我就会生成行(为了测试而简化):

    public void displayChar(String ClassID) {
        Character charac = new Character(Integer.parseInt(ClassID), getActivity());
        TableLayout stanceListTable = (TableLayout) getView().findViewById(R.id.charStanceList);
        stanceListTable.removeAllViews();
        i=0;
        for (Character.StanceCondition stanceCondition : stanceList) {
            TableRow row=createTableRow(stanceCondition);
            ((TextView)row.findViewById(R.id.stances)).setText(Integer.toString(i));
            Log.i("RowContent",((TextView)row.findViewById(R.id.stances)).getText().toString());
            stanceListTable.addView(row);
            i++;
        }
    }
    

    在我的活动(仅包含此片段)的第一次创建时 我有很好的输出: enter image description here 但旋转后: enter image description here

    但最糟糕的是日志是正确的!

    之前

    08-02 12:30:37.446    5697-5697/com.lectem.gecharacters I/RowContent﹕ 0
    08-02 12:30:37.449    5697-5697/com.lectem.gecharacters I/RowContent﹕ 1
    08-02 12:30:37.466    5697-5697/com.lectem.gecharacters I/RowContent﹕ 2
    08-02 12:30:37.469    5697-5697/com.lectem.gecharacters I/RowContent﹕ 3
    

    之后

    08-02 12:30:45.653    5697-5697/com.lectem.gecharacters I/RowContent﹕ 0
    08-02 12:30:45.656    5697-5697/com.lectem.gecharacters I/RowContent﹕ 1
    08-02 12:30:45.660    5697-5697/com.lectem.gecharacters I/RowContent﹕ 2
    08-02 12:30:45.662    5697-5697/com.lectem.gecharacters I/RowContent﹕ 3
    

    createTableRow方法:

    public TableRow createTableRow(Character.StanceCondition stanceCondition) {
        TableRow row =(TableRow) ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.stance_list_row,null);
        TextView hands=(TextView)row.findViewById(R.id.stanceHands);
        hands.setText(stanceCondition.RHand + stanceCondition.LHand);
    
        TextView stancesView =(TextView)row.findViewById(R.id.stances);
    
        MovementMethod movementMethod = stancesView.getMovementMethod();
        if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod))
        {
            stancesView.setMovementMethod(LinkMovementMethod.getInstance());
        }
        Log.i("row","creating row");
        SpannableString finalss=new SpannableString("");
        /*Should be creating my spannable string here with links, but I simplified the code and changed the text after creation of the row*/
        stancesView.setText(finalss);
        return row;
    }
    

    为什么它会显示错误的文本而getText方法返回正确的内容。。。第一个TextView具有良好的文本。 这真的让我发疯了,问题是什么?

    编辑: 这是我的活动的onCreate函数:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_char_details);
        ClassID = getIntent().getExtras().getString("ClassID");
        mDetailsFragment = (CharDetailsFragment) getSupportFragmentManager().findFragmentById(
                R.id.charDetails);
        mDetailsFragment.displayChar(ClassID);
    
    }
    

    对于我的片段:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_char_details, container, false);
    
    }
    

    编辑2:

    问题似乎来自

        MovementMethod movementMethod = stancesView.getMovementMethod();
        if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod))
        {
            stancesView.setMovementMethod(LinkMovementMethod.getInstance());
        }
    

    你知道我该怎么修吗?

    3 回复  |  直到 10 年前
        1
  •  0
  •   Raviindra    10 年前

    尝试 android:冻结文本=“true” 在您的 文本视图 在xml中,这可能会解决您的问题。

        2
  •  0
  •   Lectem    10 年前

    所以我找到了一个“答案”来解决我的问题,尽管如果有人能准确解释为什么它会起作用,我会接受他的答案。

    我只是通过片段的onStart方法调用displayChar方法。似乎在调用onClose时,LinkMovementMethod会出错。因此,使用OnStart重新生成TextViews似乎有效。

        3
  •  0
  •   Blaz    10 年前

    问题是 TextView's 在每一行中都具有相同的id。因此,当保存每一行的已保存实例状态时,它将覆盖前一行中的已保存状态,并在最后保存最后一行的状态。然后,当状态恢复时,它将用id填充所有视图 R.id.stances 最后一行的值。