チェックボックスでカスタマイズListView
しました。TextView
チェックされたチェックボックスの値を取得したい。
私の例はうまくいきません:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace Chamada
{
[Activity (Label = "Chamada", MainLauncher = true)]
public class MainActivity : Activity
{
private SqlConnection con = null;
private SqlCommand cmd = null;
List<Chamada> chamada = new List<Chamada>();
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
con = new SqlConnection ("");
con.Open ();
cmd = new SqlCommand ("", con);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
SqlDataAdapter da = new SqlDataAdapter (cmd);
DataSet ds = new DataSet ();
da.Fill (ds);
foreach (DataRow row in ds.Tables[0].Rows) {
if (row ["Imagem"].ToString () == "1") {
chamada.Add (new Chamada (row ["Ds_Nome"].ToString (), row ["Cd_Prefixo"].ToString (), Resource.Drawable.sucesso));
} else {
chamada.Add (new Chamada (row ["Ds_Nome"].ToString (), row ["Cd_Prefixo"].ToString (), Resource.Drawable.erro));
}
}
ListView listView = FindViewById<ListView> (Resource.Id.lsv_Chamar);
listView.Adapter = new adapter_list (this, chamada);
Button button = FindViewById<Button> (Resource.Id.btn_Save);
button.Click += Gravar_Click;
}
**// this is the part of code that is not working**
**void Save_Click(object sender, EventArgs e)
{
var builder = new StringBuilder();
var sparseArray = FindViewById<ListView> (Resource.Id.lsv_Chamar);
TextView text = FindViewById<TextView> (Resource.Id.txt_Prefixo);
CheckBox check = FindViewById<CheckBox> (Resource.Id.checkBox1);
for (var i = 0; i < sparseArray.Count; i++)
{
//if (check.Checked)
//{
// builder.AppendLine (text.Text);
//}
}
ShowAlert ("ListView", builder.ToString ());
}**
private void ShowAlert(string title, string message)
{
Android.App.AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog alertDialog = builder.Create();
alertDialog.SetTitle(title);
alertDialog.SetIcon(Android.Resource.Drawable.IcDialogAlert);
alertDialog.SetMessage(message);
alertDialog.SetButton("OK", (s, ev) =>
{
//DO Something
});
alertDialog.Show();
}
}
}
これは、リストビューが添付されている私のxmlです:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:background="#ffffffff">
<LinearLayout
android:orientation="horizontal"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="Prefixo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:typeface="normal"
android:layout_marginTop="10dp"
android:id="@+id/txt_Prefixo"
android:layout_marginRight="5.0dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="640.0dp"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nome"
android:textColor="#000000"
android:typeface="normal"
android:layout_marginTop="10dp"
android:id="@+id/txt_Nome"
android:layout_marginRight="5.0dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:src="@drawable/erro"
android:layout_width="38.0dp"
android:layout_height="39.0dp"
android:id="@+id/imageView1"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="17.0dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:minWidth="25px"
android:minHeight="25px">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox1"
android:shadowColor="#ff110707" />
</LinearLayout>
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@drawable/degrade_fundo">
<TextView
android:text="Lista de Chamada"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:textSize="13dp"
android:id="@+id/txt_Titulo"
android:textColor="#ffffffff" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#ff949494">
<LinearLayout
android:orientation="horizontal"
android:layout_width="31dp"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="Prefixo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffe0e0e0"
android:typeface="normal"
android:id="@+id/cabe_Prefixo"
android:layout_marginRight="5.0dp"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="640.0dp"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nome"
android:textColor="#ffe0e0e0"
android:typeface="normal"
android:id="@+id/cabe_Nome"
android:layout_marginRight="5.0dp"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status"
android:textColor="#ffe0e0e0"
android:typeface="normal"
android:id="@+id/cabe_Status"
android:layout_marginRight="5.0dp"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000000"
android:typeface="normal"
android:layout_marginTop="10dp"
android:id="@+id/cabe_Check"
android:layout_marginRight="5.0dp" />
</LinearLayout>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lsv_Chamar"
android:background="@android:color/transparent" />
<Button
android:text="@string/Save"
android:textSize="15dp"
android:textColor="#FFFFFF"
android:layout_width="177.0dp"
android:layout_height="36.0dp"
android:id="@+id/btn_Gravar"
android:gravity="center"
android:layout_marginRight="0.0dp"
android:layout_marginLeft="382.0dp"
android:fadingEdge="none"
android:background="@drawable/botao_style" />