Android - 使用OnClick侦听器的Json阵列到ImageView

时间:2015-09-04 04:59:21

标签: android arrays json

我正在尝试完成某个结果,我从Web服务中获取一个数字数组,然后将这些数字中的每一个分配给某个将作为按钮的ImageView,因此我的活动中有22个ImageViews ,这就是我的json数组看起来像这个

{
    "GetAvailableSeatsResult": [
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20
    ]
}

每个图像视图都是可点击的,所以当点击它时,它会从数组中保留其中一个数字,并在点击后显示不同的图像,我能够获取数据,但不知道如何处理这个< / p>

这是我获取数据的课程

public class Seats extends AppCompatActivity {

    private static final String availableSeatsURL = "url";
    private static final String availableSeatsArray = "GetAvailableSeatsResult";
    private Toolbar mToolbar;

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

        mToolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getAvailableSeats();
    }

    public class Seat {
        private int index;
        private int number;

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }
    }

    public ArrayList<String> getAvailableSeats(String string) {
        return null;
    }

    public void getAvailableSeats() {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, availableSeatsURL + 26 + "/" + 7, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                VolleyLog.d(response.toString());

                ArrayList<Seat> seats = new ArrayList<Seat>();
                ArrayList<String> availableSeats = new ArrayList<String>();

                try {
                    JSONArray jsonArray = response.getJSONArray(availableSeatsArray);
                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject jSONObject = jsonArray.getJSONObject(i);



                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("Error: " + error.getMessage());

            }
        });
        int MY_SOCKET_TIMEOUT_MS = 20000;
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                MY_SOCKET_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
    }

}

这是我的activity_seats.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="net.app.cairobus.cairobus.Seats">

    <android.support.v7.widget.Toolbar
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/app_bar">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView2"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView5"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView6"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView7"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView9"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView10"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView11"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView14"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView15"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView17"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView18"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView19"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView21"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView22"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView23"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView25"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView26"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView27"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView29"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView30"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView31"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView32"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView33"
                    android:src="@drawable/freeseat"
                    android:clickable="true"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

以编程方式创建imageview,将其添加到视图并将onClickListener设置为

    for (int i = 0; i < seatsArray.length; i++) {
        imageView[i] = new ImageView(this);
        //set image, etc...
        imageView[i].setId(i);
        ll.addView(imageView[i]); //add it to your layout
        imageView[i].setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //v.setBackground(...);

            }
         });
     }

如果你有很多ImageView,你可以考虑使用ListView / GridView。