1

i'm using this adapter SwipeActionAdapter (https://github.com/wdullaer/SwipeActionAdapter)

I would like to ask if how to get the textview id in my custom listview every time i swipe the selected item.

E.G: I have a textview on each items and that textview has a value of 0 then if i swipe to left it will increment to 1 then if i swipe to right it will subtract to 1. here's my activity:

FoodList.java

public class Foodlist extends ListActivity implements SwipeActionAdapter.SwipeActionListener {

    protected SwipeActionAdapter mAdapter;

    Button btnBack, btnNext;
    ProgressBar progressBar;

    Animation animReverseScale;

    int swipeCount = 0;

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

        String[] content = new String[4];
        for (int i=0;i<4;i++) {

            if (i == 0) {
                content[i] = "Burgers";
            } else if (i == 1) {
                content[i] = "Chips";
            } else if (i == 2) {
                content[i] = "Soft Drinks";
            } else if (i == 3) {
                content[i] = "Hotdogs";
            }
        }

        ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
                this,
                R.layout.foods_extra,
                R.id.txtName,
                new ArrayList<String>(Arrays.asList(content))
        );
        mAdapter = new SwipeActionAdapter(stringAdapter);
        mAdapter.setSwipeActionListener(this)
                .setListView(getListView());
        setListAdapter(mAdapter);

        mAdapter.addBackground(SwipeDirections.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
                .addBackground(SwipeDirections.DIRECTION_FAR_RIGHT, R.layout.row_bg_right_far);

        animReverseScale = AnimationUtils.loadAnimation(this, R.anim.anim_reverse_scale);

        btnBack = (Button) findViewById(R.id.btnBack);
        btnNext = (Button) findViewById(R.id.btnNext);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.getProgressDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);

        btnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent("com.reactivate.Z1");
                startActivity(myIntent);
                overridePendingTransition(0, 0);
                finish();
            }
        });

        btnBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                btnBack.setAlpha((float) 0.5);
                return false;
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                animationReverseScale(v);
                Intent myIntent = new Intent("com.reactivate.SpiderGraph");
                startActivity(myIntent);
                overridePendingTransition(0, 0);
                finish();
            }
        });
    }

    public void animationReverseScale(View view) {
        view.startAnimation(animReverseScale);
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id){
        Toast.makeText(
                this,
                "Clicked " + mAdapter.getItem(position),
                Toast.LENGTH_SHORT
        ).show();
    }

    @Override
    public boolean hasActions(int position){
        return true;
    }

    @Override
    public boolean shouldDismiss(int position, int direction){
        return false;
    }

    @Override
    public void onSwipe(int[] positionList, int[] directionList){
        for(int i=0;i<positionList.length;i++) {
            int direction = directionList[i];
            int position = positionList[i];
            switch (direction) {
                case SwipeDirections.DIRECTION_FAR_LEFT:
                    //mAdapter.getItemId(getSelectedItemPosition());
                    Toast.makeText(
                            this,
                            "Checked " + mAdapter.getItem(position),
                            Toast.LENGTH_SHORT
                    ).show();
                    break;
                case SwipeDirections.DIRECTION_FAR_RIGHT:
                    Toast.makeText(
                            this,
                            "Unchecked " + mAdapter.getItem(position),
                            Toast.LENGTH_SHORT
                    ).show();
                    break;
            mAdapter.notifyDataSetChanged();
        }
    }
}

food_extra.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="?android:listPreferredItemHeight"
    android:background="@drawable/listview_style"
    android:padding="8dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"
        android:layout_centerVertical="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/txtName"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/imageView"
        android:layout_toStartOf="@+id/imageView"
        android:layout_marginRight="30dp"
        android:layout_marginEnd="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/txtNumber"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="40dp"
        android:layout_marginEnd="40dp" />

</RelativeLayout>

foodList.xml

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_marginTop="20sp"
        android:background="@drawable/listview_style"
        android:layout_centerHorizontal="true" />

I'm already getting the specific index of each item by using

mAdapter.getItemId(getSelectedItemPosition())

Any idea? Please help. Thanks

Cris
  • 387
  • 2
  • 3
  • 13

2 Answers2

1

The library you are using does not provide such function.

However you can try to create your own function to get the corresponding View by adapter position.

You can check how to do it in the link below.

android - listview get item view by position

Community
  • 1
  • 1
Derek Fung
  • 7,947
  • 1
  • 22
  • 28
1

Since you're using a ListActivity you could try:

final ListView listView = getListView();
listView.getChildAt(position - listView.getFirstVisiblePosition());
Scott Cooper
  • 1,665
  • 1
  • 16
  • 26