Android Filtered ListView(Android Filtered ListView)
我从xml文件中获取listview的值。 这些是我的java和xml代码:
package com.gazi.tezproje; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ListView; import android.widget.Toast; public class verbs extends Activity { public boolean onCreateOptionsMenu(Menu menu) { ActionBar bar=getActionBar(); bar.setDisplayHomeAsUpEnabled(true); MenuInflater menu1 = getMenuInflater(); menu1.inflate(R.menu.info, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuinfo: Toast.makeText(getApplicationContext(),getString(R.string.verbs), Toast.LENGTH_LONG).show(); break; case android.R.id.home: finish(); break; } return false; } private ListView vlist; EditText ara; private String [] fiilliste; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.verbs); fiilliste=getResources().getStringArray(R.array.fiiller); vlist=(ListView)findViewById(R.id.listView1); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fiilliste); vlist.setAdapter(adapter); ara = (EditText) findViewById(R.id.editText1); vlist.setTextFilterEnabled(true); vlist.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> a,View v,int position,long id){ } }); ara.addTextChangedListener(new TextWatcher(){ public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { // When user changed the Text verbs.this.adapter.getFilter().filter(cs); } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } }); }}
XML文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="fiiller"> <item>A</item> <item>Affetmek</item> <item>Almak</item> <item>Anlamak</item> <item>Anlaşmak</item> <item>Anlatmak</item> <item>Ayrılmak</item> <item>B</item> <item>Bakmak</item> <item>Başlamak</item> </string-array> </resources>
例如; 当我点击列表视图“Bakmak”时,我想显示带有吐司的文本,即使过滤了listview。 而对于listview的其他项目,另一个吐司。 我不能这样做,因为如你所知过滤后,项目的位置会发生变化。
我真的需要你的想法和帮助! 谢谢!
I take values of listview from an xml file. These are my java and xml codes:
package com.gazi.tezproje; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ListView; import android.widget.Toast; public class verbs extends Activity { public boolean onCreateOptionsMenu(Menu menu) { ActionBar bar=getActionBar(); bar.setDisplayHomeAsUpEnabled(true); MenuInflater menu1 = getMenuInflater(); menu1.inflate(R.menu.info, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuinfo: Toast.makeText(getApplicationContext(),getString(R.string.verbs), Toast.LENGTH_LONG).show(); break; case android.R.id.home: finish(); break; } return false; } private ListView vlist; EditText ara; private String [] fiilliste; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.verbs); fiilliste=getResources().getStringArray(R.array.fiiller); vlist=(ListView)findViewById(R.id.listView1); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fiilliste); vlist.setAdapter(adapter); ara = (EditText) findViewById(R.id.editText1); vlist.setTextFilterEnabled(true); vlist.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> a,View v,int position,long id){ } }); ara.addTextChangedListener(new TextWatcher(){ public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { // When user changed the Text verbs.this.adapter.getFilter().filter(cs); } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } }); }}
XML File
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="fiiller"> <item>A</item> <item>Affetmek</item> <item>Almak</item> <item>Anlamak</item> <item>Anlaşmak</item> <item>Anlatmak</item> <item>Ayrılmak</item> <item>B</item> <item>Bakmak</item> <item>Başlamak</item> </string-array> </resources>
For example; when I clicked on listview which is "Bakmak", I wanna show a text with toast, even when listview is filtered. And for other item of listview, another toast. I couldn't do it, because after filtering as you know, positions of items change.
I really need your ideas and help! Thank you!
原文:https://stackoverflow.com/questions/19877747
最满意答案
正如Lukas所指出的,您需要在方法结束时返回
UITableViewCell
。事实上,你发布的内容甚至不应该编译,所以我想知道你是否错误地发布了示例代码。
首先尝试并实际返回单元格,将代码更新为:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! { let cell = UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"douban") let rowData:NSDictionary = self.tableData[indexPath.row] as! NSDictionary cell.textLabel!.text = "hehehehe"//rowData["title"] as! String cell.detailTextLabel!.text = "adasdasda"//rowData["artist"] as! String // YOU ARE MISSING THIS LINE return cell }
还要确保正确设置了
UITableViewDatasource
,并确保所需的方法正常运行。 具体来说,numberOfRowsInSection
和numberOfSectionsInTableView
需要返回大于0的值。(在您发布的代码中,您缺少numberOfSectionsInTableView
)As Lukas points out, you need to return the
UITableViewCell
at the end of the method.In fact, what you posted shouldn't even compile, so I'm wondering if you posted your sample code incorrectly.
The first thing to try, and actually return the cell, update your code to:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! { let cell = UITableViewCell(style:UITableViewCellStyle.Subtitle,reuseIdentifier:"douban") let rowData:NSDictionary = self.tableData[indexPath.row] as! NSDictionary cell.textLabel!.text = "hehehehe"//rowData["title"] as! String cell.detailTextLabel!.text = "adasdasda"//rowData["artist"] as! String // YOU ARE MISSING THIS LINE return cell }
Also ensure that you
UITableViewDatasource
is set properly, and that the required methods are functioning. Specifically, bothnumberOfRowsInSection
andnumberOfSectionsInTableView
need to be returning values greater than 0. (In the code you posted, you are missingnumberOfSectionsInTableView
)
相关问答
更多-
// cell reuse UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 你已经使用过的单元格返回了它,它已经有了一个UILabel子视图,并且你正在添加另一个子视图。 将添加子视图放在该部分 if (cell == nil) { //cell initialization 并在单元初始化后根据需要编辑子视图,例如可以通过标记访问它们。 // cell re ...
-
UITableView移动到单元格(UITableView move to cell)[2024-02-13]
您可以确定scrollToRowAtIndexPath方法。 所以所有你需要做的是创建一个新的IndexPath与你想要移动的行,例如,row index = 10: [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; ... -
将表单元格的selectionStyle属性设置为UITableViewCellSelectionStyleNone 。 这应该阻止它突出显示,您还可以检查您的tableView:didSelectRowAtIndexPath:中的属性tableView:didSelectRowAtIndexPath: Set the table cell's selectionStyle property to UITableViewCellSelectionStyleNone. That should prevent ...
-
如何从NSDictionary为UITableview中的单元格设置文本(How can I set text for cell in UITableview from NSDictionary)[2023-04-05]
您可以在字典中显示键的列表,如下所示: for (id key in [myDictionary allKeys]) NSLog(@"Key : %@ => value : %@", key, [myDictionary objectForKey:key]); 所以,对于你的字典,这将显示 ab => Air Bus 787 => Boeng 787 sp => Some Plane 我假设你想要在表中显示所有值,所以你会想要这样的东西: - (UITableViewCell *)tableView ... -
UITableView最后一个单元格(UITableView Last cell)[2021-11-18]
如果你知道表格中的单元格数量,并且想知道最后一行何时出现,你可以实现: - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 并将该行与您的表rowcount进行比较。 这个方法只是在显示一行之前触发。 If you know your number of cells in the table and you ... -
我为UITableView的单元格设置了值,但为什么它没有显示?(I have set value for cell of UITableView but why it didn't display?)[2022-12-24]
正如Lukas所指出的,您需要在方法结束时返回UITableViewCell 。 事实上,你发布的内容甚至不应该编译,所以我想知道你是否错误地发布了示例代码。 首先尝试并实际返回单元格,将代码更新为: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! { let cell = UITableViewCell(style:UITableView ... -
你需要调整你的想法。 Cocoa强烈遵循MVC(模型视图控制器)设计模式,你应该学会这样思考。 在MVC中,模型是存储数据的程序的一部分。 视图是向用户显示信息并与用户交互的信息。 控制器是用作控制逻辑的大脑,并在模型和视图之间来回传送数据。 在UIKit中,名称中包含View的对象是视图对象,名称中带控制器的对象是控制器对象。 作为一般规则,模型对象由您自己决定,因为它是特定于应用程序的。 表视图是视图对象。 它不存储信息,它将其呈现给用户和/或从用户收集信息。 数组通常是存储在表视图中显示的数据的最佳选 ...
-
要在不使用表视图的情况下直接从笔尖加载单元格,您需要从主程序包加载它。 Objective-C的: CustomClass *cell = (CustomClass *)[[[NSBundle mainBundle] loadNibNamed:@"nibName" owner:self options:nil] firstObject] 迅速: let cell = NSBundle.mainBundle().loadNibNamed("nibName", owner: self, options: ni ...
-
问题: 根据apple的HIG,默认的可点击区域是44,所以默认情况下所有控件都有44个高度。 你在里面定义新的UITableViewCell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 这是默认情况下44,你在UITableViewCell的框架中什么都不做更改并返回它。 解: 尝试设置UITableViewCell的帧大小,即cell.fra ...
-
最后我找到了解决方案。 我在我的项目中添加了一个新的用户界面空文件,然后我在这个新的nib文件中创建了一个单元格,并为其分配了自定义单元格类。 在Interface Builder中,我将prototype Cells设置为0,用于questionTableView,然后我在viewDidLoad()中注册了Nib forCellReuseIdentifier,对于searchResultsTableView,这两个都是questionTableView。 这是我的代码: var cellIdentifie ...