Charuka Silva
Charuka Silva

Reputation: 13153

behavior of the list view android

friends help,

i have a List view , i made a string array list and in onCreate i add that Strings to listview and it works

i wanted to add an item to the list view one by one when a button is clicked so on the onClickListner of the button i tried to add item. It did not work as i needed. But when the display goes off and then when it On i can see that added items are there so is it works on onResume?

then i tried to use runOnUI thread inside the onclick once and next time the place i added the string array item to the list (both button click and adding item are in the oncreate ) same result - it needs to screen go off to add items to the list

then i thought after screen goes down and up it calls oncreate that's why it happens to check it i add another class and when my button click i add the item first,and call that class and again from that class(second one) i called the main class

but this time even when the power goes off in my screen my list view did not grow up

so my question is how to add items to the list view by clicking a button in the same layout and display that added item in the list view same time ?

any good explanations

activity_main.xml

<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"
tools:context=".MainActivity">


<include

    android:id="@+id/toolbar"
    layout="@layout/tool_bar"

    ></include>

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true">


  <ListView
      android:id="@+id/listmain"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

  </ListView>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="right|bottom"
        android:layout_margin="15dp"
        android:background="@drawable/cc"
        android:src="@drawable/ic_action" />


</FrameLayout>

MainActivity

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {
    private List<String> listArtificial;
    Toolbar toolbar;
    ImageButton FAB;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
//        two.setVisibility(View.GONE);
        listView = (ListView) findViewById(R.id.listmain);
        System.out.println("sssssssssss");
        listArtificial = new ArrayList();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listArtificial.add("Artificial");
                listArtificial.add("Artificial");
            }
        });


        FAB = (ImageButton) findViewById(R.id.imageButton);
        FAB.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                listArtificial.add("Artificial");
//                Intent i = new Intent(MainActivity.this, RapidFireAtrificial.class);
//                startActivity(i);
//                finish();
                Toast.makeText(MainActivity.this, "TOUCHED", Toast.LENGTH_SHORT).show();




            }
        });
//        listArtificial.add("Artificial @ fiverr");
        CustomAdapter customAdapter = new CustomAdapter(listArtificial);

        listView.setAdapter(customAdapter);


        // ListView Item Click Listener
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view,
//                                    int position, long id) {
//
//                // ListView Clicked item index
//                int itemPosition = position;
//
//                // ListView Clicked item value
//                String itemValue = (String) listView.getItemAtPosition(position);
//
//                // Show Alert
//                Toast.makeText(getApplicationContext(),
//                        "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
//                        .show();
//
//            }
//
//        });



    }

//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(getApplicationContext(), " CLICKED...", Toast.LENGTH_SHORT).show();
//                two.setVisibility(View.VISIBLE);
//            }
//        });


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public class CustomAdapter extends BaseAdapter {
        private List<String> data;


        public CustomAdapter(List<String> data) {
            System.out.println("*** 1 CustomAdapter constructor");
            this.data = data;

        }

        // How many items are in the data set represented by this Adapter.
        @Override
        public int getCount() {

            System.out.println(data.size());
            return data.size();
        }

        // Get the data item associated with the specified position in the data set.
        @Override
        public Object getItem(int position) {
            System.out.println("*** ? getItem method");
            System.out.println(data.get(position));
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }


        // Get the row id associated with the specified position in the list.


        // Get a View that displays the data at the specified position in the data set.
        // You can either create a View manually or inflate it from an XML layout file.
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;


            if (convertView != null) {

                viewHolder = (ViewHolder) convertView.getTag();

                convertView.setTag(viewHolder);

            } else {

                viewHolder = new ViewHolder();
                // LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
                LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.line, null);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.texttt);


                convertView.setTag(viewHolder);
            }



            String dataobj = data.get(position);
            viewHolder.textView.setText(dataobj);




            return convertView;
        }
    }
    static class ViewHolder {

        TextView  textView;


    }
}

Upvotes: 1

Views: 489

Answers (2)

Dinesh Kannan
Dinesh Kannan

Reputation: 1255

have to call the notifydatasetChanged , when every time you modifying the list. and initialize the adapter before adding items in the list.

CustomAdapter customAdapter = new CustomAdapter(listArtificial);
 FAB.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
              listArtificial.add("Artificial");
              customAdapter.notifyDataSetChanged(); 
              Toast.makeText(MainActivity.this, "TOUCHED", Toast.LENGTH_SHORT).show();
            }
        });

Upvotes: 1

Roshan
Roshan

Reputation: 3592

Call

customAdapter.notifyDataSetChanged ()

after adding new items.

Upvotes: 0

Related Questions