1

Suppose I've already made a list using ListView. The list consists of colour names. Now I take one EditText text field and one Button widget. I enter the text(colour name) and press Button. The colour name entered should be added to the list. Can anyone help me with the code?

2 Answers2

3

MainActivity class

package com.example.asd;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity
{

private ListView list;
private EditText et;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adapter= new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
    adapter.add("white");
    adapter.add("black");
    adapter.add("read");
    list=(ListView)findViewById(R.id.listView1);
    list.setAdapter(adapter);

    et=(EditText)findViewById(R.id.editText1);

    Button b=(Button)findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
                adapter.add(et.getText().toString());           
        }
    });


}



}

layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="25dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:text="Button" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:cacheColorHint="#00000000"


    android:layout_below="@+id/button1" >
</ListView>

</RelativeLayout>
Fahad
  • 193
  • 1
  • 9
  • ok I've taken a String array: String[] colors={"RED","GREEN",BLUE"}; then I've used OnItemClickListener to change the color of the text in text view as soon as the corresponding colour is clicked. now I've to make changes as asked asked. The changes need not perform the color changing function.Its just that I have to add items in the list. can you help me out with that. – user3608017 May 07 '14 at 10:29
  • to be clear,do you want to apply the color change to the textbox when we select one of the color in list ? – Fahad May 07 '14 at 11:35
  • I already have made that part where I apply the color on clicking the color in the list.now when i add another color it shouldn't do so. – user3608017 May 08 '14 at 04:04
0

I think you are looking for this...

public class Colors extends Activity {

private EditText et1;
private Button but1;
private ListView list;
private ArrayList<String> colornames=new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1  = (EditText)findViewById(R.id.et1);
    but1 = (Button)findViewById(R.id.but1);
    list = (ListView)findViewById(R.id.list1);

    but1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            colornames.add(et1.getText().toString());
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(Colors.this, android.R.layout.simple_list_item_1,colornames);
            list.setAdapter(adapter); 
        }
    });
}

And the output looks like this

enter image description here

Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18
  • still doing. there r some changes to be made. – user3608017 May 07 '14 at 10:23
  • ok I've taken a String array: String[] colors={"RED","GREEN",BLUE"}; then I've used OnItemClickListener to change the color of the text in text view as soon as the corresponding colour is clicked. now I've to make changes as asked asked. The changes need not perform the color changing function.Its just that I have to add items in the list. can you help me out with that. – user3608017 May 07 '14 at 10:34
  • Sorry...Can you elaborate the above comment – Sainath Patwary karnate May 07 '14 at 11:04
  • I've made a program where I've taken String array as mentioned in the above comment.I've also taken TextView("COLORS").OK! Now whenever I press, suppose RED, text colour of COLORS changes to red. What I want is to add another Item to the list, say ORANGE. thats it. I don't want the colour of COLORS should change to orange when I press ORANGE. Is this good enough? – user3608017 May 07 '14 at 11:18
  • @SainathPatwarykarnate your solution will work indeed, but I feel it's not the "right way" of doing it, rather it's a dumb way..!!! a new `ArrayAdapter` for each click, seriously..?? Look at the second answer, also there is an API call `notifyDataSetChanged()` in `ArrayAdapter`.. u might want to have a look at it... :) – Rupesh May 08 '14 at 05:00