Reputation: 143
I have been battling this problem for over a day now and every solution I try fails to work for me. Basically what I need: I have a listview containing a checkbox and a textview. At the moment when the user taps the checkbox it gets checked/unchecked. It works perfectly. But the checkboxes are kinda small so I want them to get checked when the user taps on the corresponding textview, how must I do that?
Here's my code:
public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
Button m = (Button) findViewById(R.id.button3);
tv = (TextView) findViewById(R.id.textViewcat);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);
String[] listArray = new String[] { "All", "Friends & Family", "Sports", "Outside", "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);
Boolean[] checkedStatus = new Boolean[listArray.length];
for ( int index = 0; index < checkedStatus.length; index++)
checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);
ListView listView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
listView.setAdapter(adapter);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.animation8, R.anim.animation7);
}
}
MyAdapter:
public class MyAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener{
String[] values;
Boolean[] checkedStatus;
public MyAdapter(Context context, int resource, String[] values, Boolean[] checkedStatus) {
super(context, resource, values);
this.values = values;
this.checkedStatus = checkedStatus;
}
@Override
public int getCount() {
return values.length;
}
@Override
public String getItem(int position) {
return values[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, null);
}
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(values[position]);
CheckBox box = (CheckBox) view.findViewById(R.id.chk);
box.setTag(position);
box.setOnCheckedChangeListener(this);
box.setChecked(checkedStatus[position]);
return view;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Integer index = (Integer) buttonView.getTag();
checkedStatus[index] = isChecked;
String key = buttonView.getTag().toString();
SharedPreferences sharedPreferences=getContext().getSharedPreferences("status", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean(key,isChecked);
editor.apply();
}
}
Upvotes: 0
Views: 137
Reputation: 6162
You should use CheckedTextView instead of TextView
and CheckBox
. With this single widget you can get that effect.
EDIT :
code snippet of Activity
public class MainActivity extends Activity {
ListView myListView;
Button getResult;
private ArrayList<String> dayOfWeekList = new ArrayList<String>();
private void initDayOfWeekList(){
dayOfWeekList.add("Sunday");
dayOfWeekList.add("Monday");
dayOfWeekList.add("Tuesday");
dayOfWeekList.add("Wednesday");
dayOfWeekList.add("Thursday");
dayOfWeekList.add("Friday");
dayOfWeekList.add("Saturday");
}
MyArrayAdapter myArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDayOfWeekList();
setContentView(R.layout.activity_main);
myListView = (ListView)findViewById(R.id.list);
myArrayAdapter = new MyArrayAdapter(
this,
R.layout.row,
android.R.id.text1,
dayOfWeekList
);
myListView.setAdapter(myArrayAdapter);
myListView.setOnItemClickListener(myOnItemClickListener);
getResult = (Button)findViewById(R.id.getresult);
getResult.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String result = "";
//getCheckedItems
List<String> resultList = myArrayAdapter.getCheckedItems();
for(int i = 0; i < resultList.size(); i++){
result += String.valueOf(resultList.get(i)) + "\n";
}
myArrayAdapter.getCheckedItemPositions().toString();
Toast.makeText(
getApplicationContext(),
result,
Toast.LENGTH_LONG).show();
}});
}
OnItemClickListener myOnItemClickListener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
myArrayAdapter.toggleChecked(position);
}};
private class MyArrayAdapter extends ArrayAdapter<String>{
private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();
public MyArrayAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
for(int i = 0; i < objects.size(); i++){
myChecked.put(i, false);
}
}
public void toggleChecked(int position){
if(myChecked.get(position)){
myChecked.put(position, false);
}else{
myChecked.put(position, true);
}
notifyDataSetChanged();
}
public List<Integer> getCheckedItemPositions(){
List<Integer> checkedItemPositions = new ArrayList<Integer>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItemPositions).add(i);
}
}
return checkedItemPositions;
}
public List<String> getCheckedItems(){
List<String> checkedItems = new ArrayList<String>();
for(int i = 0; i < myChecked.size(); i++){
if (myChecked.get(i)){
(checkedItems).add(dayOfWeekList.get(i));
}
}
return checkedItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1);
checkedTextView.setText(dayOfWeekList.get(position));
Boolean checked = myChecked.get(position);
if (checked != null) {
checkedTextView.setChecked(checked);
}
return row;
}
}
}
layout activity_main
<LinearLayout 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:orientation="vertical" >
<Button
android:id="@+id/getresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Result"/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
layout row
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_vertical" />
Screenshot :
Upvotes: 0
Reputation: 2840
i would suggest you check/uncheck the checkbox when the user clicks on the list item. It sounds more appropriate to me as a solution. So inside the getView()
method of the adapter you need to add this piece of code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_layout, null);
}
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(values[position]);
CheckBox box = (CheckBox) view.findViewById(R.id.chk);
box.setTag(position);
box.setOnCheckedChangeListener(this);
box.setChecked(checkedStatus[position]);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on each click change the state of checkbox
box.setChecked(!box.isChecked());
}
});
return view;
}
Upvotes: 1
Reputation: 839
In your MyAdpater
write click listener for parent layout or for textview, and then on click of that just check currently check box is checked or not and as per that perform action.
e.g.
LinearLayout ll (parent layout)
CheckBox cb;
ll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(cb.isChecked())
{
code for unchecked checkbox
}
else
{
code for checked checkbox
}
}
});
Upvotes: 0