搜索活动listview的onitemclicklistener显示的信息与预期不同

时间:2016-02-25 06:25:58

标签: android listview search parse-platform onitemclicklistener

我在activityA中有一个listview,其中第1个listem是测试标题1,第2个项目是测试标题2,从第3个列表项目是列表标题8,列表标题9,所以如下图所示

enter image description here

enter image description here

现在我在活动A中有一个按钮,用于打开新活动,即搜索活动,从活动A enter image description here

列表中搜索用户输入

直到这里一切正常但我在查看onitemclicklistener时遇到问题

问题是::

例如,如果用户在搜索活动的editext中搜索列表,则会显示包含单词列表的列表项的searc结果,即

列出标题8 清单标题9 清单标题10 清单标题11 等等.... 如果用户单击列表标题8项目,它应该打开单一项目视图和对应于列表标题8的数据,如同它在活动A中显示的那样,但它显示属于活动A的第一个列表项的数据,依此类推搜索活动

如何将正确的单项视图数据显示为正确的搜索活动搜索结果中显示的列表项

我的搜索活动

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)
{
    ObjectMapper mapper = new ObjectMapper();
    CodeList codes = (CodeList) fnladapter.getItem(position);


    try{

        Intent intent = new Intent(SearchActivity.this, SingleItemView.class);

        String jsonString = mapper.writeValueAsString(codes);



        intent.putExtra("intro",
                        (codelist.get(position).getIntroduction()));



        // Start SingleItemView Class
        //   startActivity(intent);
        startActivityForResult(intent, 1);

    }catch(JsonProcessingException e){
        //something went w3ong
    }

    }
 }

1 个答案:

答案 0 :(得分:0)

而不是位置使用id来获取数据

更改

CodeList codes = (CodeList) fnladapter.getItem(position);

to
CodeList codes = (CodeList) fnladapter.getItem((int)p4);