2
<Button 
android:text="More Info" 
android:id="@+id/btninfo" 
android:layout_width="wrap_content" 
android:layout_height="35dp"
android:focusable="false" 
android:focusableInTouchMode="false" 
android:onClick="myClickHandler"   
>


// Listens to user selecting on an item/row

list.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {
Hashtable<String,String> listOfData = new Hashtable<String,String>();

/* Retrieve the data of the particular item selected based on the position of the item
* and pass it to the next activity
*/
listOfData = listData.get(position);

Intent detailedView = new Intent();
detailedView.setClassName("com.", "com.");
detailedView.putExtra("listOfData", listOfData);
startActivity(detailedView);


 }
});

};

This works fine as I'm able to click on the row which intents into another activity.

public void myClickHandler(View view){


String info = listData.get(position).get("coverImage");

Intent infoIntent = new Intent(android.content.Intent.ACTION_VIEW);
infoIntent.setData(Uri.parse("http://......="  + info + "......."));
startActivity(infoIntent);

}   

The problem starts here. When I click on the button in the first list view row, it intents to the correct web view for the particular item in the position. But when I click on the rest of the buttons in the list view rows it keeps returning the same webview intended only for the first position in the list view. Hmm, i want the buttons on the different rows to intent into different urls(intent to webview) according to their position in the list view. My guess the problem lies in the application not being able to detect the position?

I'm still a newbie with android and have been trying for days~ So any help given is much appreciated! Thanks in advance! Sorry if the way my question is phrased seemed confusing~

// What I've declared in a Adapter~

 public Object getItem(int position) {
    return position;
 }

 public long getItemId(int position) {
    return position;
 }
  • 1
    where is your myClickHandler defined and how have you linked it to ListView click.. post some understandable code.. so that we can help,.. – ngesh Jul 29 '11 at 09:26
  • 1
    As you show it, `myClickHandler()` accesses an undeclared variable named `position`. I assume that you are compiling OK, and I assume that you intend for the `position` variable to relate to the same-named `onItemClick()` parameter. So... where are you declaring and setting `position`? – cdhabecker Jul 29 '11 at 15:24
  • @cdhabecker I'm trying to get or sort of reuse the int position from(AdapterView> parentView, View view, int position, long id) { Hashtable listOfData = new Hashtable(); – ohthatsjuliet Aug 01 '11 at 05:31
  • I'm going with the answer by @Dharmendra: you aren't saving `position` in `onItemClick()`, so `myClickHandler()` is using 0. – cdhabecker Aug 01 '11 at 05:45

1 Answers1

3

position variable in myClickHandler() is undeclared and not varies.so default it is giving you 0 position and you are getting first index data

If you want to reuse this then take position variable global and update the value in onItemClick event of listview

But you have to use Custom-adapter for handle click event of each button on a row of list-view and in getView() method of adapter you can handle click event.

References

Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129