在我的家庭活动中。我有listview的id列表和带有id count的textview。
以下是代码:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int count = 0;
for (int i = 0; i <= list.getLastVisiblePosition(); i++) {
if (list.getChildAt(i) != null) {
count++;
counter.setText(count + "");
}
}
我想在listview
中的行的textview中显示答案 0 :(得分:0)
您可以使用getLastVisiblePosition()方法计算行数
int count=0;
for(int i = 0; i <= list.getLastVisiblePosition(); i++)
{
if(list.getChildAt(i)!= null)
{
count++; // saying that view that counts is the one that is not null, because sometimes you have partially visible items....
}
}
然后设置文本
counter.setText(count + "");
修改 - 强> 试试这样的事情,
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int count = 0;
for (int i = 0; i <= list.getLastVisiblePosition(); i++) {
if (list.getChildAt(i) != null) {
count++;
}
}
counter.setText(count + "");
答案 1 :(得分:0)
是否要显示ListView中有多少项目或屏幕上可见?
第一个选项:
如果使用ListView,则必须使用一些具有方法的ListAdapter(例如BaseAdapter或ArrayAdapter的后代)
getCount()
返回适配器和ListView中的项目数。
修改强>
TextView countTextView; // here you want to set number of items
ListView lv; // your ListView
BaseAdapter adapter; // your adapter, that is used with ListView
// call this method in some listener
private void showNumberOfItems() {
int count = adapter.getCount();
countTextView.setText(String.valueOf(count));
}