简体   繁体   中英

Android Listview new item at top

I have a Listview. I want to order it such that when new item added they add at top. I try

Collections.reverse(ListElementsArrayList); 

And it works. But after (second,third click,....) it mix all earlier item.Is this possible. If yes help me.

My MainActivity is

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

}

Just use

List.add(0, element)

to add an element as first element of a list. As described in the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM