使用 onlongClick 搜索 Listview 显示错误结果
Searching Listview with onlongClick showing wrong results
我从 SQL 服务器加载一些数据并在列表视图中显示。当我长按一个项目时,转到一个新的 activity 并将一个字符串发送到新的 activity。我在列表视图中创建了一个搜索,但是当在列表视图中搜索一个项目并长按它时,将错误的数据发送到新的 activity.this 是我的代码:
//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);
//seprate the first line
String[] substrings = sms.get(pos).split(" ");
//seprate 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());
}
});
请帮助将带有 pwn 位置的正确数据发送到另一个 activity。
我替换这段代码
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long ids) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
//seprate the first line
String[] substrings = sms.get(pos).split(" ");
//seprate numbers from sms body
number = substrings[1].replaceAll("[^0-9]", "").trim();
使用下面的新代码:
@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);
//seprate the first line
String[] substrings = text.split(" ");
//seprate ID numbers from sms body
number = substrings[1].replaceAll("[^0-9]","").trim();
并解决了
我从 SQL 服务器加载一些数据并在列表视图中显示。当我长按一个项目时,转到一个新的 activity 并将一个字符串发送到新的 activity。我在列表视图中创建了一个搜索,但是当在列表视图中搜索一个项目并长按它时,将错误的数据发送到新的 activity.this 是我的代码:
//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);
//seprate the first line
String[] substrings = sms.get(pos).split(" ");
//seprate 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());
}
});
请帮助将带有 pwn 位置的正确数据发送到另一个 activity。
我替换这段代码
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long ids) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
//seprate the first line
String[] substrings = sms.get(pos).split(" ");
//seprate numbers from sms body
number = substrings[1].replaceAll("[^0-9]", "").trim();
使用下面的新代码:
@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);
//seprate the first line
String[] substrings = text.split(" ");
//seprate ID numbers from sms body
number = substrings[1].replaceAll("[^0-9]","").trim();
并解决了