ListView,减少ListView中一个元素与另一个元素之间的空间

时间:2015-09-06 23:19:02

标签: javascript android xml listview android-listview

我有一个应用程序从XML文件中选择数据,通过ListView显示它们。但是在列表中的一个elemrny和另一个elemrny之间有很多空间。这是我的班级

package com.example.giacomob.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Giacomo B on 05/08/2015.
 */
public class Page1 extends Activity {
   // private ArrayList<String> data = new ArrayList<String>();
    final int n_campi = 8; //definisco la costante del numero di campi per fermata
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page1);

        String dato1 = getIntent().getExtras().getString("NomeDati1"); //preleva la stringa
        Toast.makeText(this, dato1, Toast.LENGTH_SHORT).show();  //PROVA: questo mi fa comparire una specie di label notifica trasparente con il valore di "data1"
        Log.d("TAG", "data1:" + dato1); //credo sia una specie di debug

        // seconda stringa
        String destination_nf =  getIntent().getExtras().getString("Destinazione_scelta");
        String destination_f = destination_nf.trim(); //tolgo gli spazi a inzio e fine
        String destination = destination_f.substring(0, 1).toUpperCase() + destination_f.substring(1,destination_f.length()).toLowerCase(); //Faccio in modo che l'utente mi passi una stringa con prima maiuscola e le altre minuscole
        Toast.makeText(this, destination, Toast.LENGTH_SHORT).show();



        //    String salve =  getIntent().getExtras().getString("ciao");

        int c=0, j=0, n=0;
        boolean verify = false;

        //FARE CONTROLLO IN CASO IL FILE XML E' VUOTO E QUINDI LA STRINGA E' VUOTA
        String[] arr = dato1.split("\\|");
        for (int i = 0; i < arr.length; i++) {
            System.out.println(i + " => " + arr[i]);
        }
        final ArrayList<String> listp = new ArrayList<String>();
        for (int i=3; i<arr.length-1; i=i+n_campi) {

            if(arr[i].equals(destination)) {
                System.out.println(i + " des " + arr[i]);
                for(; j<n_campi+c; j++)
                    if(j==0+c) {
                        n++;
                        listp.add("Informazioni fermata numero " +n);
                        listp.add("Coordinata X: " + arr[j]);
                    }
                    else if(j==1+c)
                        listp.add("Coordinata Y: " + arr[j]);
                    else if (j==2+c)
                        listp.add("Nazione di destinazione: " + arr[j]);
                    else if (j==3+c)
                        listp.add("Paese di destinazione: " + arr[j]);
                    else if (j==4+c)
                        listp.add("Via di destinazione: " + arr[j]);
                    else if (j==5+c)
                        listp.add("Id Corriera: " + arr[j]);
                    else if (j==6+c)
                        listp.add("Paese Fermata: " + arr[j]);
                    else if (j==7+c) {
                        listp.add("Via fermata: " + arr[j]);
                        listp.add("   ");
                    }

                    verify = true;

            }
            else
            {
                j=j+n_campi;
            }

            c = j;
        }
        if(verify == false)
            Toast.makeText(this,"Non sono stati trovati mezzi per " + destination, Toast.LENGTH_SHORT).show();



        final ListView mylist = (ListView) findViewById(R.id.listView1);



        mylist.setAdapter(new MyListAdaper(this, R.layout.activity_page1, listp));


        Button b_load = (Button) findViewById(R.id.button_send2);
        b_load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                Intent openTrack = new Intent(Page1.this, Track.class);

                //String ciao = "ahahah";
                //String data1 = ReadXMLFile.readXMLFile(Page1.this);
                openTrack.putExtra("Destination", getIntent().getExtras().getString("NomeDati1"));
                startActivity(openTrack);

            }
        });
        System.out.println("ciao"+listp.size());
    }

    private class MyListAdaper extends ArrayAdapter<String> {
        private int layout;



        public MyListAdaper(Context context, int resource, List<String> objects) {
            super(context, resource, objects);
            layout = resource;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

                ViewHolder mainViewHolder = null;
                System.out.println("position " + position); //IS THE REAL POSITION
                //   if(convertView == null ){
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(layout, parent, false);
                ViewHolder viewHolder = new ViewHolder();
                // viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail)
                viewHolder.title = (TextView) convertView.findViewById(R.id.textView2);


                viewHolder.button = (Button) convertView.findViewById(R.id.button_send2);



                if (position != 0 && position % 9 == 0) {
                    viewHolder.button.setVisibility(View.VISIBLE);
                    //  viewHolder.title.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.button.setVisibility(View.GONE);
                    //viewHolder.title.setVisibility(View.GONE);
                }
                System.out.println("positionMiddle " + position); //APPEARS ONLY 2 TIMES WITH 0 AND 1
                viewHolder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        System.out.println("position2 " + position); // IS ONLY 0 OR 1
                        Toast.makeText(getContext(), "Bottone cliccato" + position, Toast.LENGTH_SHORT).show();

                    }

                });
                convertView.setTag(viewHolder);
                //   }
                // else {
                mainViewHolder = (ViewHolder) convertView.getTag();
                mainViewHolder.title.setText(getItem(position));
                //}
          //  }
            return convertView;

        }


    }
    public class ViewHolder {
        //ImageView  thumbnail; //forse non serve
        TextView title;
        Button button;
    }
    }

这是我的活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <TextView
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/string_destination"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    />




    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="359dp"
        android:layout_below="@+id/textList"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/string_loadfermata"
        android:id="@+id/button_send2"
        android:layout_gravity="center_horizontal"
        android:onClick="sendMessage"/>
</LinearLayout>

这是我的问题的一个例证: http://i.imgur.com/32sGcxx.jpg

3 个答案:

答案 0 :(得分:0)

在您的活动布局文件中,您已设置&#39; layout_height&#39; ListView的属性为&#39; 359dp&#39;,这是您在图片1和3中出现问题的原因。要解决此问题,请更改&#39; layout_height&#39; to&#39; wrap_content&#39;。

对于图片2,我猜你可能犯了同样的错误。你能展示一下列表项的布局文件吗?

答案 1 :(得分:0)

您应该使用RelativeLayout替换LinearLayout,然后为ListView设置minHeight和maxHeight,以便适合您的屏幕。并使用按钮,添加display_below字段,其id为ListView的id。

答案 2 :(得分:0)

您忘记附加行布局了。你能否附上listview行布局,可能是该文件中的问题。