Issues in Custom Gridview with Multiple Radiobutton with single selection in android - android

I implemented a Gridview with radiobutton here is my code:
row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px" >
</ImageView>
<RadioButton
android:id="#+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:textSize="15px" >
</TextView>
</LinearLayout>
My MainActivity.java File
public class Radio extends Activity {
ArrayList<ParserCategory>mList;
DatabaseConnectionAPI mApi;
ImageAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*** Get Images from SDCard ***/
// gridView1
mApi=new DatabaseConnectionAPI(getApplicationContext());
try {
mApi.createDataBase();
mApi.openDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mList=mApi.getCategoryData();
final GridView gView1 = (GridView)findViewById(R.id.gridView1);
mAdapter=new ImageAdapter(Radio.this, mList);
gView1.setAdapter(mAdapter);
// Get Item Checked
Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
btnGetItem.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int count = gView1.getChildCount();
System.out.println("Count "+count);
for (int i = 0; i < count; i++) {
ViewGroup gridChild = (ViewGroup) gView1.getChildAt(i);
RadioButton rbtn = (RadioButton)gridChild.findViewById(R.id.radio);
if(rbtn.isChecked())
{
Log.d("Item "+String.valueOf(i), rbtn.getTag().toString());
System.out.println("ITEEMERM "+String.valueOf(i)+rbtn.getTag().toString());
Toast.makeText(Radio.this,rbtn.getTag().toString() ,2000).show();
}
}
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<ParserCategory> lis;
public ImageAdapter(Context c, ArrayList<ParserCategory> mList)
{
context = c;
lis = mList;
}
public int getCount() {
return lis.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.raddioshowimage, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
String strPath = lis.get(position).toString();
// Get File Name
String fileName=lis.get(position).getCname();
textView.setText(lis.get(position).getCname());
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
RadioButton radiobtn = (RadioButton) convertView.findViewById(R.id.radio);
radiobtn.setTag(fileName);
return convertView;
}
}
}
When I run the above code it selects multiple items with radiobutton, but I want a single item selection when I click a button so any idea how can I solve it?

Related

Custom ListViewAdapter with Radio Buttons.

I am coding a listView that gets the data from mySQL Server.
I have created the followings classes.
Class ListView. It has two contractors all in Strings. And I sat getters and setters.
I believe I have a problem with the Adapter its self. I can click on more than one option. While I have created the custom adapter in the layout as the followings:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RG_Adapter"
android:layoutDirection="rtl"
android:orientation="horizontal">
<RadioButton
android:id="#+id/TRIP_NAME"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:textAlignment="center"
android:textSize="25sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/SUM_TRIPS"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#000000"
android:textAlignment="center"
android:textSize="25sp" />
</RadioGroup>
I believe my problem is with the Adapter class.
I have created it like the followings:
public class TRIPS_LISTVIEW_ADAPTER extends ArrayAdapter<TRIPS_LISTVIEW> {
private Context mContext;
private ArrayList<TRIPS_LISTVIEW> mData;
private MyFunctionsClass myFunctionsClass = new MyFunctionsClass();
public TRIPS_LISTVIEW_ADAPTER (Context mContext, ArrayList<TRIPS_LISTVIEW> mData) {
super(mContext, R.layout.summary_shape_layout,mData);
this.mContext = mContext;
this.mData = mData;
}
public int getCount() {
return mData.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.summary_shape_layout, null);
}
RadioGroup RG = (RadioGroup) convertView.findViewById(R.id.RG_Adapter);
TextView TRIP_NAME = (TextView) convertView.findViewById(R.id.TRIP_NAME);
TRIP_NAME.setTypeface(myFunctionsClass.FONT( TRIP_NAME.getContext().getAssets(),1));
TRIP_NAME.setText(myFunctionsClass.get_The_trip(mData.get(position).getTRIP_TITLE()));
TextView SUM_TRIPS = (TextView) convertView.findViewById(R.id.SUM_TRIPS);
SUM_TRIPS.setTypeface(myFunctionsClass.FONT( SUM_TRIPS.getContext().getAssets(),1));
SUM_TRIPS.setText(mData.get(position).getTRIP_COUNT());
return convertView;
}
}
The Data in my MainActivity Class are retrieved correctly. But as I mentioned I have multiple mode selection.
Try the following:
1) Demo2.class:-------------
public class Demo2 extends AppCompatActivity {
private ListView lv;
private CheckBox cb;
private Adapter adapter;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
checkBoxState = new ArrayList<>();
checkBoxText = new ArrayList<>();
for(int i = 0 ; i<10 ; i++){
if(i == 0) {
checkBoxState.add(i, true);
}else{
checkBoxState.add(i , false);
}
checkBoxText.add( i , "C" + (i+1));
}
lv = (ListView) findViewById(R.id.lv);
adapter = new Adapter(getApplicationContext() , checkBoxState , checkBoxText);
lv.setAdapter(adapter);
}
}
2) Adapter.class:------
public class Adapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;
public Adapter(Context context, List<Boolean> checkBoxState , List<String> checkBoxText) {
this.context = context;
layoutInflater = LayoutInflater.from(context);
this.checkBoxState = checkBoxState;
this.checkBoxText = checkBoxText;
}
public int getCount() {
return checkBoxState.size();
}
public Object getItem(int position) {
return checkBoxState.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final CheckBox cb_list_item;
if (convertView == null) {
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.list_item, null);
}
}
cb_list_item = (CheckBox) view.findViewById(R.id.cb_list_item);
cb_list_item.setText(checkBoxText.get(position));
cb_list_item.setOnCheckedChangeListener(null); // mask onCheckedChangeListener()
cb_list_item.setChecked(checkBoxState.get(position));
cb_list_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b) { // already selected
compoundButton.setChecked(true);
} else { // is selected now
checkSelected(position);
}
}
});
return view;
}
private void checkSelected(int position){
try {
for (int i = 0; i < checkBoxState.size(); i++) {
checkBoxState.set(i, false);
}
checkBoxState.set(position, true);
this.notifyDataSetChanged();
}catch (Exception e){
e.printStackTrace();
}
}
}
3) demo2.xml:------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
</ListView>
</LinearLayout>
4) list_item.xml:----------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/cb_list_item"/>
</android.support.constraint.ConstraintLayout>
5) Output:--------

Button inside Grid layout is null

I am creating custom grid. Each row has 1 button and 2 textviews. I am trying to assign values/text to each programmatically. I am able to set text for both the text views but, findviewbyId for button returns null.
Grid is like:
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#FFFFFF"
android:padding="5dip"
android:id="#+id/gvMYActivitySearch" />
Row layout is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnHistory"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="#drawable/button_background"
android:text="History"/>
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="35dp" />
<TextView
android:id="#+id/tvContact"
android:layout_width="wrap_content"
android:layout_height="35dp" />
</LinearLayout>
Custom grid java file is like
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] names;
private final String[] Contact;
private final String[] ID;
public CustomGrid(Context c,String[] names,String[] Contact, String[] ID ) {
mContext = c;
this.Contact = Contact;
this.names = names;
this.ID = ID;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//grid = new View(mContext);
grid = inflater.inflate(R.layout.myactivity_singlerow, null);
} else {
grid = (View) convertView;
}
TextView tvName = (TextView) grid.findViewById(R.id.tvName);
TextView tvContact = (TextView) grid.findViewById(R.id.tvContact);
Button btnHistory = (Button) grid.findViewById(R.id.btnHistory);
tvName.setText(names[position]);
tvContact.setText(Contact[position]);
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
new MainActivity().testButton(view.getId());
}
}
);
}
return grid;
}
When I try to debug, I can see for the first time when position is '0', btnHistory is not null. But after that when position is 1, btnHistory is null.
What could possibly go wrong? When both the textviews are not null, why btnHistory is null?
Comment to improve your code:
callback somewhere outside your getView
I mean this:
public class CustomGrid extends BaseAdapter implements OnClickListener {
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setTag((Integer)view.getId());
btnHistory.setOnClickListener(this);
}
public void onClick(View v) {
new MainActivity().testButton(v.getTag());
}
}

Android : how to get value from assets and pass it thru array

I want to short my code length by using array.If I have 100 button or image view I have to write for 100 . I want to get value from assets folder and pass it in to array . how to use
String[] colorNames = getResources().getStringArray(R.array.colorList);
String[] animalNames = getResources().getStringArray(R.array.animalList);
Current working code :
public class Main extends Activity {
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag);
Intent intent = getIntent();
String key = intent.getStringExtra("YOUR_KEY");
image = (ImageView) findViewById(R.id.image);
Button btn1,btn2;
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
if(key.equals("animalIntent") )
{
btn1.setText("cat");
btn2.setText("dog");
// same for 100 button ............. here i want to short my code
} else {
btn1.setText("red");
btn2.setText("blue");
}
}
public void btn1(View view) {
Intent intent = getIntent();
String key = intent.getStringExtra("YOUR_KEY");
if(key.equals("animalIntent") )
{
image.setBackgroundResource(R.drawable.cat);
// same for 100 image view ............. here i want to short my code
} else {
image.setBackgroundResource(R.drawable.red);
}
}
public void btn2(View view) {
Intent intent = getIntent();
String key = intent.getStringExtra("YOUR_KEY");
if(key.equals("animalIntent") )
{
image.setBackgroundResource(R.drawable.dog);
} else {
image.setBackgroundResource(R.drawable.blue);
}
}
To do that you should use GridView,it would be best option. try the below sample code.
Step-1 add the gridview in your drag.xml
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edt"
android:numColumns="2"
android:stretchMode="columnWidth" >
</GridView>
Step-2 Declare the variables and get the grid view reference.
GridView gridview;
Integer[] drawableIds = { R.drawable.poster1, R.drawable.poster2, R.drawable.poster3, R.drawable.poster4,
R.drawable.poster5 };
String[] colorNames = { "poster one", "poster two", "poster three", "poster four", "poster five" };
//In OnCreate() method add
gridview = (GridView) findViewById(R.id.grid);
gridview.setAdapter(new CustomGridAdapter(this, drawableIds, colorNames));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Main.this, "" + position, Toast.LENGTH_LONG).show();
}
});
Step-3 create a class for grid adapter CustomGridAdapter.class
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private Integer[] drawableitems;
private String[] drawablelabel;
public CustomGridAdapter(Context context, Integer[] drawableitems, String[] drawablelabel) {
this.context = context;
this.drawablelabel = drawablelabel;
this.drawableitems = drawableitems;
}
#Override
public int getCount() {
return drawableitems.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
RecordHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new RecordHolder();
convertView = inflater.inflate(R.layout.grid_item, null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.grid_item_label);
holder.imageItem = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(holder);
} else {
holder = (RecordHolder) convertView.getTag();
}
holder.txtTitle.setText(drawablelabel[position]);
holder.imageItem.setImageResource(drawableitems[position]);
return convertView;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
Add custom view for grid grid_item.xml in layout folder.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
Hope it will help you.

CheckBox in custom ListView on Android

I got a custom ListView, and I put three TextView in the List using a custom List Adapter! How I can to add a CheckBox with each List Item and make each CheckBox checked on list item click?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#7a4b9d"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/calorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sdf"
android:textColor="#ffffff" />
<TextView
android:id="#+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="sdf"
android:textColor="#ffffff" />
<CheckBox
android:id="#+id/chkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
MainActivity
public class Main_Activity extends Fragment implements OnClickListener {
private ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.main_activity, null);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv = (ListView) root.findViewById(R.id.list_two);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setTextFilterEnabled(true);
lv.setAdapter(new MyCustomBaseAdapter(getActivity(), searchResults));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
boolean result = searchResults.get(position).isSolved();
if (result) {
searchResults.get(position).setSolved(false);
} else {
searchResults.get(position).setSolved(true);
}
Toast.makeText(getActivity(),"You have chosen: " + " " +
fullObject.getPhone(), Toast.LENGTH_SHORT).show();
}
});
return root;
}
private ArrayList<SearchResults> GetSearchResults() {
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr = new SearchResults();
sr.setName("Apple");
sr.setCalorie("35");
sr.setPrice("5");
results.add(sr);
return results;
}
}
Custom Base Adapter
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCalorie = (TextView) convertView
.findViewById(R.id.calorie);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.chkBox1 = (CheckBox)convertView.findViewById(R.id.chkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCalorie.setText(searchArrayList.get(position)
.getCalorie());
holder.txtPrice.setText(searchArrayList.get(position).getPrice());
holder.chkBox1.setChecked(searchArrayList.get(position).isSolved());
return convertView;
}
static class ViewHolder {
CheckBox chkBox1;
TextView txtName;
TextView txtCalorie;
TextView txtPrice;
}
Search Results
public boolean isSolved() {
// TODO Auto-generated method stub
return b;
}
public void setSolved(boolean b) {
// TODO Auto-generated method stub
this.b = b;
}
You will have to define your CheckBox view in custom_row_view.xml file first,
<CheckBox android:id="#+id/chkBox1"
android:layout_width=...
android:layout_height=... />
Then, you wll have to call this reference in your MyCustomBaseAdapter class or in the holder in your case, like
CheckBox chkBox1;
then,
holder.chkBox1 = = (CheckBox)convertView.findViewById(R.id.chkBox1);
You can define a boolean value in your SearchResults class which can take care of chkBox check and use
holder.chkBox1.setChecked(searchArrayList.get(position).isSolved());
Something on these lines and you will be good to go :)
EDIT: Remember to change the value of boolean in searchResult instance on itemClick.
boolean result = searchResults.get(position).isSolved();
if (result) {
searchResults.get(position).setSolved(false);
} else {
searchResults.get(position).setSolved(true);
}
lv.getAdapter().notifyDataSetChanged();

Custom List View with OnClickListener

I have custom ListView layout with a TextView and CheckBox. Everything works fine.
What I want is, when I click on the CheckBox or TextView (on the single View from ListView) both should behave like one object. (I can click on the CheckBox and it does not effect the TextView and TextView has no effect on CheckBox.) Code has no problem.
I have implemented all possible solutions but problem is still there. (One single click on every object of list should consider ONE COMPLETE CLICK for complete row.) I hope I explained very well.
MAIN ACTIVITY
package com.example.smsplanner;
public class SMSPlanner extends ListActivity {
ListView contactsListView;
private String TAG = "SMSPlanner"; CheckBox check;
int count;
List<ContactInfo> list = new ArrayList<ContactInfo>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ph = new String[3];
phType = new String[3];
LoadContactListFromPhone();
ContactsAdapter contactadAdapter = new ContactsAdapter(this, list);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(contactadAdapter);
}
#Override
public void onListItemClick(ListView parent, View v, int position, long id)
{
TextView tx =(TextView)v.findViewById(R.id.firstname);
TextView ph =(TextView)v.findViewById(R.id.phone);
Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show();
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
void LoadContactListFromPhone()
{
loadlistandreturns();
}
void call()
{
Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG);
toast.show();
}
}
CUSTOM ADAPTER
public class ContactsAdapter extends ArrayAdapter<ContactInfo>
{
private final Activity context;
int resourceid;
List<ContactInfo> list = null;
public ContactsAdapter(Activity context, List<ContactInfo> list) {
super(context, R.layout.contactrow, list);
this.context = context;
this.list = list;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
}
LAYOUT
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:orientation="vertical" >
<TextView
android:id="#+id/firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgStyle2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical" >
<CheckBox
android:id="#+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
</RadioGroup>
</LinearLayout>
1. Bydefault all the Rows of the ListView are enabled to listen to click....
You must implement onItemClickListener() for the ListView....
See this example:
http://www.mkyong.com/android/android-listview-example/
you can use a CheckedTextView, or you can create a Checkable Layout, like this one :
http://tokudu.com/2010/android-checkable-linear-layout/
i think you should make adapter as
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}

Categories

Resources