Android Listview新项目在顶部

时间:2017-03-30 07:39:55

标签: android android-studio

我有一个Listview。我想订购它,以便当新项目添加时,它们会添加到顶部。我试试

Collections.reverse(ListElementsArrayList); 

它有效。但在(第二次,第三次点击,....)之后它混合了所有早期的项目。这是可能的。如果有,请帮助我。

我的主要活动

package com.pradeep.ap;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class AP extends AppCompatActivity {

EditText A, B, D, N, AN;
Button a, b, d, n, an, AP, GP, HP;
int Count = 0;
ListView LV;

String[] ListElements = new String[]{};
List<String> ListElementsArrayList;
ArrayAdapter<String> adapter;
Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ap);

    A = (EditText) findViewById(R.id.eta);
    B = (EditText) findViewById(R.id.etb);
    D = (EditText) findViewById(R.id.etd);
    N = (EditText) findViewById(R.id.etn);
    AN = (EditText) findViewById(R.id.etan);
    AP = (Button) findViewById(R.id.apap);
    GP = (Button) findViewById(R.id.apgp);
    HP = (Button) findViewById(R.id.aphp);

    a = (Button) findViewById(R.id.a);
    b = (Button) findViewById(R.id.b);
    d = (Button) findViewById(R.id.d);
    n = (Button) findViewById(R.id.n);
    an = (Button) findViewById(R.id.an);
    LV = (ListView) findViewById(R.id.lv);



    handler = new Handler();
    ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
    adapter = new ArrayAdapter<String>(AP.this,android.R.layout.simple_list_item_1,
            ListElementsArrayList);
    LV.setAdapter(adapter);
    adapter.notifyDataSetChanged();




    a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float numb = 0;
            float numd = 0;
            float numleal=0;
            float result = 0;
            int Result;
            if (TextUtils.isEmpty(B.getText().toString())
                    || TextUtils.isEmpty(D.getText().toString())) {
                return;
            }
            numb = Float.parseFloat(B.getText().toString());
            numd = Float.parseFloat(D.getText().toString());
            ListElementsArrayList.add("  First  term  of  AP  is  =  " + A.getText().toString());
            result = numb - numd;
            Count = LV.getAdapter().getCount() + 1;
            Result = (int) result;
            A.setText("" + Result);
            adapter.notifyDataSetChanged();
            Collections.reverse(ListElementsArrayList);
        }
    });

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float numa = 0;
            float numd = 0;
            float result = 0;
            int Result;
            Count = LV.getAdapter().getCount() + 1;
            if (TextUtils.isEmpty(A.getText().toString())
                    || TextUtils.isEmpty(D.getText().toString())) {
                return;
            }
            numa = Float.parseFloat(A.getText().toString());
            numd = Float.parseFloat(D.getText().toString());
            result = numa + numd;
            Result = (int) result;
            B.setText("" + Result);
            ListElementsArrayList.add("  Second  term  of  AP  is  =  " + B.getText().toString());
            adapter.notifyDataSetChanged();
            Collections.reverse(ListElementsArrayList);

        }
    });

    d.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float numa = 0;
            float numb = 0;
            float result = 0;
            int Result;
            if (TextUtils.isEmpty(A.getText().toString())
                    || TextUtils.isEmpty(B.getText().toString())) {
                return;
            }
            numa = Float.parseFloat(A.getText().toString());
            numb = Float.parseFloat(B.getText().toString());
            result = numb - numa;
            Result = (int) result;
            Count = LV.getAdapter().getCount() + 1;
            D.setText("" + Result);
            ListElementsArrayList.add("  Value  of  D  is  =  " + D.getText().toString());
            adapter.notifyDataSetChanged();
            Collections.reverse(ListElementsArrayList);
        }
    });
    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float numa = 0;
            float numd = 0;
            float numan = 0;
            float result = 0;
            int Result;
            if (TextUtils.isEmpty(A.getText().toString())
                    || TextUtils.isEmpty(D.getText().toString())
                    || TextUtils.isEmpty(AN.getText().toString())) {
                return;
            }
            numa = Float.parseFloat(A.getText().toString());
            numd = Float.parseFloat(D.getText().toString());
            numan = Float.parseFloat(AN.getText().toString());
            result = ((numan - numa) / numd) + 1;
            Result = (int) result;
            Count = LV.getAdapter().getCount() + 1;
            N.setText("" + Result);
            ListElementsArrayList.add("  Number  of  term/terms  in  this  AP  is  =  " + N.getText().toString());
            adapter.notifyDataSetChanged();
            Collections.reverse(ListElementsArrayList);
        }
    });

    an.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float numa = 0;
            float numd = 0;
            float numn = 0;
            float result = 0;
            int Result;
            if (TextUtils.isEmpty(A.getText().toString())
                    || TextUtils.isEmpty(D.getText().toString())
                    || TextUtils.isEmpty(N.getText().toString())) {
                return;
            }
            numa = Float.parseFloat(A.getText().toString());
            numd = Float.parseFloat(D.getText().toString());
            numn = Float.parseFloat(N.getText().toString());
            result = ((numn - 1) * numd) + numa;
            Result = (int) result;
            AN.setText("" + Result);
            Count = LV.getAdapter().getCount() + 1;
            ListElementsArrayList.add("  Term/Terms of this AP  is  =  " + AN.getText().toString());
            adapter.notifyDataSetChanged();
            adapter.getCount();
            Collections.reverse(ListElementsArrayList);
        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

只需使用

List.add(0, element)

添加元素作为列表的第一个元素。如documentation

中所述