1

リストビュー項目を左から右にスワイプすると、Customadapter のテキストビューから 1 つ減算されるようにしようとしています。ただし、LayoutInflater を機能させることができないため、必要な TextView にアクセスできます。onItemClick でそれを行うことはできますが、onfling を機能させることができません。動作することを確認するためにトースト通知を追加しましたが (動作します)、このコードを動作させることができません。

主な活動

public class MainActivity extends Activity {


    //identify the elemts we are using
    private ArrayAdapter aa;
    private ArrayList<String> list = new ArrayList<String>();
    private ArrayList<Number> amount = new ArrayList<Number>();
    private ListView lv;
    private EditText et;
    private Button eom;
    private static final int SWIPE_MIN_DISTANCE = 75;
    private static final int SWIPE_THRESHOLD_VELOCITY = 50;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    GestureDetector gt;
    View.OnTouchListener gl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //assign values to the elements we are using
        lv = (ListView) findViewById(R.id.OneMoreList);
        et = (EditText) findViewById(R.id.edit_om);
        eom = (Button) findViewById(R.id.add_button);
        gt = new GestureDetector(this, new MyGestureDetector());


        //onclick listener for the button to create a new OneMore
        eom.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                String s = et.getText().toString();
                String skree = "One More " + s;    
                aa.add(skree);
                //notification of the data being sent, wipes the text, and closes the keyboard.
                et.setText("");
                hideSoftKeyboard();
            }
        });

        //defines the adapter we are using and sets it.
        aa = new OneMoreArrayAdapter(MainActivity.this, list);
        lv.setAdapter(aa);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                TextView tv2 = (TextView) view.findViewById(R.id.layout_count);
                String value = tv2.getText().toString();
                int n = Integer.valueOf(value);
                int nz = n + 1;
                String skr = Integer.toString(nz);
                tv2.setText(skr);
            }
        });

        lv.setOnTouchListener(new AdapterView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gt.onTouchEvent(event);
            }
        });


    }

    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            int pos = lv.pointToPosition((int)e1.getX(), (int)e1.getY());
            LayoutInflater inflator=MainActivity.this.getLayoutInflater();
            final String[] listarray = new String[list.size()];
            list.toArray(listarray);



            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //deletes
                    aa.remove(listarray[pos]);
                    aa.notifyDataSetChanged();
                }
                //left to right
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){


                    final View view = inflator.inflate(R.layout.one_more_list, null);
                    TextView tv2 = (TextView) view.findViewById(R.id.layout_count);
                    String value = tv2.getText().toString();
                    int n = Integer.valueOf(value);
                    int nz = n - 1;
                    String skr = Integer.toString(nz);
                    tv2.setText(skr);


                    //test to see if swipe works
                    Toast.makeText(MainActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();

                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }
}

アダプタ

class OneMoreArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private ArrayList<String> list;
    private int[] colors = new int[]{R.color.Goldenrod, R.color.Crimson, R.color.ForestGreen};

    public OneMoreArrayAdapter(Context context, ArrayList<String> list) {
        super(context, R.layout.one_more_list, list);
        this.context = context;
        this.list = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.one_more_list, parent, false);

        TextView tvtext = (TextView) view.findViewById(R.id.layout_text);
        TextView tvcount = (TextView) view.findViewById(R.id.layout_count);

        int colorPos = position % colors.length;
        view.setBackgroundResource(colors[colorPos]);

        final String[] listarray = new String[list.size()];
        list.toArray(listarray);


        tvtext.setText(listarray[position]);
        Number n = 0;
        String ns = n.toString();
        tvcount.setText(ns);
        return view;
    }
}

OnFling で TextView にアクセスできない理由を突き止めることに本当に興味があります。正しい方向にうなずいていただければ幸いです。

4

0 に答える 0