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

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

Searching Listview with onlongClick showing wrong results

问题

//Connect to SQL server and read data
try {
    connect = CONN(un, passwords, db, ip);
    Statement statement = connect.createStatement();
    rs = statement.executeQuery(query);
    List<Map<String, String>> data = null;
    data = new ArrayList<Map<String, String>>();
    //creating list of sms text
    sms = new ArrayList<String>();

    while (rs.next()) {
        Map<String, String> datanum = new HashMap<String, String>();
        datanum.put("A", rs.getString("faDateTime"));
        datanum.put("B", rs.getString("smsText"));
        data.add(datanum);
        //creating list of sms text
        smstext = rs.getString("smsText");
        sms.add(smstext);
    }
    String[] from = {"A", "B"};
    int[] views = {R.id.tx1, R.id.tx2};
    ADA = new SimpleAdapter(OpenserviceActivity.this, data, R.layout.templateforgrid, from, views);
    lv1 = (ListView) findViewById(R.id.lv1);
    lv1.setAdapter(ADA);
} catch (SQLException e) {
    e.printStackTrace();
}

//enables filtering for the contents of the given ListView
lv1.setTextFilterEnabled(true);

//on long click list view
lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long ids) {
        // TODO Auto-generated method stub
        Log.v("long clicked", "pos: " + pos);
        //separate the first line
        String[] substrings = sms.get(pos).split(" ");
        //separate numbers from sms body
        number = substrings[1].replaceAll("[^0-9]", "").trim();
        Toast.makeText(getApplicationContext(), "ID: " + number, Toast.LENGTH_LONG).show();
        //go to code sender
        Intent intent = new Intent(OpenserviceActivity.this, Codesender.class);
        intent.putExtra("number", number);
        startActivity(intent);
        finish();
        return true;
    }
});

lvsearch = (EditText) findViewById(R.id.etsearch);

lvsearch.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        ADA.getFilter().filter(s.toString());
    }
});
英文:

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:

               //Connect to SQL server and read data
try {
connect = CONN(un, passwords, db, ip);
Statement statement = connect.createStatement();
rs = statement.executeQuery(query);
List&lt;Map&lt;String, String&gt;&gt; data = null;
data = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
//creating list of sms text
sms = new ArrayList&lt;String&gt;();
while (rs.next()) {
Map&lt;String, String&gt; datanum = new HashMap&lt;String, String&gt;();
datanum.put(&quot;A&quot;, rs.getString(&quot;faDateTime&quot;));
datanum.put(&quot;B&quot;, rs.getString(&quot;smsText&quot;));
data.add(datanum);
//creating list of sms text
smstext = rs.getString(&quot;smsText&quot;);
sms.add(smstext);
}
String[] from = {&quot;A&quot;, &quot;B&quot;};
int[] views = {R.id.tx1, R.id.tx2};
ADA = new SimpleAdapter(OpenserviceActivity.this,
data, R.layout.templateforgrid, from, views);
lv1 = (ListView) findViewById(R.id.lv1);
lv1.setAdapter(ADA);
} catch (SQLException e) {
e.printStackTrace();
}
}
//enables filtering for the contents of the given ListView
lv1.setTextFilterEnabled(true);
//on long click  list view
lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1,
int pos, long ids) {
// TODO Auto-generated method stub
Log.v(&quot;long clicked&quot;,&quot;pos: &quot; + pos);
//seprate the first line
String[] substrings = sms.get(pos).split(&quot; &quot;);
//seprate numbers from sms body
number = substrings[1].replaceAll(&quot;[^0-9]&quot;, &quot;&quot;).trim();
Toast.makeText(getApplicationContext(),&quot;ID: &quot; + number, Toast.LENGTH_LONG).show();
//go to code sender
Intent intent = new Intent(OpenserviceActivity.this, Codesender.class);
intent.putExtra(&quot;number&quot;, number);
startActivity(intent);
finish();
return true;
}
});
lvsearch = (EditText) findViewById(R.id.etsearch);
lvsearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
ADA.getFilter().filter(s.toString());
}
});

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

答案1

得分: 0

我替换了这段代码:

     @Override
     public boolean onItemLongClick(AdapterView<?> parent, View view,
                                    int pos, long ids) {

         String text = ((TextView) view.findViewById(R.id.tx2)).getText().toString();

         // TODO Auto-generated method stub

         Log.v("long clicked","pos: " + pos);
         //分离第一行
         String[] substrings = text.split(" ");
         //从短信正文中分离出ID号码
         number = substrings[1].replaceAll("[^0-9]","").trim();

并解决了问题。

英文:

i replace this code

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

with a new code below:

 @Override
public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view,
int pos, long ids) {
String text = ((TextView) view.findViewById(R.id. tx2)).getText().toString();
// TODO Auto-generated method stub
Log.v(&quot;long clicked&quot;,&quot;pos: &quot; + pos);
//seprate the first line
String[] substrings = text.split(&quot; &quot;);
//seprate ID numbers from sms body
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:

确定