在setContentView之后更改布局会显示空白屏幕

时间:2013-04-13 16:54:20

标签: android android-layout

我有一个在片段中单击listView项时调用的Activity。 我的问题是,即使我更改了我的布局(设置textviews文本等)并调用setContentView,活动屏幕也是空白的。

从Log.i ive到处使用,在logcat中,我可以看到所有数据,并且它正在被正确设置,所以这不是问题。

我的问题是如何制作它以便我所做的更改显示在屏幕上/如何使用新数据刷新布局。

public class SemListViewActivity extends Activity
{
    JSONObject recdJson=new JSONObject();
    JSONArray ar;
    ArrayList<HashMap<String, String>> keyslist;
    TextView semTCredits, semSession, semGPA;
    ListView semList;
    SemListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Log.i("Info Tag Sem","Inside Sem");

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sem_details_listview_layout);

        //LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //View v = inflater.inflate(R.layout.sem_details_listview_layout,null);

        ar = new JSONArray();
        keyslist = new ArrayList<HashMap<String, String>>();
        try
        {
            recdJson = GlobalVars.semJSON;
            Log.i("Info Tag Sem","Got the JSON");
            ar = recdJson.getJSONArray("seminfo");
            Log.i("Info Tag Sem","assigning JSON Array");
            for (int i = 0; i < ar.length(); i++)
            {
                JSONObject jObject = ar.getJSONObject(i);
                HashMap<String, String> map = new HashMap<String, String>();
                Log.i("Info Tag Sem","jObject Init"+i);
                Log.i("Info Tag Sem",jObject.toString());
                Log.i("Info Tag Sem",jObject.getString("subcode"));
                map.put("subcode", jObject.getString("subcode"));
                map.put("sub", jObject.getString("sub"));
                map.put("credits", jObject.getString("credits"));
                map.put("grade", jObject.getString("grade"));


                keyslist.add(map);
            }
            Log.i("Info Tag Sem","Done with Hashmapping");

            semTCredits = (TextView)findViewById(R.id.semTCredits);
            semSession = (TextView)findViewById(R.id.semSession);
            semGPA = (TextView)findViewById(R.id.semGPA);

            Log.i("Info Tag Sem","Set the textviews");

            semTCredits.setText("Total Credits: " + recdJson.getString("credits"));
            semSession.setText("Session: " + recdJson.getString("session"));
            semGPA.setText("Semester GPA: " + recdJson.getString("gpa"));



            semList = (ListView)findViewById(R.id.semlist);

            adapter = new SemListAdapter(this, keyslist);
            semList.setAdapter(adapter);

            Log.i("Info Tag Sem","Put the content");

            setContentView(R.layout.sem_details_listview_layout);

        } catch (JSONException e)
        {
            e.printStackTrace();
            Log.i("Info Tag Sem","Nothing happened ");
        }

    }

}

这是相关的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/semTCredits"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/semSession"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textIsSelectable="true" />

    <TextView
        android:id="@+id/semGPA"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textIsSelectable="true" />

    <ListView
        android:id="@+id/semlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

如果这个问题重复,我不知道如何说出我的问题。

1 个答案:

答案 0 :(得分:0)

您正在拨打setContentView()两次,第二次致电:

        Log.i("Info Tag Sem","Put the content");

        setContentView(R.layout.sem_details_listview_layout);

    } catch (JSONException e)

删除您在上面所做的所有更改......只需将其删除即可。