Created
April 2, 2018 15:28
-
-
Save alitamoor65/0ae3d03037b142a408da87252cf31b59 to your computer and use it in GitHub Desktop.
List View with check Boxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:orientation="vertical" | |
android:layout_height="match_parent" | |
tools:context="com.tutorialscafe.readallsms.MainActivity"> | |
<TextView android:layout_width="fill_parent" | |
android:layout_height="wrap_content" android:padding="10dp" | |
android:text="Some text..." android:textSize="20sp" /> | |
<Button | |
android:id="@+id/findSelected" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Find countries that are Selected" /> | |
<ListView android:id="@+id/listView1" android:layout_width="fill_parent" | |
android:layout_height="fill_parent" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tutorialscafe.readallsms; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.CheckBox; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
/** | |
* Created by Ali Tamoor on 6/1/2017. | |
*/ | |
import java.util.ArrayList; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.View.OnClickListener; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.CheckBox; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import android.widget.AdapterView.OnItemClickListener; | |
public class MainActivity extends Activity { | |
MyCustomAdapter dataAdapter = null; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//Generate list View from ArrayList | |
displayListView(); | |
checkButtonClick(); | |
} | |
private void displayListView() { | |
//Array list of countries | |
ArrayList<Country> countryList = new ArrayList<Country>(); | |
Country country = new Country("AFG","Afghanistan",false); | |
countryList.add(country); | |
country = new Country("ALB","Albania",true); | |
countryList.add(country); | |
country = new Country("DZA","Algeria",false); | |
countryList.add(country); | |
country = new Country("ASM","American Samoa",true); | |
countryList.add(country); | |
country = new Country("AND","Andorra",true); | |
countryList.add(country); | |
country = new Country("AGO","Angola",false); | |
countryList.add(country); | |
country = new Country("AIA","Anguilla",false); | |
countryList.add(country); | |
//create an ArrayAdaptar from the String Array | |
dataAdapter = new MyCustomAdapter(this, | |
R.layout.country_info, countryList); | |
ListView listView = (ListView) findViewById(R.id.listView1); | |
// Assign adapter to ListView | |
listView.setAdapter(dataAdapter); | |
listView.setOnItemClickListener(new OnItemClickListener() { | |
public void onItemClick(AdapterView<?> parent, View view, | |
int position, long id) { | |
// When clicked, show a toast with the TextView text | |
Country country = (Country) parent.getItemAtPosition(position); | |
Toast.makeText(getApplicationContext(), | |
"Clicked on Row: " + country.getName(), | |
Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
private class MyCustomAdapter extends ArrayAdapter<Country> { | |
private ArrayList<Country> countryList; | |
public MyCustomAdapter(Context context, int textViewResourceId, | |
ArrayList<Country> countryList) { | |
super(context, textViewResourceId, countryList); | |
this.countryList = new ArrayList<Country>(); | |
this.countryList.addAll(countryList); | |
} | |
private class ViewHolder { | |
TextView code; | |
CheckBox name; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder = null; | |
Log.v("ConvertView", String.valueOf(position)); | |
if (convertView == null) { | |
LayoutInflater vi = (LayoutInflater)getSystemService( | |
Context.LAYOUT_INFLATER_SERVICE); | |
convertView = vi.inflate(R.layout.country_info, null); | |
holder = new ViewHolder(); | |
holder.code = (TextView) convertView.findViewById(R.id.code); | |
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1); | |
convertView.setTag(holder); | |
holder.name.setOnClickListener( new View.OnClickListener() { | |
public void onClick(View v) { | |
CheckBox cb = (CheckBox) v ; | |
Country country = (Country) cb.getTag(); | |
Toast.makeText(getApplicationContext(), | |
"Clicked on Checkbox: " + cb.getText() + | |
" is " + cb.isChecked(), | |
Toast.LENGTH_LONG).show(); | |
country.setSelected(cb.isChecked()); | |
} | |
}); | |
} | |
else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
Country country = countryList.get(position); | |
holder.code.setText(" (" + country.getCode() + ")"); | |
holder.name.setText(country.getName()); | |
holder.name.setChecked(country.isSelected()); | |
holder.name.setTag(country); | |
return convertView; | |
} | |
} | |
private void checkButtonClick() { | |
Button myButton = (Button) findViewById(R.id.findSelected); | |
myButton.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
StringBuffer responseText = new StringBuffer(); | |
responseText.append("The following were selected...\n"); | |
ArrayList<Country> countryList = dataAdapter.countryList; | |
for(int i=0;i<countryList.size();i++){ | |
Country country = countryList.get(i); | |
if(country.isSelected()){ | |
responseText.append("\n" + country.getName()); | |
} | |
} | |
Toast.makeText(getApplicationContext(), | |
responseText, Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tutorialscafe.readallsms; | |
/** | |
* Created by Ali Tamoor on 6/2/2017. | |
*/ | |
public class Country { | |
String code = null; | |
String name = null; | |
boolean selected = false; | |
public Country(String code, String name, boolean selected) { | |
super(); | |
this.code = code; | |
this.name = name; | |
this.selected = selected; | |
} | |
public String getCode() { | |
return code; | |
} | |
public void setCode(String code) { | |
this.code = code; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public boolean isSelected() { | |
return selected; | |
} | |
public void setSelected(boolean selected) { | |
this.selected = selected; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:padding="6dip" > | |
<CheckBox | |
android:id="@+id/checkBox1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:focusable="false" | |
android:focusableInTouchMode="false" | |
android:text="CheckBox" /> | |
<TextView | |
android:id="@+id/code" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignBaseline="@+id/checkBox1" | |
android:layout_alignBottom="@+id/checkBox1" | |
android:layout_toRightOf="@+id/checkBox1" | |
android:text="TextView" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment