在长按时搜索ListView显示错误的结果。

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

Searching Listview with onlongClick showing wrong results

问题

  1. //Connect to SQL server and read data
  2. try {
  3. connect = CONN(un, passwords, db, ip);
  4. Statement statement = connect.createStatement();
  5. rs = statement.executeQuery(query);
  6. List<Map<String, String>> data = null;
  7. data = new ArrayList<Map<String, String>>();
  8. //creating list of sms text
  9. sms = new ArrayList<String>();
  10. while (rs.next()) {
  11. Map<String, String> datanum = new HashMap<String, String>();
  12. datanum.put("A", rs.getString("faDateTime"));
  13. datanum.put("B", rs.getString("smsText"));
  14. data.add(datanum);
  15. //creating list of sms text
  16. smstext = rs.getString("smsText");
  17. sms.add(smstext);
  18. }
  19. String[] from = {"A", "B"};
  20. int[] views = {R.id.tx1, R.id.tx2};
  21. ADA = new SimpleAdapter(OpenserviceActivity.this, data, R.layout.templateforgrid, from, views);
  22. lv1 = (ListView) findViewById(R.id.lv1);
  23. lv1.setAdapter(ADA);
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. //enables filtering for the contents of the given ListView
  28. lv1.setTextFilterEnabled(true);
  29. //on long click list view
  30. lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  31. @Override
  32. public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long ids) {
  33. // TODO Auto-generated method stub
  34. Log.v("long clicked", "pos: " + pos);
  35. //separate the first line
  36. String[] substrings = sms.get(pos).split(" ");
  37. //separate numbers from sms body
  38. number = substrings[1].replaceAll("[^0-9]", "").trim();
  39. Toast.makeText(getApplicationContext(), "ID: " + number, Toast.LENGTH_LONG).show();
  40. //go to code sender
  41. Intent intent = new Intent(OpenserviceActivity.this, Codesender.class);
  42. intent.putExtra("number", number);
  43. startActivity(intent);
  44. finish();
  45. return true;
  46. }
  47. });
  48. lvsearch = (EditText) findViewById(R.id.etsearch);
  49. lvsearch.addTextChangedListener(new TextWatcher() {
  50. public void afterTextChanged(Editable s) {
  51. }
  52. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  53. }
  54. public void onTextChanged(CharSequence s, int start, int before, int count) {
  55. ADA.getFilter().filter(s.toString());
  56. }
  57. });
英文:

I load some data and show in a list view from SQL server. when I long click on a Item go to a new activity and send a string to the new activity. i create a search in list view but when search a item in list view and long click on it send wrong data to new activity.this is my codes:

  1. //Connect to SQL server and read data
  2. try {
  3. connect = CONN(un, passwords, db, ip);
  4. Statement statement = connect.createStatement();
  5. rs = statement.executeQuery(query);
  6. List&lt;Map&lt;String, String&gt;&gt; data = null;
  7. data = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
  8. //creating list of sms text
  9. sms = new ArrayList&lt;String&gt;();
  10. while (rs.next()) {
  11. Map&lt;String, String&gt; datanum = new HashMap&lt;String, String&gt;();
  12. datanum.put(&quot;A&quot;, rs.getString(&quot;faDateTime&quot;));
  13. datanum.put(&quot;B&quot;, rs.getString(&quot;smsText&quot;));
  14. data.add(datanum);
  15. //creating list of sms text
  16. smstext = rs.getString(&quot;smsText&quot;);
  17. sms.add(smstext);
  18. }
  19. String[] from = {&quot;A&quot;, &quot;B&quot;};
  20. int[] views = {R.id.tx1, R.id.tx2};
  21. ADA = new SimpleAdapter(OpenserviceActivity.this,
  22. data, R.layout.templateforgrid, from, views);
  23. lv1 = (ListView) findViewById(R.id.lv1);
  24. lv1.setAdapter(ADA);
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. //enables filtering for the contents of the given ListView
  30. lv1.setTextFilterEnabled(true);
  31. //on long click list view
  32. lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  33. @Override
  34. public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1,
  35. int pos, long ids) {
  36. // TODO Auto-generated method stub
  37. Log.v(&quot;long clicked&quot;,&quot;pos: &quot; + pos);
  38. //seprate the first line
  39. String[] substrings = sms.get(pos).split(&quot; &quot;);
  40. //seprate numbers from sms body
  41. number = substrings[1].replaceAll(&quot;[^0-9]&quot;, &quot;&quot;).trim();
  42. Toast.makeText(getApplicationContext(),&quot;ID: &quot; + number, Toast.LENGTH_LONG).show();
  43. //go to code sender
  44. Intent intent = new Intent(OpenserviceActivity.this, Codesender.class);
  45. intent.putExtra(&quot;number&quot;, number);
  46. startActivity(intent);
  47. finish();
  48. return true;
  49. }
  50. });
  51. lvsearch = (EditText) findViewById(R.id.etsearch);
  52. lvsearch.addTextChangedListener(new TextWatcher() {
  53. public void afterTextChanged(Editable s) {
  54. }
  55. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  56. }
  57. public void onTextChanged(CharSequence s, int start, int before, int count) {
  58. ADA.getFilter().filter(s.toString());
  59. }
  60. });

please help send correct data with pwn position to another activity.

答案1

得分: 0

我替换了这段代码:

  1. @Override
  2. public boolean onItemLongClick(AdapterView<?> parent, View view,
  3. int pos, long ids) {
  4. String text = ((TextView) view.findViewById(R.id.tx2)).getText().toString();
  5. // TODO Auto-generated method stub
  6. Log.v("long clicked","pos: " + pos);
  7. //分离第一行
  8. String[] substrings = text.split(" ");
  9. //从短信正文中分离出ID号码
  10. number = substrings[1].replaceAll("[^0-9]","").trim();

并解决了问题。

英文:

i replace this code

  1. @Override
  2. public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1,
  3. int pos, long ids) {
  4. // TODO Auto-generated method stub
  5. Log.v(&quot;long clicked&quot;,&quot;pos: &quot; + pos);
  6. //seprate the first line
  7. String[] substrings = sms.get(pos).split(&quot; &quot;);
  8. //seprate numbers from sms body
  9. number = substrings[1].replaceAll(&quot;[^0-9]&quot;, &quot;&quot;).trim();

with a new code below:

  1. @Override
  2. public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view,
  3. int pos, long ids) {
  4. String text = ((TextView) view.findViewById(R.id. tx2)).getText().toString();
  5. // TODO Auto-generated method stub
  6. Log.v(&quot;long clicked&quot;,&quot;pos: &quot; + pos);
  7. //seprate the first line
  8. String[] substrings = text.split(&quot; &quot;);
  9. //seprate ID numbers from sms body
  10. number = substrings[1].replaceAll(&quot;[^0-9]&quot;,&quot;&quot;).trim();

and solved

huangapple
  • 本文由 发表于 2020年7月26日 19:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099580.html
匿名

发表评论

匿名网友

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

确定