0
我有一個問題關於在我的列表視圖中的複選框的缺陷或未選中。我意識到,我實現我的複選框的方式與其他方式不同。複選框缺血或未選中android
在mainactivity.javapackage com.example.luenwong.checkboxtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Button bn;
ListView listView;
CheckBox chkAll;
String[] android_versions = { "ID:1", "ID:2", "ID:3", "ID:4", "ID:5", "ID:6", "ID:7", "ID:8", "ID:9" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
chkAll = (CheckBox) findViewById(R.id.chkAll);
bn = (Button) findViewById(R.id.buttonStore);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, android_versions);
listView.setAdapter(adapter);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int itemCount = listView.getCount();
for(int i = 0; i < itemCount; i++)
{
if(((CheckBox) view).isChecked()){
System.out.println("hey there");
}
}
}
});
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox chk = (CheckBox) view;
int itemCount = listView.getCount();
for (int i = 0; i < itemCount; i++) {
System.out.println("correct: "+adapter.getItem(i));
listView.setItemChecked(i, chk.isChecked());
}
}
};
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
CheckBox chk = (CheckBox) findViewById(R.id.chkAll);
int checkedItemCount = getCheckedItemCount();
if (listView.getCount() == checkedItemCount)
chk.setChecked(true);
else
chk.setChecked(false);
}
};
chkAll.setOnClickListener(clickListener);
listView.setOnItemClickListener(itemClickListener);
}
private int getCheckedItemCount(){
int cnt = 0;
SparseBooleanArray positions = listView.getCheckedItemPositions();
int itemCount = listView.getCount();
for(int i=0;i<itemCount;i++){
if(positions.get(i))
cnt++;
}
return cnt;
}
}
在XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
android:paddingRight="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="selectALL"
/>
<CheckBox
android:id="@+id/chkAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getGet"
/>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
/>
<Button
android:id="@+id/buttonStore"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ADD Checkbox"
></Button>
</LinearLayout>
我真的不知道如何檢查我的複選框,用這種方法。由於複選框不是基於XML中的複選框標籤創建的。
hi @Nick Asher。感謝你的迴應,但教程是在XML中有多個複選框,我沒有在我的。 –