从列表视图中获取下一个和上一个详细信息 [英] Getting Next and Previous Detail Data from listview

查看:31
本文介绍了从列表视图中获取下一个和上一个详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何通过单击基于列表视图的下一个/上一个按钮来显示下一个/上一个详细数据的功能.

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.

这是我想做的截图

我的列表视图活动

我的详细数据活动,显示来自列表视图

我有一个列表视图数据(使用 xml 解析器),并将数据传递给名为Detail_toko.class"的新活动:

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

这是我的 LazyAdapterStore 代码:

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;

}

,这是我的新活动Detail_Toko",用于显示列表视图项目的详细数据:

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


          }
     });

问题是,我不知道如何通过单击Detail_toko"活动中的下一个/上一个按钮来创建一个函数来显示列表视图中的下一个/上一个详细信息数据.请有人帮助我...

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...

提前致谢

推荐答案

刚刚在上一个帖子中看到您的请求.

Just seen your request on last post.

Parvaz 给出的解决方案是完美的,你只需要弄清楚代码.您有一个数组列表PremiumList",它负责您的列表视图行数据.您所需要的只是将位置和此列表传递给 Detail_toko.class(通过使用 parceable 的意图或将 arraylist 声明为静态(不推荐)等),您的解决方案将在几步之外.

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.

在 Detail_toko.class 中创建一个全局变量 currentPosition,它将从过去的活动中获取其值,例如从意图中获取 TAG_NAMATOKO.然后在 NEXT 按钮的 onClickListener 中将 1 增加到 currentPosition 并从您刚刚从上一个活动传输的数组列表中获取详细信息,就像您在 getView 方法中所做的那样.

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.

在 PREVIOUS 按钮的 onClickListener 中,从 currentPosition 减 1,然后是相同的代码(这意味着您必须创建一个方法并调用该方法,没有代码重复).

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 !

首先从 OnItemClick 中删除这一行

First of all remove this line from OnItemClick

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

您正在清除此行中 PremiumList 中的所有数据,这样您将在下一个活动中获得空白列表.所以摆脱这条线.一旦您在下一个活动中获得 PremiumList,您所需要的只是 position 和 totalData 以及它在 Intent 中的额外内容.您可以从您的 Intent 中删除 TAG_NAMATOKO、TAG_KATEGORI、TAG_EMAIL.

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.

现在您在 Detail_Toko" 中的代码将变为:

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

建议: 在 getView 方法中使用 ViewHolder 来填充视图,这将使您的适配器成为一个高效的适配器".(搜索示例)遵循命名约定和所有.在使用它们和 bundle.containKey 等之前检查 anyObject != null ,它们的大小或长度,以避免像 nullPointerException 这样的致命异常.

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.

这篇关于从列表视图中获取下一个和上一个详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆