英文:
Listview Items auto click
问题
I have translated the code parts as requested:
我之前发布过这个问题,但目前还无法解决。我们如何通过按钮强制列表视图项目依次自动点击?我的目标是使列表视图中的所有项目依次可点击,并执行某些操作,例如向其他人发送 WhatsApp 消息。
放置文本到列表视图的代码如下:
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();
}
}
});
发送 WhatsApp 消息的列表视图代码如下:
listNamesId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// 这两行用于获取项目的索引值
//adapterNames.getItem(pos);
//Toast.makeText(MainActivity.this, "saif = " + pos, Toast.LENGTH_SHORT).show();
//以下代码用于获取项目文本值
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, "您忘记填写目标号码!", Toast.LENGTH_SHORT).show();
} else {
//注意:请首先使用国家代码的前2位数字(不包括加号)
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 应用程序未安装
}
}
}
});
请注意,我只翻译了代码的部分,没有包含其他内容。如果需要进一步的帮助,请随时提问。
英文:
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
}
}
}
});
答案1
得分: 0
请查看以下已翻译的代码部分:
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));
}
}
希望这些翻译对您有所帮助。如果您需要进一步的帮助,请随时提出。
英文:
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));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论