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
}
}
}
});
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));
}
}