我的应用程序的主要活动包括从parse.com重新获得的列表视图(我知道解析正在关闭,我也在迁移过程中).onitemclick显示有关singleitemview活动中被点击项目的信息。这很好用
我有一个搜索活动,可以从主要活动的列表中搜索用户输入,并在列表视图中显示搜索结果
我想在搜索结果中添加onitemclicklistener,以便打开并显示与在singleitemview活动中点击的搜索结果相对应的信息,就像在主要活动中一样
现在我的问题是,我将尝试用示例解释
让我的主要活动列表
现在用户以搜索活动的编辑文本搜索德国
显示的搜索结果是
如果我从搜索结果中点击德国1项目,它会显示主要活动中有关美国1的信息,在singleitemview活动中
如果我从搜索结果中点击德国2项目,它会从singleitemview活动中的主要活动显示关于美国2的信息 依此类推......
如何显示搜索结果的正确信息
搜索活动代码
public class SearchActivity extends Activity implements OnItemClickListener
{
protected EditText searchedittext;
ImageButton searchButton;
List<ParseObject> ob;
List<CodeList> codelist = null;
FinalAdapter fnladapter;
@Override
public void onCreate(Bundle savedInstanceState )
{
// TODO: Implement this method
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
searchedittext = (EditText) findViewById(R.id.search_layoutEditText);
final ListView searchedlist = (ListView) findViewById(R.id.searchlist);
searchedlist.setOnItemClickListener(this);
searchButton = (ImageButton) findViewById(R.id.searchlayoutbtn);
searchButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String seaechedit = searchedittext.getText().toString();
if(seaechedit.isEmpty()){
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
builder.setMessage("PLEASE ENTER SOME SEARCH QUERY")
.setTitle("EMPTY SEARCH")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else{
setProgressBarIndeterminateVisibility(true);
new RemoteDataTask().execute();
// InterActivity7 is the class name in parse database where listview retrives it data from
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
query.whereContains("listheading", seaechedit);
query.orderByAscending("_created_at");
query.setLimit(200);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> p1, ParseException e)
{
setProgressBarIndeterminateVisibility(false);
if(e == null){
ob = p1;
String [] searchHeadings = new String[ob.size()];
int i = 0;
// listheading is the coloumn name in parse database
for(ParseObject heading : ob){ searchHeadings[i] = (String) heading.get("listheading");
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>( SearchActivity.this, android.R.layout.simple_list_item_1, searchHeadings );
searchedlist.setAdapter(adapter);
}else{
Log.e("searchactivity", e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
builder.setMessage(e.getMessage())
.setTitle("Nothing found")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
codelist = new ArrayList<CodeList>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"InterActivity");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("_created_at");
ob = query.find();
for (ParseObject inter : ob) {
map.setIntroduction((String) inter.get("intro"));
codelist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
fnladapter = new FinalAdapter(SearchActivity.this,
codelist);
}
}
@Override
public void onItemClick(AdapterView<?> p1, View p2, int position, long p4)
{
Intent intent = new Intent(SearchActivity.this, SingleItemView.class);
intent.putExtra("intro",
(codelist.get(position).getIntroduction()));
// Start SingleItemView Class
// startActivity(intent);
startActivityForResult(intent, 1);
}
}
编码表
public class CodeList
{
private String listHeading;
private String introduction;
public void setListHeading(String listHeading)
{
this.listHeading = listHeading;
}
public String getListHeading()
{
return listHeading;
}
public void setIntroduction(String introduction)
{
this.introduction = introduction;
}
public String getIntroduction()
{
return introduction;
}
}
fnlwdapter的getview()
public View getView(final int position, View view, ViewGroup parent)
{
final ViewHolder holder;
if(view == null){
holder = new ViewHolder();
view = inflater.inflate(R.layout.beg_list_item,null);
holder.listHeading = (TextView) view.findViewById(R.id.beg_list_itemTextView);
holder.favariteImage = (ImageView) view.findViewById(R.id.favbtn);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
CodeList codes = (CodeList) getItem(position);
holder.listHeading.setText(codeList.get(position).getListHeading());
if (checkFavoriteItem(codes)) {
holder.favariteImage.setImageResource(R.drawable.favorite);
holder.favariteImage.setTag("yes");
} else {
holder.favariteImage.setImageResource(R.drawable.unfavorite);
holder.favariteImage.setTag("no");
}
return view;
}
答案 0 :(得分:1)
您从过滤列表中获取项目的位置(搜索后列表),但您从未过滤的列表中获取项目。这导致新活动显示错误数据。从当前加载的列表中获取项目。
替换
intent.putExtra("intro",
(codelist.get(position).getIntroduction()));
带
String value = (String)p1.getItemAtPosition(position);
for(int i = 0; i < codelist.size(); i++)
{
CodeList itemData = (CodeList)codelist.get(i);
String temp = itemData.getListHeading();
if(value.equalsIgnoreCase(temp))
{
intent.putExtra("intro", itemData.getIntroduction());
intent.putExtra("conclusion", itemData.getconclusion());
break;
}
if(i == codelist.size() - 1)
{
Log.e("onItemClick", "No item found to display");
retrun;
}
}
onItemClick
中的
答案 1 :(得分:0)
尝试将自定义适配器设置为searchedlist
。截至目前,您正在设置ArrayAdapter<String>
而不是ArrayAdapter<ArrayList<CodeList>>
然后在onItemClick
中你会获得CodeList
个对象;然后您可以使用此对象通过intent