Abh*_*nav 4 sqlite indexing android listview position
我正在创建一个列表..列表的元素是从sqlite数据库中提取的.我使用ArrayList和ArrayAdapter填充列表...单击列表中的项目我希望能够触发包含有关项目点击...信息像项目的索引号..
使用方法:onItemClick(AdapterView av,View v,int index,long arg)
我确实得到了点击项目的索引.但它是当前显示的列表.问题出现在我做setFilterTextEnabled(true)时,并在应用程序中键入一些文本来搜索某个项目..然后单击它..而不是给我原始列表中项目的索引它给了我索引在筛选列表..
以下是代码片段:
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int index, long arg) {
Intent lyricsViewIntent = new Intent(iginga.this, LyricsPage.class);
lyricsViewIntent.putExtra("title", songList.get((int)arg).getTitle());
lyricsViewIntent.putExtra("id", songList.get((int)arg).getSongId());
startActivity(lyricsViewIntent);
}
});
myListView.setTextFilterEnabled(true);
Run Code Online (Sandbox Code Playgroud)
我有什么方法可以获得项目的原始索引/位置,而不是过滤后的文本中显示的那个...过滤时.
我最近对这个问题进行了一些摔跤,解决方案结果相当简单.您可以使用getListAdapter()
on 来检索"可见列表" ListActivity
,它反映了列表的当前过滤视图.
例如,在你的ListActivity
子类中onCreate()
:
final ListView listView = getListView();
final ListAdapter listAdapter = getListAdapter();
listView .setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyClass item = (MyClass) listAdapter .getItem(position);
// now do something with that item
}
});
Run Code Online (Sandbox Code Playgroud)
因此,忽略放入列表适配器的"原始"列表,而不是每次事件进入时从适配器请求列表.
小智 5
Abhinav我完全了解您的问题,因为过去两天我一直在努力解决同一问题。当您使用软键盘过滤数据时,直到我开始在调试器上使用断点时,我才意识到“ int position”和“ int id”的值会发生变化。希望这段代码可以使您更好地解决问题。我最终编写了一个for循环,以便可以将过滤列表的toString()与未过滤列表的toString()进行匹配。这样,我可以检索/更正“ int位置”值。
public class FacultyActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Retrieves the array stored in strings.xml
final String[] facultyList1 = getResources().getStringArray(R.array.professor_name);
final String[] facultyList2 = getResources().getStringArray(R.array.professor_email);
// Develop an array based on the list_view.xml template
setListAdapter(new ArrayAdapter<String>(this, R.layout.faculty, facultyList1));
final ListView lv = getListView();
// Allow the user to filter the array based on text input
lv.setTextFilterEnabled(true);
// Handle the user event where the user clicks on a professor's name
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
/* Filtering text changes the size of the array[index]. By clicking on a filtered
* entry the int position and long id does not correlate to the original array list.
* This block of code will search the original array based on the toString() function
* and for loop the orignial array to find the matching string, retrieving the
* correct index/position.
*/
String name = lv.getItemAtPosition(position).toString();
for (int index = 0; index < facultyList1.length; index++)
{
if (name.equals(facultyList1[index]))
{
position = index;
break;
}
}
Bundle bundle = new Bundle();
bundle.putString("email", facultyList2[position]);
startActivity(new Intent(FacultyActivity.this, EmailActivity.class).putExtras(bundle));
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8114 次 |
最近记录: |