Listview获取位置错误并同时检查多个ToggleButtons

时间:2014-04-01 12:08:23

标签: android listview android-listview

我正在尝试创建数据库的listview搜索结果。我有一个问题,连续按ToggleButton并标记多个。只有当列表长于屏幕时才会发生这种情况。我不知道怎么做才会发生这种情况。 这是我的代码..谢谢!

我的ListView和行布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".BuscarUsuaris"
    android:background="@drawable/main_header_selector"
  >

      <ListView
          android:id="@+id/listView1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true" >

      </ListView>

</RelativeLayout>

行布局:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/nom_usuari"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="25dp"
        android:text=""
        android:textSize="22sp"
        android:textColor="#000000"
        android:textStyle="bold" />

    <ToggleButton
        android:id="@+id/boto_agregar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:textOn=""
        android:textOff=""
        android:layout_marginBottom="5dp"
        android:background="@drawable/togglebutton" />

</RelativeLayout>

和我的活动:

 private void connect(String busqueda) {
            String data = null;

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    getApplicationContext(), R.layout.row, R.id.nom_usuari, r) {


                class ViewHolder {
                    ToggleButton boto_agregar;
                    TextView nom_usuari;

                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    ViewHolder holder; // See the static class below. It's used to
                                        // store a temporary one time reference of
                                        // the child views (for example, TextViews
                                        // for labels, buttons etc)
                    if (convertView == null) { // We are creating the row!
                        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        convertView = inflater.inflate(R.layout.row, null); // inflate
                                                                            // our
                                                                            // XML
                                                                            // view
                        holder = new ViewHolder(); // Fetch a new ViewHolder (which
                                                    // is static FYI)

                        // Grab a copy of each child view and stick a reference in
                        // our holder
                        holder.nom_usuari = (TextView) convertView
                                .findViewById(R.id.nom_usuari);
                        holder.boto_agregar = (ToggleButton) convertView
                                .findViewById(R.id.boto_agregar);
                        // Remember the tag we mentioned earlier? We're going to use
                        // that to store a copy of our holder so we can reuse the
                        // child view references.
                        convertView.setTag(holder); // but note, as we're using this
                                                    // for our holder we can't use
                                                    // it to store the position :(
                    } else {
                        // This view already exists! Let's grab our holder we
                        // created earlier.
                        holder = (ViewHolder) convertView.getTag();
                        // Note, we could use the method findViewById rather than a
                        // holder but a holder is type safe and a little faster.
                    }

                    holder.nom_usuari.setText(r.get(position));
                    return convertView;
                }
};

        ListView list = (ListView) findViewById(R.id.listView1);
        adapter.clear(); 
        List<NameValuePair> parametres = new ArrayList<NameValuePair>();
        parametres.add(new BasicNameValuePair("busqueda", busqueda));
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(
                    "MY_HOST");
            request.setEntity(new UrlEncodedFormEntity(parametres));
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            data = EntityUtils.toString(entity);
            Log.e("DADES OBTINGUDES", data);

            try {

                JSONArray json = new JSONArray(data);

                for (int i = 0; i < json.length(); i++) {
                    JSONObject obj = json.getJSONObject(i);
                    String nombre = obj.getString("nombre");

                    Log.e("PERSONA TROBADA:", nombre);

                    r.add(nombre);

                    list.setAdapter(adapter);

                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (ClientProtocolException e) {
            Log.d("HTTPCLIENT", e.getLocalizedMessage());
        } catch (IOException e) {
            Log.d("HTTPCLIENT", e.getLocalizedMessage());
        }

0 个答案:

没有答案