How to drive gridview in Android? - android

I have written following to show my data into grid view. This is the procedure of my code.
In onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newslist);
// Initializing NewsAdapter in order to loading the list of NEWS items
newsAdapter = new NewsAdapter(this);
// Initializing grid view to show news
gridView = (GridView) findViewById(R.id.news_gridview);
gridView.setAdapter(newsAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(NewsList.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
Then in onStart() I'll get XML data from server then parse it and final parsed data will be stored in "parsedList" (100% I have data in this variable because I saw it in logcat).
#Override
protected void onStart() {
super.onStart();
String strNewsContent = "***";
...
newsAdapter.setData(parsedList);
newsAdapter.notifyDataSetChanged();
}
Finally this is my "NewsAdapter":
public class NewsAdapter extends BaseAdapter {
private LayoutInflater myInflater;
private NewsFeedItemList itemList;
public NewsAdapter(Context c) {
myInflater = LayoutInflater.from(c);
}
public void setData(NewsFeedItemList newsFeedItemList){
itemList = newsFeedItemList;
Log.i("NewsAdapter", "Parsed list passed to the adapter.");
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.grid_news, null);
holder = new ViewHolder();
holder.ivIcon = (TextView) convertView.findViewById(R.id.news_icon);
holder.tvLabel = (TextView) convertView.findViewById(R.id.news_label);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ivIcon.setText(itemList.getImage().get(position));
holder.tvLabel.setText(itemList.getTitle().get(position));
Log.i("Position is>>>>:", Integer.toString(position));
return convertView;
}
static class ViewHolder {
TextView ivIcon;
TextView tvLabel;
}
}
The format of "grid_news" is like this
<?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" >
<TextView
android:id="#+id/news_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/menu_contentdescription"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/news_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10dip" />
</RelativeLayout>
I have no idea why I can't see the result in my emulator/device.

I think it is because of getCount() returning 0; shouldn't it be itemList.size()? in news NewsAdapter.

Related

How can I make an android xml layout for Category and CategoryItem

I'am new in using java and android studio and i was wondering how can i make a layout like this in 1 activity. myImage
What you are looking for is a Listview adapter
public class ExampleAdapter extends BaseAdapter {
List<Product> products;
private Activity context;
#Override
public int getCount() {
return products.size();
}
public ExampleAdapter(List<Product> products, Activity context) {
this.products = products;
this.context = context;
}
#Override
public Object getItem(int position) {
return products.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Product product = products.get(position);
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.row_shopping_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.getProductTextView().setText(product.getName());
holder.getProductImageView(). setImageResource(product.getPicture());
return convertView;
}
private class ViewHolder {
private final TextView productTextView;
private final ImageView productImageView;
private ViewHolder(View wrapperView) {
productTextView = (TextView) wrapperView.findViewById(R.id.tvName);
productImageView = (TextView) wrapperView.findViewById(R.id.ivPicture);
}
public TextView getProductTextView() {
return productTextView;
}
public ImageView getProductImageView() {
return productImageView;
}
}
}
This adapter needs a layout to define what the rows will look like. This will be something like this
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_gravity="center_vertical"
android:layout_width="50dp"
android:id="#+id/ivPicture"
android:layout_height="50dp" />
<TextView
android:id="#+id/tvName"
android:text="Product 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Lastly in your activity where you want to display the overview you will need to use a listview and set the adapter
final ExampleAdapter adapter = new ExampleAdapter(list, this);
listview.setAdapter(adapter);

How to create a list as below picture?

A setting picture
How to do that in android?
Before I used to referencescreen but it could'n use icon for each item.
It is a simple Listview. Refer the following code and replace the list_row with the list row design .xml that you want. This should help.
public class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
View row;
public static List<String> list = new ArrayList<String>();
Context context;
class ViewHolder {
ImageView ivShare;
}
public ListAdapter(Context context, List<String> _list) {
this.context = context;
this.list = _list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
ViewHolder holder;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row, parent, false);
holder = new ViewHolder();
holder.ivShare = (ImageView) convertView
.findViewById(R.id.iv_share);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ivShare.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return convertView;
}
}
Use ListView with CustomAdapter for doing this.
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView lv_applicationlist;
ApplicationListingAdapter adapter;
ArrayList<Integer> app_image=new ArrayList<>();
ArrayList<String> app_name=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_applicationlist = (ListView) findViewById(R.id.lv_applicationlist);
app_name.add("Camera");
app_name.add("Gallery");
app_name.add("Send");
app_name.add("Share");
app_image.add(R.drawable.ic_menu_camera);
app_image.add(R.drawable.ic_menu_gallery);
app_image.add(R.drawable.ic_menu_send);
app_image.add(R.drawable.ic_menu_share);
adapter = new ApplicationListingAdapter(MainActivity.this, app_name,app_image);
lv_applicationlist.setAdapter(adapter);
}
}
activity_main.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:orientation="vertical">
<ListView
android:id="#+id/lv_applicationlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:listSelector="#android:color/transparent" />
</LinearLayout>
ApplicationListingAdapter.java
public class ApplicationListingAdapter extends BaseAdapter {
private Activity activity;
private static LayoutInflater inflater = null;
ArrayList<String> app_name;
ArrayList<Integer> app_image;
public ApplicationListingAdapter(Activity a, ArrayList<String> application_name, ArrayList<Integer> application_image) {
activity = a;
app_name = application_name;
app_image = application_image;
inflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return app_name.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView tv_appname;
public ImageView iv_appicon;
public Switch mySwitch;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.application_list_row, parent, false);
holder = new ViewHolder();
holder.tv_appname = (TextView) vi.findViewById(R.id.tv_appname);
holder.iv_appicon = (ImageView) vi.findViewById(R.id.iv_appicon);
holder.mySwitch = (Switch) vi.findViewById(R.id.mySwitch);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.tv_appname.setText(app_name.get(position));
holder.iv_appicon.setImageResource(app_image.get(position));
if(app_name.get(position).equals("Send"))
{
holder.mySwitch.setVisibility(View.VISIBLE);
}else{
holder.mySwitch.setVisibility(View.GONE);
}
return vi;
}
}
application_list_row.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="wrap_content">
<ImageView
android:id="#+id/iv_appicon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/tv_appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="10dip"
android:layout_weight="1"
android:text="Store App"
android:textSize="20dp" />
<Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:visibility="gone"
android:layout_gravity="center_vertical" />
</LinearLayout>
Output will be look like this :

ListAdapter working incorrectly on scroll Android

I wanted to do a generic ListAdapter, where instead of inserting predefined view structures, you can add different views in each list item.
I have my ListAdapter:
public class ListAdapter extends BaseAdapter {
private HashMap<Integer, Integer> listDataTypes; //For each element of the list, defines if its an ítem or a separator
private List<View> listData;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
public ListAdapter() {
listDataTypes = new HashMap<Integer, Integer>();
listData = new ArrayList<>();
}
public void addItem(final View item) {
listDataTypes.put(listData.size(), TYPE_ITEM);
listData.add(item);
notifyDataSetChanged();
}
public void addSeparator(final View item) {
listDataTypes.put(listData.size(), TYPE_SEPARATOR);
listData.add(item);
notifyDataSetChanged();
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public int getItemViewType(int position) {
return listDataTypes.get(position);
}
#Override
public View getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
View itemView = listData.get(position);
convertView = itemView;
}
return convertView;
}
}
Here is an example of a list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/tvInfo1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvInfo2"
android:layout_toLeftOf="#+id/tvInfo3"
android:layout_toStartOf="#+id/tvInfo3"
android:layout_toEndOf="#id/tvInfo1"
android:layout_toRightOf="#id/tvInfo1"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:id="#+id/tvInfo3"
android:layout_centerVertical="true"/>
</RelativeLayout>
</LinearLayout>
And how I use the list adapter:
protected void onCreate(Bundle savedInstanceState) {
...
ListAdapter listAdapter = new ListAdapter();
TextView tvHeader = new TextView(this);
tvHeader.setText("I am a header");
listAdapter.addSeparator(tvHeader);
for(int i=0; i<50; i++){
View itemView = layoutInflater.inflate(R.layout.layout_two_items_list_item, null);
TextView tvInfo1= (TextView) itemView .findViewById(R.id.tvInfo1);
TextView tvInfo2= (TextView) itemView .findViewById(R.id.tvInfo2);
TextView tvInfo3= (TextView) itemView .findViewById(R.id.tvInfo3);
tvInfo1.setText("Id " + i);
tvInfo2.setText("Info " + i);
tvInfo3.setText("Hour " + i);
listAdapter.addItem(itemView);
}
ListView listView = (ListView)findViewById(R.id.listview);
listView.setAdapter(listAdapter);
listView.setSelector(android.R.color.transparent);
}
But for some reason, when I scroll the list, some items start to float, other are not shown, other are repeated, other changes places, etc. What am I doing wrong?
This is how is shown (incorrectly)
It is a very bad practice what you are doing. Keeping an array of views in the memory. The whole purpose of a listview to minimise that. In order to avoid the repetition replace
if (convertView == null) {
View itemView = listData.get(position);
convertView = itemView;
}
return convertView;
with
return listData.get(position);
But if I were you I would seriously consider rewriting the way you are doing your listview.
Change your getView to something like:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = listData.get(position);
return itemView;
}
I suspect that you're re-using old views when you don't really want to be.

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