我创建了一个ListView。在每一行中都有一个TextView和一个ToggleButton。每个TextView中显示的文本都是来自我的数据库的结果。如何从行的ToggleButton的OnClick中的每一行获取文本?谢谢!
这是我的活动代码:
public class BuscarUsuaris extends Activity implements OnQueryTextListener {
ToggleButton boto_agregar;
TextView nom_usuari;
public boolean pressed = false;
@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);
nom_usuari=(TextView)findViewById(R.id.nom_usuari);
boto_agregar=(ToggleButton)findViewById(R.id.boto_agregar);
}
private void connect(String busqueda) {
String data=null;
final List<String> r = new ArrayList<String>();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.row, R.id.nom_usuari,r);
ListView list=(ListView)findViewById(R.id.listView1);
boto_agregar=(ToggleButton)findViewById(R.id.boto_agregar);
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());
}
}
我的行布局:
<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="22dp"
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:background="@drawable/togglebutton" />
</RelativeLayout>
答案 0 :(得分:1)
您需要覆盖适配器的getView:
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, 0 , r){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View from recycle
View row = convertView;
// Handle inflation and make sure not to re-use a header view
if (row == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row, null);
}
String rowText = r.get(position);
ToggleButton tBtn = (ToggleButton) row.findViewById(R.id.boto_agregar);
tBtn.setTag(position);
tBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//see which row was changed
int position = Integer.parseInt(buttonView.getTag().toString());
//get the row text
String changedRowText = r.get(position);
}
});
TextView rowTV = (TextView) row.findViewById(R.id.nom_usuari);
rowTV.setText(rowText);
return row;
}
};
答案 1 :(得分:0)
你应该添加一个onClick监听器。 如果您有自定义适配器类,请尝试以下方法:
public class MyAdapter extends ArrayAdapter<MyClass> implements
OnClickListener{
//adapter behavior
@Override
public void onClick(View v) {
TextView idView = (TextView) v
.findViewById(R.id.TextId);
//etc..
}
}