adapterview和addview的问题

时间:2013-05-10 21:38:47

标签: android android-arrayadapter

所以我正在尝试构建和Android应用程序,它从用户输入中获取代码并将其放入listview的自定义布局中。

这是我对列表中每个元素的布局的xml(以下称为game_row.xml)

  <RelativeLayout 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/dateout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/team1out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/dateout"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/team2out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/team1out"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/score1out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/team2out"
        android:layout_alignParentRight="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/score2out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/team2out"
        android:layout_alignParentRight="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <View
    android:layout_width="fill_parent"
    android:layout_below="@+id/team2out"
    android:layout_height="1dp"
    android:background="@android:color/white"/>

   </RelativeLayout>

这是我班级的java代码:

public class KeepScoreHomeScreen extends Activity {

    ArrayList<Game> Game_data = new ArrayList<Game>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);

        if (savedInstanceState != null) {
            System.out.println("this shouldnt happen");
            ArrayList<Game> inp = (ArrayList<Game>) savedInstanceState.get("games");
            System.out.println("game made");
            Game_data.addAll(inp);

        }
        // System.out.println("helo");
        // Game_data.add(new Game(1,2,3,4,3, "t","T"));

        GameAdapter adapter = new GameAdapter(this, R.layout.game_row, Game_data);

        // This is used to update the maximum number of days in a month

        Bundle extras = getIntent().getExtras();

        ListView gameList = (ListView) findViewById(R.id.main_list_view);
        gameList.setAdapter(adapter);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putParcelableArrayList("games", Game_data);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_newEntry:
            toNewEntry(this.findViewById(R.layout.home_screen));
            break;
        case R.id.action_help:
            buildHelp(this.findViewById(R.layout.home_screen));
            break;
        case R.id.action_settings:
            buildSettings(this.findViewById(R.layout.home_screen));
        default:
            return super.onOptionsItemSelected(item);
        }
        return false;
    }

    public void toNewEntry(View view) {
        Intent intent = new Intent(this, KeepScoreActivity.class);
        startActivityForResult(intent, 1);
    }

    protected void onActivityResult(int r, int j, Intent intent) {
        // System.out.println("hedslo");
        Bundle bundle = intent.getBundleExtra("myfirstintent");
        int sc1 = bundle.getInt("score1");
        int sc2 = bundle.getInt("score2");
        int ye = bundle.getInt("year");
        int mo = bundle.getInt("month");
        int da = bundle.getInt("day");
        String te1 = bundle.getString("team1");
        String te2 = bundle.getString("team2");
        Game g = new Game(sc1, sc2, ye, mo, da, te1, te2);
        Game_data.add(g);
        GameAdapter adapter = new GameAdapter(this, R.layout.game_row, Game_data);
        ListView gameList = (ListView) findViewById(R.id.main_list_view);
        gameList.setAdapter(adapter);
    }

    public void buildHelp(View view) {
        /***
         * this code to build an alert dialog was taken from
         * http://stackoverflow
         * .com/questions/2795300/how-to-implement-a-custom-alertdialog-view
         * 
         * I used this code because it allowed me to insert a simple xml file as
         * my alertdialog
         * 
         */

        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.help_layout, (ViewGroup) getCurrentFocus());
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        builder.show();
    }

    public void buildSettings(View view) {
        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.settings_layout, (ViewGroup) getCurrentFocus());
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialoglayout);
        builder.show();

    }
}

我遇到的问题是,每次我设法创建game_row输出然后尝试给设置栏充气我都会遇到错误

05-10 14:27:56.153: E/AndroidRuntime(1569): FATAL EXCEPTION: main
05-10 14:27:56.153: E/AndroidRuntime(1569): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.widget.AdapterView.addView(AdapterView.java:477)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.LayoutInflater.inflate(LayoutInflater.java:497)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at edu.ucsb.cs.cs185.hw4skeleton.KeepScoreHomeScreen.buildSettings(KeepScoreHomeScreen.java:145)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at edu.ucsb.cs.cs185.hw4skeleton.KeepScoreHomeScreen.onOptionsItemSelected(KeepScoreHomeScreen.java:91)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:958)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.View.performClick(View.java:4084)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.view.View$PerformClick.run(View.java:16966)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.os.Handler.handleCallback(Handler.java:615)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.os.Looper.loop(Looper.java:137)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at java.lang.reflect.Method.invoke(Method.java:511)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-10 14:27:56.153: E/AndroidRuntime(1569):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

任何人都可以帮我弄清楚如何让我的观点与彼此很好地合作?

1 个答案:

答案 0 :(得分:1)

您未正确使用LayoutInflater。在以下代码块中......

View dialoglayout = inflater.inflate(R.layout.settings_layout, (ViewGroup) getCurrentFocus());

...... inflater试图做两件事:

  1. 使用提供的ViewGroup扩展XML布局以确定根LayoutParams
  2. 将膨胀的布局附加到提供的ViewGroup
  3. 您不希望发生第二件事,因为您将视图关闭到AlertDialog,而不是将其附加到“当前焦点”(显然是崩溃日志中的ListView)。您也不希望第一件事发生,因为“当前焦点”不会是这个膨胀视图的父级,因此使用它来计算LayoutParams也是没有意义的。

    通常,您应始终将要附加的直接父视图作为inflate()的第二个参数传递,然后如果您不想使用第三个参数当时附上两个。但是,在此示例中,您无法访问实际的父视图(位于正在构建的AlertDialog中),并且它将覆盖任何LayoutParams,因此您应该修改代码以在通过膨胀时不传递父代:< / p>

    View dialoglayout = inflater.inflate(R.layout.settings_layout, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialoglayout);
    

    侧面注意:

    同样,这仅适用于这种特殊情况。在几乎每个其他情况下,您应该使用inflate(),如下所示:

    inflater.inflate(R.layout.settings_layout, parent, false);

    传递膨胀布局的真实父视图的位置。