1

My app' s mainactivity consists of a listview that is retrived from parse.com(i know that parse is getting closed and i am also in the process of migrating).onitemclick shows information about the clicked item in singleitemview activity. This works good

I have a search activity that searches user input from list in main activity and display the search results in listview

I want to add onitemclicklistener to the search results so that it would open and show information corresponding to the search ressult clicked in singleitemview activity as it does in main activity

Now my problem is, I'll try to explain with example

let my main activity list be

  1. America 1
  2. America 2
  3. America 3
  4. Germany 1
  5. Germany 2
  6. Germany 3
  7. France 1
  8. France 2
  9. France 3
  10. Italy 1

now the user searches for Germany in edittext of search activity

The search results displayed are

  • Germanny 1
  • Germany 2
  • Germany 3

If I click germany 1 item from search results it showing information about America 1 from main activity, ,in singleitemview activity

If I click germany 2 item from search results it showing information about America 2 from main activity in singleitemview activity and so on...

What shall i do to show correct information for search results

the search activity code

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);



    }
 }

Codelist

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;
}

}

getview() of fnlwdapter

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;
}
user5894647
  • 544
  • 6
  • 15

2 Answers2

1

You are getting position of item from filtered list (list after search), but you are fetching item from the unfiltered list. This is causing the new activity to show wrong data. Get the item from currently loaded list.

Replace

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

with

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;
    }
}

in onItemClick

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

try setting custom adapter to searchedlist. as of now you are setting ArrayAdapter<String> instead of that try settings adapter of ArrayAdapter<ArrayList<CodeList>>

and then in onItemClick you will get CodeList object; then you can use this object to pass to next activity via intent

Amol Desai
  • 872
  • 1
  • 9
  • 17