充分利用列表视图下一首和上详细数据(Getting Next and Previous Detail

2019-07-21 11:26发布

我不知道如何做一个功能通过点击基于列表视图下一个/上一个按钮,显示下一个/上详细数据。

这是我想做的屏幕截图

我的列表视图活动

我的详细资料的活动 ,展现从列表视图下/上一数据

编辑:我有一个ListView数据(使用XML解析器),并将数据传递到名为“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代码:

  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”从列表视图项显示详细数据:

@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”活动的一个/上一个按钮,显示列表视图,从下一个/上详细数据。 请帮助我的人...

预先感谢

Answer 1:

刚看到过去后您的请求。

通过Parvaz给出的解决方案是完美的,你只需要弄清楚的代码。 你有一个ArrayList“PremiumList”,这是负责你的ListView行的数据。 所有你需要的是通过位置,这份名单Detail_toko.class(通过使用parceable或声明的ArrayList静态意图(不推荐)等),您的解决方案将是几个步之遥。

在Detail_toko.class创建一个全局变量currentPosition将从过去的活动从意图得到其数值喜欢TAG_NAMATOKO。 于是,在下一个按钮递增1至currentPosition的onClickListener并从你的ArrayList你刚刚从去年的活动运输正如你在getView方法做了细节。

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

设置这些值和新视图将随之更新。

在onClickListener PREVIOUS按钮从currentPosition递减1,随后相同的代码(这意味着必须创建一个方法并调用,无码重复)。

试试吧。 这是尽可能详细,我们可以在不实际做你的功课解释!

编辑:

首先,要从OnItemClick这条线

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

您是从PremiumList在这一行清除所有数据和这样你会得到下一个活动空白列表。 因此,摆脱这一行的。 一旦你会在你的下一个活动得到PremiumList,所有你需要的是位置和totalData与它一起在意图上额外的费用。 你可以从你的意图额外摆脱TAG_NAMATOKO,TAG_KATEGORI,TAG_EMAIL的。

我告诉过你不要重复代码,因为你是初学者你跟着,因为它会让你的生活轻松通过采取这样的考虑,使你的习惯中是非常重要的。

现在,您在“Detail_Toko”代码将变为:

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

建议:使用ViewHolder填充视图getView方法,它会使你的适配器“的efficent适配器”。 (搜索例子)按照命名规则,所有。 使用,并bundle.containKey等东西,以避免致命的异常像NullPointerException异常前检查anyObject!= NULL,它们的大小或长度。



Answer 2:

只需通过PremiumList的ArrayList您Detail_toko活动......无论是意图传递或直接从getter方法得到它。 然后,在每一个/上一个按钮,点击增量的Detail_toko活动的ArrayList的位置来获取数据,然后相应地填充。

这是你将如何通过你的ArrayList 通过意图传递的ArrayList

和而不是从意向获取数据的其余部分得到ArrayList中的数据。

像arraylist.get(索引)。

像这样

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

/ ************************使用像这里给出的方法如何做下一个按钮动作? ************ /

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


文章来源: Getting Next and Previous Detail Data from listview