Android 不确定如何在listView中实现开关

Android 不确定如何在listView中实现开关,android,listview,android-switch,Android,Listview,Android Switch,因此,我面临的问题是,我有一个listView,其中包含一个开关和JSON中的一些附加数据。我需要能够发送开关的位置(开或关)和json的一部分、设备ID,然后将它们发送到数据库。由于我对这类事情还不熟悉,我不知道该怎么做,甚至不知道该在哪里做 任何帮助都将不胜感激 这是我的前端 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/

因此,我面临的问题是,我有一个
listView
,其中包含一个开关和
JSON
中的一些附加数据。我需要能够发送开关的位置(开或关)和
json
的一部分、设备ID,然后将它们发送到数据库。由于我对这类事情还不熟悉,我不知道该怎么做,甚至不知道该在哪里做

任何帮助都将不胜感激

这是我的前端

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:gravity="center_vertical|center_horizontal">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/device_type"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center_vertical" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.77">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="device_name"
            android:id="@+id/device_name" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="deviceID"
            android:id="@+id/device_id"
            android:layout_weight="1" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="type"
            android:id="@+id/type"
            android:layout_weight="1" />
    </LinearLayout>

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="New Switch"
        android:id="@+id/switch1"
        android:layout_gravity="center_vertical" />
</LinearLayout>
显示列表的类

package mmu.tom.linkedviewproject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import org.json.JSONArray;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ShowDevice";


private ListView GetAllDevicesListView;
private JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i(TAG, "  OPened this");


    ImageButton button1 = (ImageButton) findViewById(R.id.image_button_new);

    button1.setOnClickListener(new View.OnClickListener() {
        Class ourClass;

        public void onClick(View v) {

            Intent i;
            i = new Intent(MainActivity.this, DeviceDetailsActivity.class);
            startActivity(i);
        }
    });




    this.GetAllDevicesListView = (ListView) this.findViewById(R.id.GetAllDevicesListView);

    new GetAllDevicesTask().execute(new ApiConnector());
}



public void setListAdapter(JSONArray jsonArray){

    this.jsonArray = jsonArray;
    this.GetAllDevicesListView.setAdapter((new GetAllDeviceListViewAdapter(jsonArray, this)));


}

private class GetAllDevicesTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
    @Override
    protected JSONArray doInBackground(ApiConnector... params) {

        // it is executed on Background thread

         return params[0].GetAllDevicesState();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {

     setListAdapter(jsonArray);



    }


}


}

如果您的问题是将一些侦听器添加到此listView中的视图,则可以使用。您应该将它们添加到适配器中。谢谢,我将在下班后尝试!
package mmu.tom.linkedviewproject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;

import org.json.JSONArray;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ShowDevice";


private ListView GetAllDevicesListView;
private JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i(TAG, "  OPened this");


    ImageButton button1 = (ImageButton) findViewById(R.id.image_button_new);

    button1.setOnClickListener(new View.OnClickListener() {
        Class ourClass;

        public void onClick(View v) {

            Intent i;
            i = new Intent(MainActivity.this, DeviceDetailsActivity.class);
            startActivity(i);
        }
    });




    this.GetAllDevicesListView = (ListView) this.findViewById(R.id.GetAllDevicesListView);

    new GetAllDevicesTask().execute(new ApiConnector());
}



public void setListAdapter(JSONArray jsonArray){

    this.jsonArray = jsonArray;
    this.GetAllDevicesListView.setAdapter((new GetAllDeviceListViewAdapter(jsonArray, this)));


}

private class GetAllDevicesTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
    @Override
    protected JSONArray doInBackground(ApiConnector... params) {

        // it is executed on Background thread

         return params[0].GetAllDevicesState();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {

     setListAdapter(jsonArray);



    }


}


}
package mmu.tom.linkedviewproject;

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;


public class ApiConnector {

public JSONArray GetAllDevicesState() {
    // URL for getting all customers


    String url = "http://52.88.194.67:8080/IOTProjectServer/getDevice";

    // Get HttpResponse Object from url.
    // Get HttpEntity from Http Response Object

    HttpEntity httpEntity = null;

    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();


    } catch (ClientProtocolException e) {

        // Signals error in http protocol
        e.printStackTrace();

        //Log Errors Here


    } catch (IOException e) {
        e.printStackTrace();
    }


    // Convert HttpEntity into JSON Array
    JSONArray jsonArray = null;

    if (httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);

            Log.e("Entity Response  : ", entityResponse);

            jsonArray = new JSONArray(entityResponse);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;


}

public JSONArray GetDeviceDetails(int DeviceID) {
    // URL for getting all customers


    String url = "http://52.88.194.67:8080/IOTProjectServer/registerDevice?";

    // Get HttpResponse Object from url.
    // Get HttpEntity from Http Response Object

    HttpEntity httpEntity = null;

    try {

        DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();


    } catch (ClientProtocolException e) {

        // Signals error in http protocol
        e.printStackTrace();

        //Log Errors Here


    } catch (IOException e) {
        e.printStackTrace();
    }


    // Convert HttpEntity into JSON Array
    JSONArray jsonArray = null;
    if (httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);
            Log.e("Entity Response  : ", entityResponse);

            jsonArray = new JSONArray(entityResponse);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;


}

}