I have a listview inside a fragment class. When a listview item is clicked another activity is opened. The xml file of that activity has 2 imageviews inside a scroll view. The images change with respect to every listview item clicked. No matter which listview item i click on it shows the same image view. In my case it shows the R.drawable.geet94 no mater which listview item i click.
public class urdufrag1 extends Fragment {
public urdufrag1() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
private static final String TAG = "urdufrag1";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
main_content, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.geetfrag_urdu, main_content, false);
final int[] menuImage = {R.drawable.tgeet94,
R.drawable.tgeet95,R.drawable.tgeet96,R.drawable.tgeet97};
final ListView listView = (ListView) view.findViewById(R.id.GeetListU);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getActivity(), PackageG.class);
//intent.putExtra("GeetType",
listView.getItemAtPosition(position).toString());
//intent.putExtra("GeetType", Integer.toString(position));
intent.putExtra("position", Integer.toString(position));
startActivity(intent);
}
});
AdapterGeetUrdu adapter = new AdapterGeetUrdu(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
}
Second Activity
public class PackageG extends AppCompatActivity {
ImageView img1, img2;
PhotoViewAttacher mAttacher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_package_g);
img1 = (ImageView) findViewById(R.id.imageView8);
img2 = (ImageView) findViewById(R.id.imageView9);
mAttacher = new PhotoViewAttacher(img1);
mAttacher = new PhotoViewAttacher(img2);
Intent intent = this.getIntent();
if(intent != null){
Integer position = intent.getExtras().getInt("position");
switch (position){
case 0: img1.setImageResource(R.drawable.geet94);
img2.setImageResource(R.drawable.geet94_1);
break;
case 1: img1.setImageResource(R.drawable.geet95);
img2.setImageResource(R.drawable.geet95_1);
break;
case 2: img1.setImageResource(R.drawable.geet96);
img2.setImageResource(R.drawable.geet96_1);
break;
default:
img1.setImageResource(R.drawable.carol);
img2.setImageResource(R.drawable.carol);
break;
}
}
mAttacher.update();
}
}
intent.putExtra("position", Integer.toString(position));
This is where you made mistake.
Change this to, and will solve your problem.
intent.putExtra("position", position);
Because in your activity you are expecting an integer but you are passing string position from listview. Change the click listener as per the below code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getActivity(), PackageG.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
Related
I have a listview inside a fragment class. When a listview item is clicked another activity is opened. The xml file of that activity has 2 imageviews inside a scroll view. The images change with respect to every listview item clicked. There are 5 listviews in my app. Each listview has 400+ items. What approach to use so that it runs smoothly. Is switch statement a good idea? Currently i am using a switch statement and there are only 4 items so far. Should i keep this approach?
My code
public class urdufrag1 extends Fragment {
public urdufrag1() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
private static final String TAG = "urdufrag1";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
main_content, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.geetfrag_urdu, main_content, false);
final int[] menuImage = {R.drawable.tgeet94,
R.drawable.tgeet95,R.drawable.tgeet96,R.drawable.tgeet97};
final ListView listView = (ListView) view.findViewById(R.id.GeetListU);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getActivity(), PackageG.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
AdapterGeetUrdu adapter = new AdapterGeetUrdu(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
}
Second Activity
public class PackageG extends AppCompatActivity {
ImageView img1, img2;
PhotoViewAttacher mAttacher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_package_g);
img1 = (ImageView) findViewById(R.id.imageView8);
img2 = (ImageView) findViewById(R.id.imageView9);
mAttacher = new PhotoViewAttacher(img1);
mAttacher = new PhotoViewAttacher(img2);
Intent intent = this.getIntent();
if(intent != null){
Integer position = intent.getExtras().getInt("position");
switch (position){
case 0: img1.setImageResource(R.drawable.geet94);
img2.setImageResource(R.drawable.geet94_1);
break;
case 1: img1.setImageResource(R.drawable.geet95);
img2.setImageResource(R.drawable.geet95_1);
break;
case 2: img1.setImageResource(R.drawable.geet96);
img2.setImageResource(R.drawable.geet96_1);
break;
default:
img1.setImageResource(R.drawable.carol);
img2.setImageResource(R.drawable.carol);
break;
}
}
mAttacher.update();
}
}
My list view
Second Activity
This is the perfect job for a TypedArray. Create a file res/values/arrays.xml with something like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="firstImages">
<item>#drawable/R.drawable.geet94</item>
<item>#drawable/R.drawable.geet95</item>
<item>#drawable/R.drawable.geet96</item>
...
</array>
<array name="secondImages">
<item>#drawable/R.drawable.geet94_1</item>
<item>#drawable/R.drawable.geet95_1</item>
<item>#drawable/R.drawable.geet96_1</item>
...
</array>
</resources>
and then in your second Activity:
Intent intent = this.getIntent();
if(intent != null){
Integer position = intent.getExtras().getInt("position");
Resources res = getResources();
TypedArray firstImages = res.obtainTypedArray(R.array.firstImages);
TypedArray secondImages = res.obtainTypedArray(R.array.secondImages);
Drawable firstDrawable = firstImages.getDrawable(position);
Drawable secondDrawable = secondImages.getDrawable(position);
img1.setImageDrawable(firstDrawable);
img2.setImageDrawable(secondDrawable);
}
I am using BackendLess backend service, but my prob is (i guess) more to android/java. So even if u are not familiar with BackendLess, i guess u can help, if u know of course :)
I have there a Fragment that calls and opens a DialogFragment with a ListView.
Using there an iterator to retrieve the data. It brings each column from the data table as an Array.
I set an onClickedItemListener that when item is clicked, it send the value to a TextView in the Fragment it was called from.
The data comes in the wrong order - didnt get how to do a sortBy, that connects to the bigger prob i have there -
There is a column there named "PropertyTypes". It holds 4 strings, which are coming out in the opposite order that i need. I want the "A" first, and get:
"D"
"C"
"B"
"A"
ok, so far no big deal, i guess can be sorted out with a sortBy that i just dont know how to do.
But... what happens is that it sends the wrong value to the TextView, meaning, for example, when i press "C" it set "A" on the TextView and so on, and, when i press the last one, in this case "A", the app is crashing...
What the hell is going on there?? :))
Here is the code -
The DialogFragment code:
public class OptionDialogFragment extends DialogFragment implements
AdapterView.OnItemClickListener {
ListView mylist;
TextView chosenProperty;
TextView presentListItem;
ArrayAdapter adapter;
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//mylist.addHeaderView(inflater.inflate(R.layout.option_dialog_header, null, false));
View view = inflater.inflate(R.layout.option_dialog_content, null, false);
mylist = (ListView) view.findViewById(R.id.list);
View headerView = inflater.inflate(R.layout.option_dialog_header, mylist, false);
headerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
mylist.addHeaderView(headerView);
View footerView = inflater.inflate(R.layout.option_dialog_footer, mylist, false);
footerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
mylist.addFooterView(footerView);
chosenProperty = (TextView) view.findViewById(R.id.chosenProperty);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ArrayList<String> propertyTypes = new ArrayList<String>();
final ArrayList<Integer> numOfRoomies = new ArrayList<Integer>();
Backendless.Data.of(DialogOptions.class).find(new AsyncCallback<BackendlessCollection<DialogOptions>>() {
#Override
public void handleResponse(final BackendlessCollection<DialogOptions> dialogOptions) {
final Iterator<DialogOptions> iterator = dialogOptions.getCurrentPage().iterator();
while (iterator.hasNext()) {
DialogOptions dialogOptionsObject = iterator.next();
propertyTypes.add(dialogOptionsObject.getPropertyTypes());
// numOfRoomies.add( dialogOptionsObject.getNumOfRoomies() );
}
adapter = new ArrayAdapter<String>(getActivity(), R.layout.dialog_option_list_item, R.id.presentListItem, propertyTypes);
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String chosenItem = propertyTypes.get(position);
Intent intent = new Intent();
intent.putExtra("chosenItem", chosenItem);
getTargetFragment().onActivityResult(
getTargetRequestCode(), Activity.RESULT_OK, intent);
dismiss();
}
});
}
#Override
public void handleFault(BackendlessFault fault) {
// TODO: make sure to log the exception, just in case
}
});
}
}
This is the Relevant code in the Fragment that calls the DialogFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.find_a_place, container, false);
chosenProperty = (TextView) view.findViewById(R.id.chosenProperty);
return view;
}
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
final LinearLayout propertTypes = (LinearLayout)view.findViewById(R.id.propertyTypes);
propertTypes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(getActivity(), "OptionDialog");
}
});
}
private void showDialog(FragmentActivity activity, String optionDialog) {
android.support.v4.app.FragmentManager manager = getFragmentManager();
DialogFragment dialog = new OptionDialogFragment();
dialog.setTargetFragment(this, 0);
dialog.show(manager, "OptionDialog");
dialog.setCancelable(true);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
if(data!=null){
// set value to your TextView
chosenProperty.setText(data.getStringExtra("chosenItem"));
}
}
break;
}
}
Thanks a lot in advance for any answer!!
Reference :-
https://backendless.com/feature-47-loading-data-objects-from-server-with-sorting/
To Sort while retrieving Object use :-
QueryOptions queryOptions = new QueryOptions();
queryOptions.addSortByOption( "created ASC" );
dataQuery.setQueryOptions( queryOptions );
Use Query as below:-
// fetch restaurants
Backendless.Data.of( Restaurant.class ).find( dataQuery, new AsyncCallback<BackendlessCollection<Restaurant>>(){
So I have two fragments linking to layout files which display ListViews. The ListViews are defined in the xml and have entries from a string array. I want to click on items in the ListView and open new activities. There are 8 items in one ListView and 9 in the other. In the onItemClick code, how do I create intents to start activities based on the item clicked? I will create 1 class per item as its own activity. How can I start the activities in the classes via intents inside the onItemClick methods of this code?
class CommunityFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.community_fragment, container, false);
ListView lv = (ListView) view.findViewById(R.id.communityListView);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
// TODO Auto-generated method stub
}
});
return view;
}
}
class ResourcesFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.resources_fragment, container, false);
ListView lv = (ListView) view.findViewById(R.id.resourcesListView);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
return view;
}
}
Implement your OnItemClickListener() like below
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), nextactivity.class);
startActivity(intent);
}
});
Make switch statement for each items click and open activities accordingly as below:
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
int itm=arg0.getItemAtPosition(arg2);
switch (itm) {
case 0:
Toast.makeText(m_context, "Position Zero", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), FirstActivity.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new Intent(getActivity(), SecondActivity.class);
startActivity(intent1);
break;
case 2:
//..............................
}
});
on Item Click you will get Position based on position you can start fragment
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
switch(position) {
case CONST_FRAGMENT_1 :
//Start fragment 1
...
...
}
}
});
one Generic solution can be ..
Create an array of items that hold class name of activity you want to open ..
like ..
Class[] activityArray = new Class[numberOfItemsInListView];
activityArray[0] = Activity1.class;
//add all activities like that..............
now on ListView onItemCLick :
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long arg3) {
Intent intent = new Intent(CommunityFragment.this.getActivity(), activityArray[postion]);
CommunityFragment.this.getActivity.startActivity(intent);
}
});
Use this to start next intent in your onItemClickListener:
Intent intent = new Intent(getActivity(), nextactivity.class);
startActivity(intent);
I think following code help you.
public class PdfListViewFragment extends Fragment {
ListView listView;
Activity rootView;
Activity context;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
View rootView = inflater.inflate(R.layout.pdf_list_view, container, false);
context = getActivity();
// Get ListView object from xml
listView = (ListView) rootView.findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[]{"Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 5) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
if (position == 6) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity1.class);
startActivityForResult(myIntent, 0);
}
if (position == 7) {
Intent myIntent = new Intent(view.getContext(), ListItemActivity2.class);
startActivityForResult(myIntent, 0);
}
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(context.getApplicationContext(), "Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("XYZ");
}
}
If you use an adapter to display the items in a list, it is important to differentiate the following:
The ID of the list AdapterView#getId() is different than the ID of the items in the list ArrayAdapter<String>#getId(), because the view of the list contains the views of the elements.
Suppose an example where you are going to launch activities with user roles. You will have to do the transformation for your particular case.
public class SignInFragment extends Fragment implements AdapterView.OnItemClickListener {
//TODO: Declare constants (GUEST, HOST, EMPLOYEE...)
private ArrayAdapter<String> userRolesAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_sign_in, container, false);
// ListView Instance
ListView userRolesList = root.findViewById(R.id.user_roles_list);
String[] userRoles = {
GUEST,
HOST,
EMPLOYEE
};
// Initialize the adapter
userRolesAdapter = new ArrayAdapter<>(
getActivity(),
android.R.layout.simple_list_item_1,
userRoles
);
// Link to the list with the adapter. This reference starts the process of filling the list.
userRolesList.setAdapter(userRolesAdapter);
// Events
userRolesList.setOnItemClickListener(this);
return root;
}
/**
* #param adapterView: View using the adapter from the list
* #param view: View of the item that has been pressed
* #param i: Refers to the position of the item that the adapter handles
*/
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.user_roles_list) {
// Obtain the item pressed on the adapter with the entry position
String currentUserRol = userRolesAdapter.getItem(i);
assert currentUserRol != null;
switch (currentUserRol) {
case GUEST:
startActivity(Host.class);
break;
case HOST:
startActivity(Host.class);
break;
case EMPLOYEE:
startActivity(Employee.class);
break;
default:
Log.d("Error", "The activity passed as an argument to startActivity() does not exist");
break;
}
}
}
/**
* PRECONDITION: The class given as an argument exists.
*/
public void startActivity(Class<?> cls) {
Intent intent = new Intent(getActivity(), cls);
startActivity(intent);
}
}
Best :)
I have a gridView with imageViews in one Activity and I want to load one of them in other activity on a new ImageView when select it, but i have no results
Activity 1:
public class Level extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level);
GridView gridview = (GridView) findViewById(R.id.gridviewLevel);
ImageAdapter ia = new ImageAdapter(this);
gridview.setAdapter(ia);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Level.this, "" + position, Toast.LENGTH_SHORT).show();
initImageCheck( v.getId());
}
});
}
public void initImageCheck(int id){
Intent intentIC = new Intent(this, ImageCheck.class);
intentIC.putExtra("ID", id);
startActivity(intentIC);
}
}
Activity 2:
public class ImageCheck extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_check);
Bundle extras = getIntent().getExtras();
int id = extras.getInt("ID");
ImageView imgView = (ImageView) findViewById(R.id.imageViewCheck);
imgView.setImageResource(id);
}
}
Whats the problem??? Thanks.
v.getId() is the ID of the View, not of the image resource.
Have ImageAdapter also hold the id of resource used per item, and also provide a method like getResourceIdForItem(int position).
I have created a custom listview using SimpleAdapter and in each row of list view i put a button has single id.
I want to get the position of each row to pass the button but i have a single button id for each row and I want when I click on button it find the position of row and start another activity
please help me
public void click(View v){
//RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
TextView tv = (TextView)findViewById(R.id.text1);
ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
ListView lv=(ListView)findViewById(android.R.id.list);
int position = 0;
Long id=Long.parseLong((String) adapter.getItem(position));
Intent i=null;
switch(position){
case 1:
i=new Intent(this, ButtonActivity.class);
startActivity(i);
break;
case 2:
i = new Intent(this, PickerActivity.class);
startActivity(i);
break;
}
try this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
final ProgressDialog progressBar = ProgressDialog.show(
VechiclesDetails.this, "", loadingString);
progressBar.setIndeterminate(true);
progressBar.setIndeterminateDrawable(getResources()
.getDrawable(R.anim.progressbar_handler));
new Thread(new Runnable() {
public void run() {
try {
Intent carDetail = new Intent(
Details.this,
Screen.class);
carDetail.putExtra("index", arg2);
carDetail.putExtra("sellerId", SellerId);
startActivity(carDetail);
progressBar.dismiss();
} catch (Exception e) {
progressBar.dismiss();
}
}
}).start();
}
});
I am not sure about this, but lets try it, and inform me about your success..
The line , int position = listView.getPositionForView(v.getParent());
public void click(View v){
// This line is important
int position = listView.getPositionForView(v.getParent());
//RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
TextView tv = (TextView)findViewById(R.id.text1);
ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
ListView lv=(ListView)findViewById(android.R.id.list);
int position = 0;
Long id=Long.parseLong((String) adapter.getItem(position));
Intent i=null;
switch(position){
case 1:
i=new Intent(this, ButtonActivity.class);
startActivity(i);
break;
case 2:
i = new Intent(this, PickerActivity.class);
startActivity(i);
break;
}
}
If this not works then only option you have to is making a Custom Adapter and write your button's onClick() in adapter's getView() in which you have a position variable accessible.
On list view, you can use setOnItemClickListener
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView, int position, long id) {
//Put Your code here
}
public void onNothingSelected(AdapterView parentView) {
}
});