Search code examples
androidlistviewandroid-activitylistviewitem

How to Highlight the Item (Listview) of Next Activity After Clicking the Item(Listview) of Previous Activity?


I have two Fragments. One Fragment Consist of Questions While OtherFragment consist of Answers. For Example If one person click on Question(8) It means u can say that Item No 8, then it should go to next activity and highlight the Answer (8) of that Question. I am stuck. I shall be thank full to you. Here is Some Code Which are sending Question No or list Item no to next Activity and next Activity code receiving this Question no and i saved it into Interger "a". Now what type of IF condition can work in the Next Activity?

First Activity

lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int index = position + 1;
            Intent it = new Intent(this.getActivity(), NextActivity.class);
            it.putExtra("Qlist", index);
            startActivity(it);


        }

Next Activity

Intent it = getActivity().getIntent();

    int a = it.getIntExtra("Qlist", 0);

    String result=String.valueOf(a);

    Toast.makeText(getActivity(), ""+a, Toast.LENGTH_LONG).show();

ListView Adapter code of First Activity (using SQlite)

DatabaseAccess db=DatabaseAccess.getInstance(getActivity().getApplicationContext());



    List<String> lst;
    db.open();
    lst=db.getexamq();
    db.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ExamQ.this.getActivity(), android.R.layout.simple_list_item_1, lst);
    lv.setAdapter(adapter);

ListView Adapter code of Next Activity (using SQlite)

DatabaseAccess db=DatabaseAccess.getInstance(getActivity().getApplicationContext());



    List<String> lst;
    db.open();
    lst=db.getexamq();
    db.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ExamA.this.getActivity(), android.R.layout.simple_list_item_1, lst);
    lv.setAdapter(adapter);

Solution

    1. Use the intent Bundle to move the questionId or question No to the next activity
    2. In the adapter of the Questions ListView, set a different background color for the question with a similar questionId. You can even set the background to have a fade animation or change to normal after a certain number of milliseconds or seconds.

    ALL THE CODE REQUIRED IS A LOT Write something(some code), and we can help on the ListViewAdapter in case it doesn't work

    EDIT: SAMPLE FAST CUSTOM ADAPTER CODE

        lv.setAdapter(new ArrayAdapter(ExamQ.this.getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, lst){
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View v = convertView;
                final String examQ = lst[position];
    
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(android.R.layout.simple_list_item_1, null);
                }
    
                TextView tv = (TextView) v.findViewById(android.R.id.text1);
                tv.setText(examQ);
    
    
                if( examQ.equals(result)) { // Unique identifier here
                    v.setBackground(Color.RED); //Some other fancy color here
                }
                return v;
            }
        });
    

    However, I just discovered you do not have a unique identifier for each question. So for the time being just use the question string and pass it to the next activity