如何在每个ListView行中添加ToggleButton

时间:2014-03-12 09:37:21

标签: android listview

我创建了一个ListView并显示了一个字符串数组,但现在我想向每一行添加一个ToggleButton。我该怎么做?如果我想编辑TextView样式?

我的活动代码:

public class BuscarUsuaris extends Activity implements OnQueryTextListener {
 TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buscar_usuaris);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

    }
    private void connect(String busqueda) {
      String data;

      List<String> r = new ArrayList<String>();
     ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
      ListView list=(ListView)findViewById(R.id.listView1);



      List<NameValuePair> parametres = new ArrayList<NameValuePair>();
        parametres.add(new BasicNameValuePair("busqueda", busqueda));
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost("MYURL");
            request.setEntity(new UrlEncodedFormEntity(parametres));
            HttpResponse response = client.execute(request);
            HttpEntity entity=response.getEntity();
            data=EntityUtils.toString(entity);
            Log.e("STRING", 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("STRING", 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());
        }


    }


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

     //  Atributs  pel   S E A R C H   V I E W
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setQueryHint(getString(R.string.pista_buscar));
        searchView.setOnQueryTextListener(this);
        searchView.setFocusable(true);
        searchView.setIconified(false);
        searchView.requestFocusFromTouch();

      // O C U L T A R   A L T R E S   I T E M S   D E L   M E N Ú
        MenuItem afegir = menu.findItem(R.id.afegir_amics);
        MenuItem configuracio = menu.findItem(R.id.configuracio);
        afegir.setVisible(false);  
        configuracio.setVisible(false);

        return true;
    }


    public boolean onQueryTextChange(String arg0) {
        String busca = arg0;

        if(busca.length()>4) { 
        connect(busca);
        }
        return false;
    }


    public boolean onQueryTextSubmit(String arg0) {
        String busca = arg0;


        connect(busca);



        return false;
    }

    @Override
    public void onBackPressed()
    {
        Intent i=new Intent(BuscarUsuaris.this, Perfil.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(i);
        finish();

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
                default:
                return super.onOptionsItemSelected(item); 
        }
    }

}

我的行布局:

<LinearLayout 
    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_marginLeft="3dp"
        android:text="Application Title"
        android:textSize="22dp"/>

    <ToggleButton
        android:id="@+id/boto_agregar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

我的lisview布局:

<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"
    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="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
           >
      </ListView>

</RelativeLayout>

0 个答案:

没有答案