Duplicate Items In ArrayList Android Studio

Ash Zudus :

I am receiving the variable from a different activity

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // check that it is the SecondActivity with an OK result
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) { // Activity.RESULT_OK

                // get String data from Intent
                String returnString = data.getStringExtra("keyName");
                // Add data to ArrayList
                Subreddit_Array_List.add(1,returnString);
            }
        }
    }

Expected Result:

Item Entered by user

Result: Duplicates the last added item on the ArrayList


public class MainActivity extends AppCompatActivity {

    public final String FILENAME = "example.txt";
    ArrayList<String> Subreddit_Array_List = new ArrayList<String>();
    Context context = this;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView lv = (ListView) findViewById(R.id.LV_Main);


        Subreddit_Array_List.add(0,"Saved Posts");
        //Ensures Add new is always at the end
        Subreddit_Array_List.add(Subreddit_Array_List.size(), "Add New +");

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.activity_list_item,android.R.id.text1,
                Subreddit_Array_List );
                lv.setAdapter(arrayAdapter);
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {






    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // check that it is the SecondActivity with an OK result
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) { // Activity.RESULT_OK

                // get String data from Intent
                String returnString = data.getStringExtra("keyName");
                // Add data to ArrayList
                Subreddit_Array_List.add(1,returnString);
            }
        }
    }



    }

Thanks in advance

Edit: Subreddit_Array_List.add(Subreddit_Array_List.size() - 1, "Add New +"); Does not work Also, I need Add New to always be at the bottom and Saved Posts at the top

It is weird as if I remove the Subreddit_Array_List.add(1,returnString); Then it works but adds the returnstring to the bottom of the ArrayList, however I need the Add New always at the bottom

PPartisan :

The behaviour you're looking for already exists in ListView - it's called a "Header" and a "Footer". You can also just define one in xml to make your life easier:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ListView lv = (ListView) findViewById(R.id.LV_Main);

    lv.addHeaderView(createTextView("Saved Posts"));
    lv.addFooterView(createTextView("Add New +"));

    //Etc. etc.
}
private TextView createTextView(String content) {
    TextView tv = new TextView(this);
    tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    tv.setText(content);
    return tv;
}

Now you don't need to worry about inserting the first and last elements yourself - just add the content to your list and update your adapter with it.

Edit: If you want the header and footer to have the same styling as the rest of your list elements:

private View createHeaderOrFooter(String content) {
    final LayoutInflater inflater = LayoutInflater.from(this);
    final View row = inflater.inflate(android.R.layout.activity_list_item, null);
    ((TextView)row.findViewById(android.R.id.text1)).setText(content);
    return row;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396567&siteId=1