0

I have an list view with image and link functionaliy also,now what i need is i want to create an button in each item in an list view,if i click the buttons it has to go another activity, in that activity page it has to open an xml file respectively of clicking the button,i will hard code the details of xml file preiously in an list view java file,how to do that one.

public class SouthIndianvegrecipes  extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        ArrayList<Recipedetails1> image_details = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.listV1_main);
        lv1.setAdapter(new ItemListBaseAdapter1(this, image_details));

        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv1.getItemAtPosition(position);
                Recipedetails1 obj_itemDetails = (Recipedetails1)o;
                Toast.makeText(SouthIndianvegrecipes.this, "You have chosen : " + " " + obj_itemDetails.getName1(), Toast.LENGTH_LONG).show();


            } 
        });
    }

    private ArrayList<Recipedetails1> GetSearchResults(){
        ArrayList<Recipedetails1> results = new ArrayList<Recipedetails1>();

        Recipedetails1 item_details = new Recipedetails1();
        item_details.setName1("Vegterian");
        item_details.setItemDescription1("Recipes made by raw materials");
        item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber1(1);
        results.add(item_details);

        item_details = new Recipedetails1();
        item_details.setName1("Non-Vegterian");
        item_details.setItemDescription1("Flesh of sweet animals");
        item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber1(2);
        results.add(item_details);

        item_details = new Recipedetails1();
        item_details.setName1("Pickels");
        item_details.setItemDescription1("Touchable dish  by Homemade");
        item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber1(3);
        results.add(item_details);

        item_details = new Recipedetails1();
        item_details.setName1("Soups");
        item_details.setItemDescription1("Startup for our food");
        item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
        item_details.setImageNumber1(4);
        results.add(item_details);




        return results;
    }

}

kkarthickk
  • 99
  • 4
  • 13
  • I don't understand what the question is... – codeMagic Mar 07 '13 at 03:39
  • in my listview i will create button for all item,if i click the button in an list view,it has to open an activity,and respective of their click it has to show their content – kkarthickk Mar 07 '13 at 03:43
  • @user2118898 you need to have a custom adapter to populate listview try searching in google there are lot of tutorials – Pragnani Mar 07 '13 at 05:00

3 Answers3

1

You just need to setOnClickListener for the button in getView() method of ItemListBaseAdapter1 like this.

    class ItemListBaseAdapter1 extends BaseAdapter
    {

    private Activity mActivity;
    public ItemListBaseAdapter1(Activity activity, ...)
    {
        mActivity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO: get view here
        final Recipedetails1 item = (Recipedetails1) getItem(position);
        theButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(mActivity, [your activity class]);
                intent.putExtra("The Link Key", item.getUrlWiki());
                mActivity.startActivity(intent);
            }
        });
        return view;
    }

}

And on the target Activity you get link by this:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    String link = getIntent.getString("The Link Key", null);
}
Tale
  • 76
  • 3
0

you need to add android:descendantFocusability="blocksDescendants"

I just found solution from here

https://stackoverflow.com/a/14372750/1136023

Use this to pass your contains intent.putExtra("TAG", Data);

Community
  • 1
  • 1
Umesh Lakhani
  • 2,382
  • 1
  • 14
  • 10
  • ,thank you,can you tell me how to use the button functionailty in list view,if i click the first button in an listview of item1,it has to go activity,but its contents is depend upon the button in an listview,if i click the second button in an listview it has to go same activity,but its content should be different,but same layout only – kkarthickk Mar 07 '13 at 03:51
0

As you said, you have a button in a list item of list. as u know on button click listener u got view in method of onclicklistener

new OnClickListener()
{
@Override
public void onClick(View view)
{}
}

here view has a method of getparent() which returns you a view in which your button is set. you can check it by printing in log. if u got parent of button then u can also get all child views of parent in which 1 child view is button. from all child views u can get there values to send them in next activity. if button's parent has another parent in view of list item then u can also get parent of parent view by calling a getparent() method.

Please keep in mind that views have parent child relation so if you wants all child then you have to get parent.

vikrant kumar
  • 208
  • 2
  • 14