我有一个edittext和一个包含数据的listview,因此在显示了listtext相关关键字后,应该显示listview中的关键字。为此我已经完成了编码..但过滤了所需的项目。当我点击它。如果列表包含三个项目aa,bb和cc,则不会打开该项目。如果我搜索cc项目然后点击它打开aa item ..所以请告诉我如何解决这个问题...
package com.example.education;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FirstActivity extends Activity implements OnItemClickListener {
private ListView listview;
private ArrayList<Temple> arraylist;
private ArrayAdapter<Temple> adapter;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
init();
}
private void init() {
listview=(ListView)findViewById(R.id.listView1);
arraylist=new ArrayList<Temple>();
arraylist.add(new Temple("Birla","ttttt",R.drawable.birla));
arraylist.add(new Temple("Badrinath", "ddddd",R.drawable.badrinath));
arraylist.add(new Temple("ganga", "dddddd",R.drawable.ganga));
adapter=new ArrayAdapter<Temple>(this,android.R.layout.simple_list_item_1,arraylist );
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
First.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_first, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
// TODO Auto-generated method stub
Temple t= arraylist.get(index);
Intent intent=new Intent(this,DetailActivity.class);
intent.putExtra("Title",t.getTitle() );
intent.putExtra("Desc",t.getDescription() );
intent.putExtra("Img",t.imgId );
startActivity(intent);
}
}
第二节课
package com.example.education;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
public class DetailActivity extends Activity {
private TextView Title;
private TextView Description;
private ImageView Image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
init();
}
private void init() {
// TODO Auto-generated method stub
Image=(ImageView)findViewById(R.id.imageView1);
Title=(TextView)findViewById(R.id.textView1);
Description=(TextView)findViewById(R.id.textView2);
Intent intent=getIntent();
Title.setText(intent.getStringExtra("Title"));
Description.setText(intent.getStringExtra("Desc"));
Image.setImageResource(intent.getIntExtra("Img",R.drawable.ic_launcher));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_detail, menu);
return true;
}
}
第三节课
package com.example.education;
public class Temple {
String Title;
String Description;
int imgId;
@Override
public String toString() {
return "" + Title;
}
public Temple() {
super();
// TODO Auto-generated constructor stub
}
public Temple(String title, String description, int imgId) {
super();
Title = title;
Description = description;
this.imgId = imgId;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
}
答案 0 :(得分:0)
通过阅读说明,我理解的是,因为有三个项目,例如aa,bb和cc,并且在其中搜索cc,返回一个项目,即cc,其索引现在为0。单击时,显示的选定内容为aa。问题在于这些指数。您正在使用cc的新索引,该索引为0但显示旧索引,其中o是aa的索引。
几年前,当我遇到这个问题时,我遇到了完全相同的问题。我所做的是添加一个额外的字段,将原始索引存储为对象中的字段。因此,当点击上面的exaple中的cc时,我会做一些像cc.getOriginalIndex()这样会返回2并显示打印正常的结果。
如果这对您有意义,请尝试实施,或者我们会进入代码。