gpt4 book ai didi

android - android中的自定义 ListView

转载 作者:行者123 更新时间:2023-11-29 22:17:56 25 4
gpt4 key购买 nike

我正在借助 Checkbox 创建具有多项选择的自定义列表。最后,我设法在列表的项目选择事件中设置了选中的复选框。

但是当我根据列表的选择未选中复选框时当我点击第一行时,第四行的复选框会自动被点击。总之序列是不维护的。我正在工作的代码如下

  ListAdapter  adapter = new SimpleAdapter(
this,
Datalist ,
R.layout.customlist,
new String[] {"fileName","contentLength","keyPath"},
new int[] {R.id.title,R.id.size, R.id.path}
);
setListAdapter(adapter);

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ViewGroup group=(ViewGroup)v;
CheckBox check=(CheckBox)group.findViewById(R.id.sharecheckbox);
check.toggle();
}

最佳答案

    ListView mainListView; 
mainListView = (ListView) findViewById( R.id.mainListView );

// Create and populate a List of planet names.
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );

// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );

// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
}

关于android - android中的自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8061121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com