3

I have the following code: and a list appears on my fragment. How can i use the list items as buttons so when i click on them to change the page from the ViewFlipper?
I tried using a variable (changepage) witch is initialised with -1. and to get the position of the adapter into it. so that on click to check how much it is, and to change the displaychildren function of the ViewFlipper

package com.weinco.fragments;

import java.util.ArrayList;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.weinco.R;


public class VinocardFragment extends Fragment {
int mNum;
private ListView menuItemList;
private ListViewAdapter listViewAdapter;
public static String text;
public static ViewFlipper VF;
public static ListView listViewVino;
public static final ArrayList<String> ListviewContent = new ArrayList<String>();
public static int changepage=-1;

/**
 * Create a new instance of CountingFragment, providing "num" as an
 * argument.
 */
static VinocardFragment newInstance(int num) {
    VinocardFragment f = new VinocardFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

// ListView

public static class ListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ListViewAdapter(Context context) {

        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return ListviewContent.size();
    }

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

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ListContent holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listviewinflate, null);

            holder = new ListContent();
            holder.text = (TextView) convertView
                    .findViewById(R.id.TextView01);


            convertView.setTag(holder);
        } else {

            holder = (ListContent) convertView.getTag();
        }

        holder.text.setText(ListviewContent.get(position));
        return convertView;
    }

    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
        if (position==0){
            changepage=0;
        }
    }

    class ListContent {
        TextView text;

    }
}

/**
 * When creating, retrieve this instance's number from its arguments.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNum = getArguments() != null ? getArguments().getInt("num") : 1;

}

/**
 * The Fragment's UI is just a simple text view showing its instance number.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.vinocard, container, false);
    VF = (ViewFlipper) v.findViewById(R.id.viewFlipperVino);

    OnClickListener anmeldenListener;
    OnClickListener zuruckListener;
    OnClickListener zuruckListener2;

    // Variables
    ImageButton anmeldenB = (ImageButton) v.findViewById(R.id.anmelden);
    ImageButton backB = (ImageButton) v.findViewById(R.id.backVinoNr);
    ImageButton backB2 = (ImageButton) v.findViewById(R.id.backVinomenu);
    menuItemList = (ListView) v.findViewById(R.id.listViewVino);
    EditText vE = (EditText) v.findViewById(R.id.vinonr);
    text = vE.getText().toString();
    getMenuItems();




    // Onclick Listeners for buttons
    anmeldenListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            VF.showNext();

            menuItemList.setAdapter(listViewAdapter);

        }

    };

    if (changepage==0){
        VF.showNext();

    }


    zuruckListener = new OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };
    zuruckListener2 = new OnClickListener() {
        @Override
        public void onClick(View v) {
            VF.showPrevious();
        }
    };

    //Get Item ID's to pages




    anmeldenB.setOnClickListener(anmeldenListener);
    backB.setOnClickListener(zuruckListener);
    backB2.setOnClickListener(zuruckListener2);
    return v;
}

private void getMenuItems() {
     ListviewContent.add("Exklusivangebote");
     ListviewContent.add("Gutschein schneken");
     ListviewContent.add("Kundendaten");
     ListviewContent.add("Meine Gutscheine");
     ListviewContent.add("Jahresbonus");
     listViewAdapter = new ListViewAdapter(getActivity());

}
}
rosu alin
  • 5,674
  • 11
  • 69
  • 150

2 Answers2

2

You can extend ListFragment as opposed to Fragment and implement onListItemClick(). From there, you can get the id of the ListView item clicked and do whatever you will with it.

To implement onListItemClick(),

  1. Right-click your codespace.
  2. Mouse-over "Source"
  3. Click "Override/Implement Methods"
  4. Find and check onListItemClick(ListView l, View v, int position, long id)
  5. Click "OK" to implement checked methods.

Find onListItemClick in your code and you should see:

public void onListItemClick (ListView l, View v, int position, long id)
{
   // l - ListView that was clicked
   // v - View that was clicked within ListView l
   // position - Position of View v within ListView l
   // id - row id of item that was clicked
}
Jason L
  • 1,812
  • 2
  • 22
  • 43
  • P.S. I'm not sure how `Fragments` work, but you might want to remove the `import com.weinco.R` statement as importing any R file is unnecessary and could possibly mess up your build. – Jason L Jul 18 '12 at 13:01
  • how do i get the id of the ListView item? sorry but im new to list views, and im learning:p – rosu alin Jul 18 '12 at 13:20
  • Updated answer, give it a look-see. – Jason L Jul 18 '12 at 13:31
  • It doesnt let me change to ListFragment: Logcat Says: 07-18 16:31:46.242: E/AndroidRuntime(7535): FATAL EXCEPTION: main 07-18 16:31:46.242: E/AndroidRuntime(7535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weinco/com.weinco.activity.WeinCoTabActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 07-18 16:31:46.242: E/AndroidRuntime(7535): at android.os.Handler.dispatchMessage(Handler.java:99) – rosu alin Jul 18 '12 at 13:32
  • Judging from the LogCat, you should change your `ListView` id to "android.R.id.list". – Jason L Jul 18 '12 at 13:36
  • Actually it should be `android:id="@android:id/list"` – Benito Bertoli Jul 18 '12 at 13:40
  • doesn't work with them like that, and also i tried android:id="@+id/list" and still nothing – rosu alin Jul 18 '12 at 13:48
0

This solved it:

menuItemList.setOnItemClickListener(new OnItemClickListener() {

           public void onItemClick(AdapterView<?> menuItemList, View v, int position,
             long id) {
           changepageothers=position;
            if (changepageothers==0){              
             VF.showNext();
             changepageothers=-1;
            }
            if (changepageothers==1){
             VF.showPrevious();
             changepageothers=-1;
            }

           }
          });
rosu alin
  • 5,674
  • 11
  • 69
  • 150