findViewById returns null on second call - android

I've created a ListView with a list of items, each with their own delete button.
I've overridden the getView method in the ArrayAdapter and attached a handler to the delete button.
The problem is, the getView method is being called twice. On the second time, the v.findViewById(DeleteButton) call returns null. I've inspected the view and the ImageButton is there in the hierarchy, but the Id is 0.
Can anyone explain why this is happening please?
ManualFragment.java
public class ManualFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.fragment_manual, container, false);
Button addButton = (Button) view.findViewById(R.id.AddButton);
ListView listView = (ListView) view.findViewById(R.id.ListView);
final List<String> items = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), R.layout.list_item, R.id.TextItem, items) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) ManualFragment.this.getLayoutInflater(savedInstanceState);
v = vi.inflate(R.layout.list_item, null);
}
TextView time = (TextView) v.findViewById(R.id.TextItem);
time.setText(items.get(position));
ImageButton delete = (ImageButton) v
.findViewById(R.id.DeleteButton);
//THIS LINE THROWS A NULLREF ON SECOND CALL
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageButton deleteButton = (ImageButton) v;
items.remove(deleteButton.getId());
notifyDataSetChanged();
}
});
delete.setId(position);
return v;
}
};
listView.setAdapter(adapter);
final EditText addText = (EditText) view.findViewById(R.id.AddText);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String value = addText.getText().toString();
if (!"".equals(value)) {
items.add(value);
adapter.notifyDataSetChanged();
addText.setText("");
}
}
});
return view;
}
}
list_item.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="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusable="false" >
<TextView
android:id="#+id/TextItem"
android:layout_width="267dp"
android:layout_height="match_parent"
android:layout_weight="0.89"
android:focusable="false"
android:gravity="center_vertical"
android:text="Item"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="#+id/DeleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:focusable="false"
android:src="#drawable/delete" />
</LinearLayout>
</LinearLayout>

You're setting it with
delete.setId(position)
I think you wanted to tag the buttojn with the position
delete.setTag(position)

Related

GridView buttons not working correctly

I have a GridView and am using a BaseAdapter to populate the values for each grid item. I am programmatically adding the buttons in each grid item using this adapter. The problem is, I have a total of SEVEN buttons. After each button click, the button is disabled so can only be clicked once.
So when the second button is clicked, the 7th button stops working. And if the 7th button is clicked, the first button stops working. The buttons are working fine and function properly apart from this.
EDIT: It seems like scrolling through the gridview (since the elements inside the gridview are large is causing the problem.)
Here is the relevant code.
MyAdapter.java
import static com.example.tuss.mafia.R.layout.layout_grid_item;
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Players> playerList;
private static LayoutInflater inflater = null;
public MyAdapter(Context context, ArrayList<Players> playerList){
this.context = context;
this.playerList = playerList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return playerList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(layout_grid_item, null);
final Button button = (Button) convertView.findViewById(R.id.btnRoleReveal);
TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
final TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);
Players p;
p = playerList.get(position);
idTextView.setText("ID: " + String.valueOf(p.getId()));
nameTextView.setText(String.valueOf(p.getName()));
roleTextView.setText("Your Role Is: " + String.valueOf(p.getRole())+ ", you won't be able to view this again.");
final String role = roleTextView.getText().toString();
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
new AlertDialog.Builder(context)
.setTitle("Remember your role and keep it a secret!")
.setMessage(role)
.create()
.show();
button.setEnabled(false);
button.setText("Role has been viewed");
return true;
}
});
return convertView;
}
}
The XML for the gridview, fragment_viewroles.xml
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tuss.mafia.GameActivity" >
<GridView
android:id="#+id/gv_players"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:stretchMode="columnWidth"
android:nestedScrollingEnabled="true"
android:columnWidth="150dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:textAlignment="center"
android:gravity="center">
</GridView>
</RelativeLayout>
layout_grid_item.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:background="#drawable/customborder"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_player_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/white"
android:visibility="gone"
android:text="ID" />
<TextView
android:id="#+id/tv_player_name"
android:textAlignment="center"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/holo_blue_bright"
android:text=""
android:textStyle="normal|bold" />
<TextView
android:id="#+id/tv_player_role"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:clickable="true"
android:text="role"
android:visibility="invisible">
</TextView>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hold to reveal your role"
android:id="#+id/btnRoleReveal"
/>
RolesFragment.java
public RolesFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewroles, container, false);
gridView = (GridView) view.findViewById(R.id.gv_players);
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
playersList = new ArrayList<Players>();
playersList = databaseHelper.getPlayers();
adapter = new MyAdapter(getActivity(), playersList);
gridView.setAdapter(adapter);
return view;
}
replace button.setEnabled(false); with ((Button)v).setEnabled(false);
I think the problem is in your button event.
Just Initialize button event once
if (convertView == null){
convertView = inflater.inflate(layout_grid_item, null);
Button button = (Button) convertView.findViewById(R.id.btnRoleReveal);
button.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Button btn = (Button)v;
new AlertDialog.Builder(context)
.setTitle("Remember your role and keep it a secret!")
.setMessage(role)
.create()
.show();
btn.setEnabled(false);
btn.setText("Role has been viewed");
return true;
}
});
}
Check this KodeCenter example

How to access listview items generated by ArrayAdapter by CLICKING ON BUTTON

I'm using a ListView with ArrayAdapter. There is a Button along with TextView in the listview.xml. I want to retrieve data from that TextView by Clicking over corresponding buttons.
My listview.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="90dp"
android:id="#+id/btConfirmfriend"
android:text="Confirm Friend"
/>
</RelativeLayout>
My PendingRequest Class is as follows:
public class PendingRequest extends AddFriend {
TextView tvPending;
Button btConfirmfriend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pendingrequest);
final String[] s= getIntent().getExtras().getStringArray("pending");
System.out.println(s[1]);
final ListAdapter adapter = new ArrayAdapter<String>(this,R.layout.listview,R.id.label,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
final View inflatedView = getLayoutInflater().inflate(R.layout.listview, null);
btConfirmfriend = (Button) inflatedView.findViewById(R.id.btConfirmfriend);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ServerConnection sc = new ServerConnection();
//here I want to retrieve the value from List
//System.out.println(s[adapter.]);
int status = sc.confirmRequest(s[listView.getSelectedItemPosition()]);
AlertDialog.Builder alert = new AlertDialog.Builder(PendingRequest.this);
if (status == 1) {
alert.setMessage("Friend has been Added");
alert.show();
} else {
alert.setMessage("Sorry for inconvinient!!Try Again");
alert.show();
}
}
});
}
}
My pendingrequest.xml file is:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#drawable/background"
android:layout_height="match_parent">
<Button
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#e9abab"
android:id="#+id/btSend"
android:text="Pending Requests"
/>
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
On clicking over the button nothing happening.onClick() method is not executing.Please help.
Since you need a click listener on a view inside your list item, you need to write your own adapter for that -
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, String[] list) {
super(context, 0, list);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Lookup view for data population
TextView label = (TextView) convertView.findViewById(R.id.label);
Button btConfirmfriend = (Button) convertView.findViewById(R.id.btConfirmfriend);
// Populate the data into the template view using the data object
label.setText(item);
btConfirmfriend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getItem(position); //Get your selected text
}
});
return convertView;
}
}
And this is how you will set the adapter -
final CustomAdapter adapter = new CustomAdapter(this,s);
final ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);

Listview with button OnItemClickListener is not calling

I create a custom listview with a TextView and 2 ImageButton. But onitemclicklistner is not calling. Here is my code
police_station_list = (ListView) findViewById(R.id.police_station_listView_xml);
adapter = new ArrayAdapter<String>(this, R.layout.coustome_listview, R.id.district_listview_xml1, police_station_name);
PSAdapter = new Police_Station_Adapter(this);
police_station_list.setAdapter(PSAdapter);
police_station_list.setOnItemClickListener(PSAdapter);
Here is my custom ArrayAdapter class:
public class Police_Station_Adapter extends ArrayAdapter<String> implements OnItemClickListener{
Context context;
public Police_Station_Adapter(Context context) {
//super(context, R.layout.police_station_listview, R.id.police_station_listView_textView, police_station_name);
super(context, R.layout.police_station_listview, police_station_name);
this.context = context;
}
private class ViewHolder {
TextView tv;
ImageButton mobile, phone;
public ViewHolder(View v) {
tv = (TextView) v.findViewById(R.id.police_station_listView_textView);
mobile = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_mobile);
phone = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_phone);
}
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.police_station_listview, parent, false);
holder = new ViewHolder(v);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
holder.tv.setText(police_station_name[position]);
holder.mobile.setTag(position);
holder.mobile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Message(context, police_station_name[(Integer) v.getTag()]+" button", 500);
}
});
return v;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
new Message(context, "Item Clicked", 500); //Toast message
}
}
Here is the custom ListView 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="40dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/police_station_listView_textView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="8"
android:textSize="16sp"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_mobile"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="15dp"
android:background="#drawable/mobile_icon" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_phone"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="#drawable/telephone_icon" />
</LinearLayout>
it's seems like i can add onClikcListner for textview but it doesn't show any pressed animation. It may not be the appropriate solution. I need the proper way to setOnItemClickListner.
You need to set OnItemClickListener in your Activity. then change this
police_station_list.setOnItemClickListener(PSAdapter); to
police_station_list.setOnItemClickListener(this);
Remove implementation of OnItemClickListener from adapter.
Check this tutorial.
add this attribute to imageButton:
android:focusable="false"

How to registre clicks on Listview with Custom Arrayadpter inside Fragment

I got a custom ArrayAdapter:
public class EditArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
public ArrayList<String> strArrC;
public ArrayList<String> strArrT;
public EditArrayAdapter(Activity context, ArrayList<String> web,
ArrayList<String> imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.strArrC = web;
this.strArrT = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_singlefirst, null, true);
EditText txtTitle = (EditText) rowView
.findViewById(R.id.singelfirstet1);
txtTitle.setText(strArrC.get(position));
return rowView;
}
}
with this as the list_singlefirst:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="100" >
<ImageButton
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_photo" />
<ImageButton
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:contentDescription="#string/button_delete_row_description"
android:src="#drawable/testi" />
<EditText
android:id="#+id/singelfirstet1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/buttonDelete"
android:layout_toRightOf="#+id/imageView1"
android:ems="10" >
</EditText>
</RelativeLayout>
The onCreateView on my Fragment looks like:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.editlistsfirst, container, false);
list2 = (ListView) mLinearLayout.findViewById(R.id.editlistsfirstlv);
count = new ArrayList<String>();
operations = new ArrayList<String>();
count = retrievecounter(1);
operations = retrievecounter(0);
EditArrayAdapter adapter = new EditArrayAdapter(this.getActivity(), operations, count);
list2.setAdapter(adapter);
list2.setOnItemClickListener(this);
adapter.notifyDataSetChanged();
return mLinearLayout;
}
The Fragment is inside the MainActivity.
This is my first App so i'm not too knowledgeable on the subject.
What I want to do is create a listview with my rows which contain 2 imagebuttons and a editText (which i was able to do) and depending on which button I click one should show a Toast with the text from the edittext and the other should delete the text within the edittext.
I tried it with setonitemclicklistener in the Fragment but this didn't really work.
So the Question is how do I implement the different Clicklistener and distinguish between what Button on which Row has been pressed?
Thanks in advance.
You need to set OnClickListeners for each button in your getView() method:
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_singlefirst, null, true);
EditText txtTitle = (EditText) rowView
.findViewById(R.id.singelfirstet1);
txtTitle.setText(strArrC.get(position));
ImageButton deleteButton = (ImageButton) rowView.findViewById(R.id.buttonDelete);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something here
}
}
return rowView;
}
Apply OnClickListener inside getView() method. And apply what ever task you want to do.
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_singlefirst, null, true);
EditText txtTitle = (EditText) rowView
.findViewById(R.id.singelfirstet1);
txtTitle.setText(strArrC.get(position));
Button button = (Button) view.findViewById(R.id.yourbutton);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do something. May be call function based on text from edittext or anything else
}
});
return rowView;
}

get position of selected button in listview

I have a list view every item in list contains textviews and button what I want is when I click on the button of any item in list I want to printout the position of button if i clicked the button in first line i want to print out 0 and go on but it doesn't work
this is my method to display view
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
PhonesDbAdapter.KEY_NAME,
PhonesDbAdapter.KEY_CONTINENT,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.continent,
R.id.name
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.phone_layout,
cursor,
columns,
to,
0);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
public void print(View v)
{
}
and here how my item look like
<?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"
android:background="#f0f0f0" >
<TextView
android:id="#+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#275a0d"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView"
android:textColor="#000"/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/call"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1"
android:background="#drawable/ic_launcher"
android:onClick="print"/>
</RelativeLayout>
and this the layout that contain the listview
<?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"
android:background="#f0f0f0">
<EditText android:id="#+id/myFilter" android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10">
</EditText>
<ListView android:id="#+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
i spent alot of time with this problem and i hope that u can help me and im really sorry for my bad english
Are you implementing the method getView() on your adapter?
That's where you want to add the OnClickListener to your Button
Once you do that you can set the position as the tag of the Button and retrieve it on the onClick method.
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.your_item_layout, parent, false);
Button myButton = (Button) row.findViewById(R.id. call);
myButton.setOnClickListener(new OnClickListener {
#Override
public void onClick(View view) {
int position = (int)view.getTag();
//Do whatever you want with the position here
}
});
myButton.setTag(position);
return row;
}
From last 3 three years I am not working on android so I am not sure my suggestion is correct or not. You need to test it for you purpose.
In your above case you cannot use onItemClickListner because it can only be used when you are tracking your complete list row. What I think is you can create a custom adapter and override its getView method. In getView method you will have your position and you can set button click listner for you button in getview. So In this way you can get your button position.
You try it once if you need some code then I can try to write it for you...
THIS IS HOW YOU CAN CREATE CUSTOM ADAPTER.
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
ArrayList<String> elements = null;
public my_custom_adapter(Context context, int type, ArrayList<String> elements)
{
super(context, type, elements);
this.elements = elements;
this.context = context;
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public Button cb;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (Button) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// IF YOUR BUTTON TAG HERE AND YOU CAN HAVE POSITION USING "position" PARAMETER
}
});
}
return rowView;
}
}

Categories

Resources