【问题标题】:Set new TextView to fragment in AsyncTask在 AsyncTask 中将新的 TextView 设置为片段
【发布时间】:2016-08-16 04:41:38
【问题描述】:

我想在我的片段中添加一个新的TextView。这是我在 asynctask backgroundjob 类中的代码:

protected void onPostExecute(String json) {

    try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray jsonArray = jsonObject.getJSONArray("server_response");
        JSONObject JO = jsonArray.getJSONObject(0);
        String code = JO.getString("code");
        String message = JO.getString("message");

        if (code.equals("true")) {
            /**********************************/

            LayoutInflater inflater = activity.getLayoutInflater();
            View mContainer = inflater.inflate(R.layout.fragment_home, null);
            LinearLayout linearLayout = (LinearLayout)mContainer.findViewById(R.id.mylinearLayout);

            TextView text = new TextView(linearLayout.getContext());
            text.setText("TEST");
            text.setTextSize(30);

            Log.d("View", "Start");
            try {
                linearLayout.addView(text);
            } catch (Exception e) {
                e.printStackTrace();
            }

这是我的片段类:

public class Home extends Fragment {

    public Home() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        TopicBackgroundTask backgroundTask = new TopicBackgroundTask(getActivity());
        backgroundTask.execute("yes");

        // Inflate the layout for this

        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}

注意Home 片段和TopicBackgroundTask 是两个不同的

这对我不起作用。谁能告诉我为什么?

【问题讨论】:

    标签: java android android-layout android-fragments android-asynctask


    【解决方案1】:

    您无需在onPostExecute() 中再次膨胀布局

    您需要重新使用onCreateView() 中给出的布局, 所以你必须创建一个全局变量视图, 设置在onCreateView()而不是直接返回。

    点赞-
    myView = inflater.inflate(R.layout.fragment_home, container, false);

    onPostExecute()

    LinearLayout linearLayout = (LinearLayout)mContainer.findViewById(R.id.mylinearLayout);
    
        TextView text = new TextView(linearLayout.getContext());
                    text.setText("TEST");
                    text.setTextSize(30);
    
                    Log.d("View", "Start");
                    try {
                        linearLayout.addView(text);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    

    【讨论】:

    • Home 片段和TopicBackgroundTask 是两个不同的 + 你不能从onCreateView() 调用View 我尝试添加getActivity().setContentView(mContainer); 并且它工作但它显示在片段之外
    【解决方案2】:

    找到解决方案,您必须将ViewLayout To ViewGroup

    protected void onPostExecute(String json) {
    
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("server_response");
            JSONObject JO = jsonArray.getJSONObject(0);
            String code = JO.getString("code");
            String message = JO.getString("message");
    
            if (code.equals("true")) {
                /**********************************/
    
                LayoutInflater inflater = activity.getLayoutInflater();
                View mContainer = inflater.inflate(R.layout.fragment_home, null);
               // LinearLayout linearLayout = (LinearLayout)mContainer.findViewById(R.id.mylinearLayout);
    
                TextView text = new TextView(linearLayout.getContext());
                text.setText("TEST");
                text.setTextSize(30);
    
                Log.d("View", "Start");
                try {
                    ((ViewGroup)activity.findViewById(R.id.mylinearLayout)).addView(text);
                } catch (Exception e) {
                    e.printStackTrace();
                }
    

    【讨论】:

      猜你喜欢
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多