2

My main layout main.xml has only a Button, a EditText and an empty ListView as below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
>
     <LinearLayout 
         android:id="@+id/input_area"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"

     >
         <EditText 
           android:id="@+id/input_field"
           android:layout_height="40dip"
           android:layout_width="fill_parent"
           android:layout_weight="5"
         />

         <Button
            android:id="@+id/send_btn"
            android:layout_width="60dip" 
            android:layout_height="30dip"
            android:text="@string/send"
            android:layout_weight="1"
          />

      </LinearLayout>
      <LinearLayout 
        android:id="@+id/output_area"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#006633"

        android:visibility="gone"

       >
        <ListView 
            android:id="@+id/output_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10dip"
        >

        <!-- When "send" button in above input_area is pressed, 
            text from EditText field show here programmatically as a new row of the listview-->

        </ListView>

       </LinearLayout>

</LinearLayout> 

As you see above, there are two child LinearLayout hosted by main LinearLayout.

The 1st child LinearLayout with id input_area consists of a EditText and a Button.

The 2nd child LinearLayout with id output_area is an LinearLayout with an empty ListView, AND its visibility is set to "gone".

The feature I am going to implement is very simple, that's in the input_area, when user input some text in the EditText field, and press the send button, then the input string should be shown in the output LinearLayout as a new row of the listview programmatically.

What I tried is the java code below:

EditText inputTxt = (EditText) findViewById(R.id.input_field);
Button sendBtn = (Button) findViewById(R.id.send_btn);

LinearLayout outputArea = findViewById(R.id.output_area);
//Updated by Saurabh
ListView lv = findViewById(R.id.output_list);
MyListAdapter mAdapter = new MyListAdapter(this, arraylist); 
//Update finished
sendBtn.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        int visibility = outputArea.getVisibility();
        if(visibility==View.GONE)
                    // set ListView visible
            outputArea.setVisibility(View.VISIBLE);

               //get user input   
               String userInput = inputTxt.getText().toString(); // show this string in a new row of listview

                 //BUT how to dynamically add new row to the listview here???           

    }
});

But I am not sure how to add new row to the listview programmatically, anyone can help me?

By the way, I have another layout xml fiel (row.xml) which defined each row's layout.

-----------------------UPDATE------------------------------------

Each row of the list contain a icon and a text string. My list adapter and row layout are showing below:

My adapter:

private static class MyListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    ArrayList<String> arraylist;

    public MyListAdapter(Context context, ArrayList<String> arraylist) {
        mInflater = LayoutInflater.from(context);
            this.arraylist = arraylist;
    }



    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.userInput = (TextView) convertView.findViewById(R.id.user_input);

        convertView.setTag(holder);
        } else {
        holder = (ViewHolder) convertView.getTag();
        }

        holder.icon.setImage(???);
        holder.userInput.setText(arraylist.get(position));

        return convertView;
    }

    static class ViewHolder {
        ImageView icon;
        TextView userInput;

    }
}

my list row layout:

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

        <ImageView 
             android: id="@+id/icon"
             android:layout_width="wrap_content"
         android:layout_height="wrap_content"
             >

    <TextView
        android:id="@+id/user_input" 
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textSize="10dip"/>

</LinearLayout>
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
Mellon
  • 37,586
  • 78
  • 186
  • 264

1 Answers1

4

Make a Global variable as below

ArrayList<String> arraylist = new ArrayList<String>();

On Click of send button update the adapter that you are setting on ListView by adding

String userInput = inputTxt.getText().toString();
arraylist.add(userInput)

in adapter and then call

adapter.notifyDataSetChanged();

Update I updated answer in your question and in this post copy your new Adapter class and use that

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • @ Saurabh , can you suggest a proper adapter for this case? I am not sure how to add userInput to adapter... – Mellon Jul 14 '11 at 06:50
  • If there is only text then take an arraylist of string and set that as adapter. I can help you better if you provide src of your listview and adapter – ingsaurabh Jul 14 '11 at 06:51
  • @ Saurabh , I updated my post with my adapter and my list row layout, each row contain an image icon and a string. I am not sure the adapter I am using is the right one, could you please have a look? thank you. (see my update above) – Mellon Jul 14 '11 at 07:03