Listview项目自动点击

huangapple go评论94阅读模式
英文:

Listview Items auto click

问题

I have translated the code parts as requested:

  1. 我之前发布过这个问题但目前还无法解决我们如何通过按钮强制列表视图项目依次自动点击我的目标是使列表视图中的所有项目依次可点击并执行某些操作例如向其他人发送 WhatsApp 消息
  2. 放置文本到列表视图的代码如下
  3. button2.setOnClickListener(new View.OnClickListener() {
  4. @Override
  5. public void onClick(View view) {
  6. if(!namesText.getText().toString().isEmpty()){
  7. adapterNames.add(namesText.getText().toString());
  8. namesText.setText("");
  9. adapterNames.notifyDataSetChanged();
  10. }
  11. }
  12. });
  13. 发送 WhatsApp 消息的列表视图代码如下
  14. listNamesId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  15. public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
  16. // 这两行用于获取项目的索引值
  17. //adapterNames.getItem(pos);
  18. //Toast.makeText(MainActivity.this, "saif = " + pos, Toast.LENGTH_SHORT).show();
  19. //以下代码用于获取项目文本值
  20. View nextItem = listNamesId.getChildAt(pos+1);
  21. if(nextItem != null){
  22. String selectedFromList = (String) (listNamesId.getItemAtPosition(pos));
  23. namesText.setText(selectedFromList);
  24. }
  25. if (namesTexter.getText().toString().trim().length() == 0) {
  26. Toast.makeText(MainActivity.this, "您忘记填写目标号码!", Toast.LENGTH_SHORT).show();
  27. } else {
  28. //注意:请首先使用国家代码的前2位数字(不包括加号)
  29. try {
  30. namesTexter.setText(listNamesId.getItemAtPosition(pos).toString());
  31. String mobile = namesTexter.getText().toString();
  32. String msg = textmessage.getText().toString();
  33. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://api.whatsapp.com/send?phone=" + mobile + "&text=" + msg)));
  34. } catch (Exception e) {
  35. // WhatsApp 应用程序未安装
  36. }
  37. }
  38. }
  39. });

请注意,我只翻译了代码的部分,没有包含其他内容。如果需要进一步的帮助,请随时提问。

英文:

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.

  1. button2.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View view) {
  4. if(!namesText.getText().toString().isEmpty()){
  5. adapterNames.add(namesText.getText().toString());
  6. namesText.setText(&quot;&quot;);
  7. adapterNames.notifyDataSetChanged();
  8. }
  9. }
  10. });

Listview Code for Sending Whats app message is:

  1. listNamesId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. public void onItemClick(AdapterView&lt;?&gt; list, View v, int pos, long id) {
  3. // This two lines is for get indexing value of an item
  4. //adapterNames.getItem(pos);
  5. //Toast.makeText(MainActivity.this, &quot;saif = &quot; + pos, Toast.LENGTH_SHORT).show();
  6. //this below code is for getting item text value
  7. View nextItem = listNamesId.getChildAt(pos+1);
  8. if(nextItem != null){
  9. String selectedFromList = (String) (listNamesId.getItemAtPosition(pos));
  10. namesText.setText(selectedFromList);
  11. }
  12. if (namesTexter.getText().toString().trim().length() == 0) {
  13. Toast.makeText(MainActivity.this, &quot;You are missing your destination number!&quot;, Toast.LENGTH_SHORT).show();
  14. } else {
  15. //NOTE : please use with country code first 2digits without plus signed
  16. try {
  17. namesTexter.setText(listNamesId.getItemAtPosition(pos).toString());
  18. String mobile = namesTexter.getText().toString();
  19. String msg = textmessage.getText().toString();
  20. startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(&quot;https://api.whatsapp.com/send?phone=&quot; + mobile + &quot;&amp;text=&quot; + msg)));
  21. } catch (Exception e) {
  22. //whatsapp app not install
  23. }
  24. }
  25. }
  26. });

My Task is as shown in image
Listview项目自动点击

答案1

得分: 0

请查看以下已翻译的代码部分:

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/bt_all_displaying_items"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Auto Click All Displaying items" />
  14. <Button
  15. android:id="@+id/bt_all_list_items"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="Auto Click All List items" />
  19. <EditText
  20. android:id="@+id/et"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:text="Testing"
  24. android:textSize="30sp" />
  25. <ListView
  26. android:id="@+id/lv"
  27. android:layout_width="match_parent"
  28. android:layout_height="match_parent" />
  29. </LinearLayout>

MainActivity.java:

  1. public class MainActivity extends AppCompatActivity {
  2. final static int delayTime = 250;
  3. ArrayList<String> listSample = new ArrayList<>();
  4. Button bt1, bt2;
  5. EditText et;
  6. ListView lv;
  7. int i;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. bt1 = findViewById(R.id.bt_all_displaying_items);
  13. bt2 = findViewById(R.id.bt_all_list_items);
  14. et = findViewById(R.id.et);
  15. lv = findViewById(R.id.lv);
  16. setupSampleList();
  17. ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listSample);
  18. lv.setAdapter(adapter);
  19. bt1.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. i = 0;
  23. Handler handler = new Handler();
  24. Runnable runnable = new Runnable() {
  25. @Override
  26. public void run() {
  27. lv.performItemClick(lv.getChildAt(i), i + lv.getFirstVisiblePosition(), 0);
  28. i++;
  29. if (i < (lv.getChildCount())) handler.postDelayed(this, delayTime);
  30. }
  31. };
  32. handler.postDelayed(runnable, delayTime);
  33. }
  34. });
  35. bt2.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View view) {
  38. i = 0;
  39. lv.smoothScrollToPosition(i);
  40. Handler handler = new Handler();
  41. Runnable runnable = new Runnable() {
  42. @Override
  43. public void run() {
  44. lv.performItemClick(lv.getChildAt(i - lv.getFirstVisiblePosition()), i, 0);
  45. i++;
  46. if (i < listSample.size()) {
  47. lv.smoothScrollToPosition(i);
  48. handler.postDelayed(this, delayTime);
  49. } else {
  50. Toast.makeText(getBaseContext(), "End of List!", Toast.LENGTH_LONG).show();
  51. }
  52. }
  53. };
  54. handler.postDelayed(runnable, delayTime);
  55. }
  56. });
  57. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  58. @Override
  59. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  60. TextView textView = (TextView) view;
  61. textView.setText(textView.getText() + ": " + et.getText());
  62. textView.postDelayed(new Runnable() {
  63. @Override
  64. public void run() {
  65. textView.setText((String) lv.getItemAtPosition(i));
  66. }
  67. }, 200);
  68. }
  69. });
  70. }
  71. private void setupSampleList() {
  72. for (int i = 0; i < 50; i++) listSample.add("Sample Item: " + (i + 1));
  73. }
  74. }

希望这些翻译对您有所帮助。如果您需要进一步的帮助,请随时提出。

英文:

Try this sample:

activity_main.xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:orientation=&quot;vertical&quot;
  8. tools:context=&quot;.MainActivity&quot;&gt;
  9. &lt;Button
  10. android:id=&quot;@+id/bt_all_displaying_items&quot;
  11. android:layout_width=&quot;wrap_content&quot;
  12. android:layout_height=&quot;wrap_content&quot;
  13. android:text=&quot;Auto Click All Displaying items&quot; /&gt;
  14. &lt;Button
  15. android:id=&quot;@+id/bt_all_list_items&quot;
  16. android:layout_width=&quot;wrap_content&quot;
  17. android:layout_height=&quot;wrap_content&quot;
  18. android:text=&quot;Auto Click All List items&quot; /&gt;
  19. &lt;EditText
  20. android:id=&quot;@+id/et&quot;
  21. android:layout_width=&quot;match_parent&quot;
  22. android:layout_height=&quot;wrap_content&quot;
  23. android:text=&quot;Testing&quot;
  24. android:textSize=&quot;30sp&quot; /&gt;
  25. &lt;ListView
  26. android:id=&quot;@+id/lv&quot;
  27. android:layout_width=&quot;match_parent&quot;
  28. android:layout_height=&quot;match_parent&quot; /&gt;
  29. &lt;/LinearLayout&gt;

MainActivity.java:

  1. public class MainActivity extends AppCompatActivity {
  2. final static int delayTime = 250;
  3. ArrayList&lt;String&gt; listSample = new ArrayList&lt;&gt;();
  4. Button bt1, bt2;
  5. EditText et;
  6. ListView lv;
  7. int i;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. bt1 = findViewById(R.id.bt_all_displaying_items);
  13. bt2 = findViewById(R.id.bt_all_list_items);
  14. et = findViewById(R.id.et);
  15. lv = findViewById(R.id.lv);
  16. setupSampleList();
  17. ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1, listSample);
  18. lv.setAdapter(adapter);
  19. bt1.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. i = 0;
  23. Handler handler = new Handler();
  24. Runnable runnable = new Runnable() {
  25. @Override
  26. public void run() {
  27. lv.performItemClick(lv.getChildAt(i), i + lv.getFirstVisiblePosition(), 0);
  28. i++;
  29. if (i &lt; (lv.getChildCount())) handler.postDelayed(this, delayTime);
  30. }
  31. };
  32. handler.postDelayed(runnable, delayTime);
  33. }
  34. });
  35. bt2.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View view) {
  38. i = 0;
  39. lv.smoothScrollToPosition(i);
  40. Handler handler = new Handler();
  41. Runnable runnable = new Runnable() {
  42. @Override
  43. public void run() {
  44. lv.performItemClick(lv.getChildAt(i - lv.getFirstVisiblePosition()), i, 0);
  45. i++;
  46. if (i &lt; listSample.size()) {
  47. lv.smoothScrollToPosition(i);
  48. handler.postDelayed(this, delayTime);
  49. } else {
  50. Toast.makeText(getBaseContext(), &quot;End of List!&quot;, Toast.LENGTH_LONG).show();
  51. }
  52. }
  53. };
  54. handler.postDelayed(runnable, delayTime);
  55. }
  56. });
  57. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  58. @Override
  59. public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  60. TextView textView = (TextView) view;
  61. textView.setText(textView.getText() + &quot;: &quot; + et.getText());
  62. textView.postDelayed(new Runnable() {
  63. @Override
  64. public void run() {
  65. textView.setText((String)lv.getItemAtPosition(i));
  66. }
  67. }, 200);
  68. }
  69. });
  70. }
  71. private void setupSampleList() {
  72. for (int i = 0 ; i &lt; 50; i++) listSample.add(&quot;Sample Item: &quot; + (i + 1));
  73. }
  74. }

huangapple
  • 本文由 发表于 2023年6月12日 18:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定