私は次のコードを持っています:そしてリストが私のフラグメントに表示されます。リストアイテムをボタンとして使用するにはどうすればよいですか?クリックすると、ViewFlipperからページを変更できますか?
変数(changepage)を使ってみました魔女は-1で初期化されています。アダプターの位置をその中に入れます。クリックすると、金額を確認し、ViewFlipperのdisplaychildren関数を変更できます。
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());
}
}