英文:
Scrolling the listview to position of the newly added item
问题
我有一个带有ListView的自定义适配器。我从SQL Server查询项目,并使用我的ListView将它们显示为列表。我在ListView上有项目点击侦听器,以便获取项目位置。
public void getData() {
String query = "SELECT * FROM Table";
List<Map<String, String>> data = new ArrayList<>();
data = filterData(query);
adapter = new ContactListAdapter(this, data, R.layout.items, from, to);
listView.setAdapter(adapter);
listView.invalidateViews();
listView.setOnItemClickListener((adapterView, view, i, l) -> {
HashMap<String, String> retrieve = (HashMap<String, String>) adapterView.getAdapter().getItem(i);
callerID = retrieve.get("CallerDetailID");
.
.
});
}
我使用HashMap字符串将值存储在列表中,如下所示。
// 从SQL Server筛选联系人详细信息
List<Map<String, String>> filterData(String query) {
List<Map<String, String>> contact_details = new ArrayList<>();
try {
connect = connectionClass.CONN(); // 连接到数据库
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Map<String, String> datanum = new HashMap<>();
datanum.put("CallerDetailID", rs.getString("CallerID"));
.
.
.
contact_details.add(datanum);
}
} catch (Exception e) {
e.printStackTrace();
}
return contact_details;
}
我知道我可以通过单击项目来将其定位到位置(因为我已经使用了setOnItemClickListener
)。但我这里的问题是,如何在不点击项目的情况下滚动到新添加的项目位置?
一旦我获得了项目的位置,我可以在ListView中使用以下其中一种方法进行滚动。
listView.smoothScrollToPosition();
listView.smoothScrollToPosition();
listView.smoothScrollToPositionFromTop();
是否可能在ListView中不点击项目的情况下获取项目的位置?有什么想法吗?
英文:
I have a custom adapter with a listview. I query the items from SQL Server and display them as a list using my listview. I have item click listener on my list view in order to get the item position.
public void getData() {
String query = "SELECT * FROM Table";
List<Map<String, String>> data = new ArrayList<>();
data = filterData(query);
adapter = new ContactListAdapter(this, data, R.layout.items, from, to);
listView.setAdapter(adapter);
listView.invalidateViews();
listView.setOnItemClickListener((adapterView, view, i, l) -> {
HashMap<String, String> retreive = (HashMap<String, String>) adapterView.getAdapter().getItem(i);
callerID = retreive.get("CallerDetailID");
.
.
});
}
I store the values using hashmap strings in a list as follows.
// filter the contact details from sql server
List<Map<String, String>> filterData(String query) {
List<Map<String, String>> contact_details = new ArrayList<>();
try {
connect = connectionClass.CONN(); // Connect to database
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Map<String, String> datanum = new HashMap<>();
datanum.put("CallerDetailID", rs.getString("CallerID"));
.
.
.
contact_details.add(datanum);
}
} catch (Exception e) {
e.printStackTrace();
}
return contact_details;
}
I am aware that I can get the items to the position by clicking on it (since I already use setOnItemClickListener
). But my question here is how can I scroll to the newly added item's position in my list view, without clicking the item?
Once I get the position of the item, I can use one of the following methods in the listview to scroll.
listView.smoothScrollToPosition();
listView.smoothScrollToPosition();
listView.smoothScrollToPositionFromTop();
Is it possible to get the item's to position without clicking on the item in the listview? Any ideas folks.
答案1
得分: 2
要实现所需的行为,您需要向listview添加一个属性以实现自动滚动。
android:transcriptMode="alwaysScroll"
您只需要调用notifyDataSetChanged()
,您的列表将会自动滚动。
英文:
To achieve the behaviour you need to add a property to scroll listview automatically.
android:transcriptMode="alwaysScroll"
You just have to call notifyDataSetChanged() and your list will be auto scrolled.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论