0

I have posted this question before but was unable to resolve the issue as of now. How can we force list view items to be auto clicked in turn through Button? My goal is to make all items in the list view be clickable in turn and perform certain actions such s send Whats app message to others. Code of putting Text into Listview is.

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!namesText.getText().toString().isEmpty()){
                adapterNames.add(namesText.getText().toString());
                namesText.setText("");
                adapterNames.notifyDataSetChanged();
            }
        }
    });

Listview Code for Sending Whats app message is:

    listNamesId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
           // This two lines is for get indexing value of an item
            //adapterNames.getItem(pos);
            //Toast.makeText(MainActivity.this, "saif = " + pos, Toast.LENGTH_SHORT).show();

            //this below code is for getting item text value
            View nextItem = listNamesId.getChildAt(pos+1);
            if(nextItem != null){
                String selectedFromList = (String) (listNamesId.getItemAtPosition(pos));
                namesText.setText(selectedFromList);
            }
            if (namesTexter.getText().toString().trim().length() == 0) {
                Toast.makeText(MainActivity.this, "You are missing your destination number!", Toast.LENGTH_SHORT).show();

            } else {
                //NOTE : please use with country code first 2digits without plus signed
                try {
                    namesTexter.setText(listNamesId.getItemAtPosition(pos).toString());
                    String mobile = namesTexter.getText().toString();
                    String msg = textmessage.getText().toString();
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=" + mobile + "&text=" + msg)));

                } catch (Exception e) {
                    //whatsapp app not install
                }

            }


        }
    });

My Task is as shown in image Send Whats app message auto through list view

  • You need to look at [how to programmatically click listview items](https://stackoverflow.com/q/9820175/16653700) and you need to know [how to iterate through listview items](https://stackoverflow.com/questions/30044584/android-loop-through-all-items-in-a-listview). – Alias Cartellano Jun 12 '23 at 22:11
  • In my question, i have already explain that i scroll listview items through timer (means a button start loop through every items of listview and perform some function. – Best Thinking Jun 13 '23 at 04:24
  • If only need to iterate the displaying items, then try `listNamesId.performItemClick(null, i, 0)` and loop `i` from 0 to (listNamesId.getChildCount() - 1). Pls note getChildAt is for the Views displaying, the toppest item view is always getChildAt(0) even the listView is scrolled away from the 1st item. – i_A_mok Jun 14 '23 at 15:21
  • Please share a code example with tanks. – Best Thinking Jun 16 '23 at 00:48

1 Answers1

0

Try this sample:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
    android:id="@+id/bt_all_displaying_items"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Auto Click All Displaying items" />

<Button
    android:id="@+id/bt_all_list_items"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Auto Click All List items" />

<EditText
    android:id="@+id/et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Testing"
    android:textSize="30sp" />

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

final static int delayTime = 250;

ArrayList<String> listSample = new ArrayList<>();
Button bt1, bt2;
EditText et;
ListView lv;
int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt1 = findViewById(R.id.bt_all_displaying_items);
    bt2 = findViewById(R.id.bt_all_list_items);
    et = findViewById(R.id.et);
    lv = findViewById(R.id.lv);

    setupSampleList();
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listSample);
    lv.setAdapter(adapter);

    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            i = 0;
            Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    lv.performItemClick(lv.getChildAt(i), i + lv.getFirstVisiblePosition(), 0);
                    i++;
                    if (i < (lv.getChildCount())) handler.postDelayed(this, delayTime);
                }
            };
            handler.postDelayed(runnable, delayTime);
        }
    });
    bt2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            i = 0;
            lv.smoothScrollToPosition(i);
            Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    lv.performItemClick(lv.getChildAt(i - lv.getFirstVisiblePosition()), i, 0);
                    i++;
                    if (i < listSample.size()) {
                        lv.smoothScrollToPosition(i);
                        handler.postDelayed(this, delayTime);
                    } else {
                        Toast.makeText(getBaseContext(), "End of List!", Toast.LENGTH_LONG).show();
                    }
                }
            };
            handler.postDelayed(runnable, delayTime);
        }
    });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            TextView textView = (TextView) view;
            textView.setText(textView.getText() + ": " + et.getText());
            textView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    textView.setText((String)lv.getItemAtPosition(i));
                }
            }, 200);
        }
    });
}

private void setupSampleList() {
    for (int i = 0 ; i < 50; i++) listSample.add("Sample Item: " + (i + 1));
}
}
i_A_mok
  • 2,744
  • 2
  • 11
  • 15
  • Edited the sample. – i_A_mok Jun 18 '23 at 15:04
  • Thanks for your help and improvement. Now my further task is how we can add text in list view through edit text box. for example " button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!namesText.getText().toString().isEmpty()){ adapterNames.add(namesText.getText().toString()); namesText.setText(""); adapterNames.notifyDataSetChanged(); } } }); " – Best Thinking Jun 19 '23 at 02:49
  • OK i have done. Thanks – Best Thinking Jun 19 '23 at 03:43
  • How to stop delay time ? I mean if any user opens another activity by clicking any item in the list view then the delay time method should stop. And when the user closes this second activity and returns to the first main activity, the delay time method starts again. – Best Thinking Jun 23 '23 at 03:51
  • There may be something to do on life cycle of the activity. – i_A_mok Jun 25 '23 at 04:07