0

i don't know how to make a function to show the next/previous detail data by clicking the next/previous button based on listview.

This is the screenshot that i want to do

My listview Activity

My Detail data Activity, to show next/previous data from listview

EDIT : i have a listview data (use xml parser), and pass the data to new acitivity called "Detail_toko.class" :

                List<NameValuePair> paramemeter = new ArrayList<NameValuePair>();
                paramemeter.add(new BasicNameValuePair("keyword", "a"));  
                // paramemeter.add(new BasicNameValuePair("kategori",Category.getSelectedItem().toString()));

                XMLParser parser = new XMLParser();
                String xml = parser.getXmlFromUrl(URL, paramemeter); // getting XML from URL
                Document doc = parser.getDomElement(xml); // getting DOM element
                NodeList nl = doc.getElementsByTagName(TAG_DETAIL);
                // looping through all song nodes <song>
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, parser.getValue(e, TAG_ID));
                    map.put(TAG_NAMATOKO, parser.getValue(e, TAG_NAMATOKO));
                    map.put(TAG_KATEGORI, parser.getValue(e, TAG_KATEGORI));
                    map.put(TAG_EMAIL, parser.getValue(e, TAG_EMAIL));
                    map.put(TAG_ICON, parser.getValue(e, TAG_ICON));


                    // adding HashList to ArrayList
                    PremiumList.add(map);



                        } 

            return null;

           }


            /**
             * Updating parsed JSON data into ListView
             * */
            list=(ListView)findViewById(R.id.listview);

            if(PremiumList.size() > 0)
                    {

                adapter=new LazyAdapterStore(ListPerusahaan.this, PremiumList);        
                list.setAdapter(adapter);
            }

            else
                    {
                Toast toast= Toast.makeText(getApplicationContext(), "No data found, enter another keyword", Toast.LENGTH_SHORT);  
                    toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();
                    }

                // Click event for single list row
                    list.setOnItemClickListener(new OnItemClickListener() {

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

                            // getting values from selected ListItem
                            String nama_toko = ((TextView) view.findViewById(R.id.nama_toko)).getText().toString();
                            String kategori = ((TextView) view.findViewById(R.id.kategori)).getText().toString();
                            String email = ((TextView) view.findViewById(R.id.email)).getText().toString();



                         PremiumList = new ArrayList<HashMap<String, String>>();
                         Intent in = new Intent(ListPerusahaan.this, Detail_toko.class); 
                         in.putExtra("PremiumList", PremiumList);
                         //in.putStringArrayListExtra( "PremiumList", PremiumList );
                         in.putExtra("position", position);
                         in.putExtra("TotalData", TotalData);
                         in.putExtra(TAG_NAMATOKO, nama_toko);
                         in.putExtra(TAG_KATEGORI, kategori);
                         in.putExtra(TAG_EMAIL, email);
                         startActivity(in);

This is my LazyAdapterStore code :

  public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_data_store, null);

    TextView nama_toko = (TextView)vi.findViewById(R.id.nama_toko); // title
    TextView kategori = (TextView)vi.findViewById(R.id.kategori); // title
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
    TextView email = (TextView)vi.findViewById(R.id.email); // artist name


    HashMap<String, String> detail = new HashMap<String, String>();
    detail = data.get(position);


    // Setting all values in listview
    nama_toko.setText(detail.get(ListPerusahaan.TAG_NAMATOKO));
    kategori.setText(detail.get(ListPerusahaan.TAG_KATEGORI));
    email.setText(detail.get(ListPerusahaan.TAG_EMAIL));
    imageLoader.DisplayImage(detail.get(ListPerusahaan.TAG_ICON), thumb_image);
    return vi;

}

EDIT : and, this is my new activity "Detail_Toko" to display detail data from listview items :

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

  //this must be called BEFORE setContentView
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.detail_toko);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    call_btn=(Button)findViewById(R.id.call);

    email_btn=(Button)findViewById(R.id.email);

    sms_btn=(Button)findViewById(R.id.sms);

    Next_btn=(Button)findViewById(R.id.Next);

    Prev_btn=(Button)findViewById(R.id.Prev);

    phonenumber=(TextView)findViewById(R.id.telpon);


    // getting intent data
    Intent in = getIntent();

   // @SuppressWarnings("unchecked")
    //ArrayList<HashMap<String, String>> PremiumList = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("PremiumList");


    // Get String values from previous intent
    final String nama_toko = in.getStringExtra(TAG_NAMATOKO);
    final String kategori = in.getStringExtra(TAG_KATEGORI);
    final String email = in.getStringExtra(TAG_EMAIL);

    // Get Int values from previous intent
    final int posisi =  in.getExtras().getInt("position");
    final int Total_data =  in.getExtras().getInt("TotalData");

   /*
    Bundle bundle = in.getExtras(); 
    HashMap<String, String> list = (HashMap<String, String>) bundle.getSerializable("PremiumList");
    list = data.get(currentposition);
    */

    Bundle bundle = in.getExtras();
    HashMap<String, String> list =  (HashMap<String, String>) bundle.getSerializable("PremiumList");
    list = data.get(currentposition);

    // Displaying all values on the screen
    lblPosisi = (TextView) findViewById(R.id.namatoko);
    lbltotaldata = (TextView) findViewById(R.id.nama);
    lblNamatoko = (TextView) findViewById(R.id.nama_toko);
    lblKategori = (TextView) findViewById(R.id.kategori);
    lblEmail = (TextView) findViewById(R.id.textemail);

    lblPosisi.setText(String.valueOf(posisi));
    lbltotaldata.setText(String.valueOf(Total_data));
    lblNamatoko.setText(nama_toko);
    lblKategori.setText(kategori);
    lblEmail.setText(email);


    // Set the int value of currentposition from previous selected listview item
    currentposition = posisi;

    Next_btn.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){  

          if(currentposition >= Total_data - 1)
          {
              Toast toast= Toast.makeText(getApplicationContext(), "Last Record", Toast.LENGTH_SHORT);  
              toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
              toast.show();
          }
          else
          {

              currentposition++;
              lblPosisi.setText(String.valueOf(currentposition));
              lblNamatoko.setText(list.get(nama_toko));
              lblKategori.setText(list.get(kategori));
              lblEmail.setText(list.get(email));


          }
        }
      });


    Prev_btn.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){  

          if(currentposition <= 0)
          {
              Toast toast= Toast.makeText(getApplicationContext(), "First Record", Toast.LENGTH_SHORT);  
              toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
              toast.show();
          }
          else
          {
              currentposition--;
              lblPosisi.setText(String.valueOf(currentposition));
              lblNamatoko.setText(list.get(nama_toko));
              lblKategori.setText(list.get(kategori));
              lblEmail.setText(list.get(email));
          }


          }
     });

the problem is, i don't know how to make a function to show the next/previous detail data from listview by clicking the next/previous button in "Detail_toko" activity. Please someone Help me...

Thank in advance

Jonas
  • 121,568
  • 97
  • 310
  • 388
Arsyah
  • 79
  • 3
  • 11

2 Answers2

3

Just seen your request on last post.

Solution given by Parvaz is perfect, you just need to figure out the code. You have an arraylist "PremiumList" which is responsible for your listview rows data. All you need is to pass position and this list to Detail_toko.class (via intent using parceable or declaring arraylist as static (not recommended) etc. ) and your solution will be couple of steps away.

In Detail_toko.class create a global variable currentPosition which will get its value from past activity like TAG_NAMATOKO from intent. Then in onClickListener of NEXT button increment 1 to currentPosition and get detail from your arraylist you have just transported from last activity as you have done in your getView method.

 HashMap<String, String> detail = new HashMap<String, String>();
    detail = data.get(currentPosition);


// Setting all values in listview
nama_toko = detail.get(ListPerusahaan.TAG_NAMATOKO);
kategori = detail.get(ListPerusahaan.TAG_KATEGORI);
email = detail.get(ListPerusahaan.TAG_EMAIL);

Set these values and your new view will be updated accordingly.

In onClickListener of PREVIOUS button decrement 1 from currentPosition, followed by same code (which means you must create a method and call that, no code repetition).

Try it. This is as detailed as we can explain without actually doing your homework !

EDIT :

First of all remove this line from OnItemClick

 PremiumList = new ArrayList<HashMap<String, String>>();

You are clearing all the data from PremiumList in this line and this way you will get blank list in next activity. So get rid of this line. Once you will get PremiumList in your next activity, all you need is position and totalData along with it in intent extra. You can get rid of TAG_NAMATOKO, TAG_KATEGORI,TAG_EMAIL from your intent extra.

I had told you to not to repeat the code, as you are beginner it is very important for you to follow as it will make your life easy by taking such considerations and making your habbit.

Now your code in "Detail_Toko" will become:

 HashMap<String, String> list = null;
 int currentposition = 0;
 int  Total_data =0;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

  //this must be called BEFORE setContentView
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.detail_toko);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    call_btn=(Button)findViewById(R.id.call);

    email_btn=(Button)findViewById(R.id.email);

    sms_btn=(Button)findViewById(R.id.sms);

    Next_btn=(Button)findViewById(R.id.Next);

    Prev_btn=(Button)findViewById(R.id.Prev);

    phonenumber=(TextView)findViewById(R.id.telpon);

// Displaying all values on the screen
    lblPosisi = (TextView) findViewById(R.id.namatoko);
    lbltotaldata = (TextView) findViewById(R.id.nama);
    lblNamatoko = (TextView) findViewById(R.id.nama_toko);
    lblKategori = (TextView) findViewById(R.id.kategori);
    lblEmail = (TextView) findViewById(R.id.textemail);

    // getting intent data
   Bundle bundle = getIntent().getExtras();
// Get Int values from previous intent
    currentposition =  bundle.getInt("position");
      Total_data =  bundle.getInt("TotalData");
    list =  (HashMap<String, String>) bundle.get("PremiumList");
    setView();
    Next_btn.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){  
        if(currentposition >= Total_data - 1)
          {
              Toast toast= Toast.makeText(getApplicationContext(), "Last Record", Toast.LENGTH_SHORT);  
              toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
              toast.show();
          }
          else
          {
             currentposition++;
             setView();
          }
        }
      });


    Prev_btn.setOnClickListener(new Button.OnClickListener(){
      public void onClick(View v){  

          if(currentposition <= 0)
          {
              Toast toast= Toast.makeText(getApplicationContext(), "First Record", Toast.LENGTH_SHORT);  
              toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
              toast.show();
          }
          else
          {
              currentposition--;
              setView();
          }
        }
     });

     private void setView()
     {
     if(list != null && list.size() > currentposition)
     {
     HashMap<String, String> detail = new HashMap<String, String>();
    detail = list.get(currentposition);
     lblPosisi.setText(String.valueOf(currentposition));
    lbltotaldata.setText(String.valueOf(Total_data));
    lblNamatoko.setText(detail.get(ListPerusahaan.TAG_NAMATOKO));
    lblKategori.setText(detail.get(ListPerusahaan.TAG_KATEGORI));
    lblEmail.setText(detail.get(ListPerusahaan.TAG_EMAIL));
    }
    }

Suggestion: Use ViewHolder to populate view in getView Method, it will make your adapter "An efficent adapter". (search for examples) Follow naming convention and all. Check anyObject != null , their size or length before using them and bundle.containKey etc stuff to avoid fatal exception like nullPointerException.

Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
  • Hi Mr.Sourab Sharma, i've spend my last four days to learn how to fix my problems about next/previous detail data from listview. I've followed your instructions and i did some functions, i've got currentposition, totaldata, and Arraylist "PremiumList" from listview, so when i push next/previous button, the value of index/position will be updated accordingly, when we push the button in the last index, it will show toast "last record". i did this functions, but my detail data still not change...:( What I'm doing wrong? what I'm missing? i've edit my code, please check it out Mr.Sourab Sharma – Arsyah Mar 18 '13 at 09:52
  • First of all, Thank you very much for your answers and suggestion Mr.Sourab Sharma, speechless for all of your helps. you are a good teacher for me in android programming...:) but, there is one error in "Detail_toko" activity.. in line "detail = list.get(currentposition);" the editor said "Type mismatch: cannot convert from String to HashMap" what i'm missing? – Arsyah Mar 19 '13 at 07:19
  • Thank God...Thank Mr.Sourab Sharma...My problem was solved....the error is in the "HashMap list = null;" should be "Arraylist HashMap list = null;" God Bless you Mr.Sourab Sharma...:) – Arsyah Mar 19 '13 at 07:39
  • Thanks :) But remember any time you ask question on SO, you must have done all related R&D before that and must have put your best efforts to resolve the issue. This way only you will become brilliant developer otherwise any one will become only dependent on seniors. TC. – Sourab Sharma Mar 19 '13 at 09:13
  • hi @SourabSharma, can you help me in this problem [link](http://stackoverflow.com/questions/21573810/pass-string-from-fragment-main-activity-to-fragments-activity-in-viewpager) please... – Arsyah Feb 05 '14 at 10:24
0

Just pass the PremiumList ArrayList to your Detail_toko Activity...either pass it by Intent or get it directly from the getter methods. Then in your Detail_toko activity on each next / previous button click increment the position of arraylist to get the data and then populate accordingly.

This is how you will pass your arraylist Passing ArrayList through Intent

and instead of getting the rest of the data from intent get the data from the arraylist.

like arraylist.get(index).

just like this

  /********Instead of getting your values like this*******************/
String nama_toko = in.getStringExtra(TAG_NAMATOKO);
String kategori = in.getStringExtra(TAG_KATEGORI);
String email = in.getStringExtra(TAG_EMAIL);

/************************Use a method like given here How to do the next button action?****************************/

  //The  method setvalue would be quite useful for you where you will pass the int j parameter to get appropriate values. If next button is clicked increment the j if previous button is clicked decrement it. and then display your values asusual.
// Displaying all values on the screen
lblNamatoko = (TextView) findViewById(R.id.namatoko);
lblKategori = (TextView) findViewById(R.id.kategori);
lblEmail = (TextView) findViewById(R.id.textemail);

lblNamatoko.setText(nama_toko);
lblKategori.setText(kategori);
lblEmail.setText(email);
Community
  • 1
  • 1
Parvaz Bhaskar
  • 1,367
  • 9
  • 29
  • Thank for the answer @Parvaz, but i'm a newbie in android..... i'm still confused... can you show me the code...:) – Arsyah Mar 13 '13 at 04:26
  • ok....i got it, how to passing "PremiumList" from activity 1 to activity 2 but, how to make next/previous button function to show the data....sorry...:( – Arsyah Mar 13 '13 at 04:49
  • hi @Parvaz, my listview data use hashmap...it has a problem when passing "PremiumList" to another activity use "Intent i = getIntent(); PremiumList = i.getStringArrayListExtra("PremiumList");" how to solve this problem? :D – Arsyah Mar 13 '13 at 07:38
  • if you want to pass the hashmap itself that you created you can refer this http://stackoverflow.com/questions/4992097/android-how-to-pass-hashmapstring-string-between-activities – Parvaz Bhaskar Mar 13 '13 at 07:48
  • Thank @Parvaz, but can you show me sample code for "Next_btn" button click listener in my "Detail_toko" activity...so, i can display the next data from listview after clicked Next Button... honestly, your explanation still complecated for newbie like me..:D – Arsyah Mar 13 '13 at 07:58
  • Sorry I can't provide you the code itself..I've given you the implementation.. you can search on SO or google it for the rest of the part. I've given a way to implement it...you'll have to code it yourself. – Parvaz Bhaskar Mar 13 '13 at 08:01
  • Ok...Thank u very much for the explanation @Parvaz...i'm so appreciated your good will...i'll try harder to fix my problem...:) – Arsyah Mar 13 '13 at 08:06
  • just follow the answers that you got and try to search what you don't know. I know its hard..but SO works this way. And feel free to ask any doubts. – Parvaz Bhaskar Mar 13 '13 at 08:18