Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to make a dynamic Custom ListView, where a user can enter a name and age, an unknown amount of times. credit to @Razgriz he helped me get the Custom ListView working. I am now trying to make it dynamic. My issue is when I instantiate the NameAndAgeClass object thru the constructor, my arraylist will show what i entered thru the onclick, but it is also showing the original instantiation a bunch of times as well, in the NameAndAgeClass class i tried to create 2 arraylists for the name and age, but i was getting a out of memory error. In the for loop in M class to add a entry to the ArrayList nameAndAgeList how would i get the size of NameAndAgeClass object right now i am using while i < 10.

public class MainActivity extends Activity {

M gg = new M();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aa);      
}


public void ss(View v){       
    Intent intent2 = new Intent(MainActivity.this,M.class);
    startActivity(intent2);       
 }
    public void sa(View v){       
    gg.addit("phil");       
   } 

}

 public class M extends Activity {

static ArrayList<NameAndAgeClass> nameAndAgeList = new     
ArrayList<NameAndAgeClass>();
static NameAndAgeClass nandc = new NameAndAgeClass("bill", 88);
 static int ihg = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView nameAndAgeListView = (ListView) findViewById(R.id.listView);       


   //create your listView with your custom object       
   /*       
    get no error with this just says not loading do i want to cancel

    for(int i = 1 ; i < nameAndAgeList.size() ; i ++){
        NameAndAgeClass entry = new NameAndAgeClass("lou",23);
        nameAndAgeList.add(entry);
    }
    */

    for(int i = 1 ; i < 10 ; i ++){
        NameAndAgeClass entry = new NameAndAgeClass("lou",23);
        nameAndAgeList.add(entry);
    }

   //create your adapter, use the nameAndAgeList ArrayList
    CustomListViewAdapterNameAndAge nameAndAgeAdapter = new  
   CustomListViewAdapterNameAndAge(this, nameAndAgeList);

   //get your listView and use your adapter
    nameAndAgeListView.setAdapter(nameAndAgeAdapter);

    nameAndAgeListView.setOnItemClickListener(new  
    AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int  
        i, long l) {
            /*
                Do what ever you want inside this onItemClick function
             */
        }
    });

}


public void addit(String nn){   
     ihg++;       

     nameAndAgeList.add(( new NameAndAgeClass("phill",ihg)));  
    }   
}

public class NameAndAgeClass {

static public ArrayList<String> namee = new ArrayList<String>();
static public ArrayList<Integer> agee = new ArrayList<Integer>();

 String name;
int age;

public NameAndAgeClass(String name, int age) {
    this.name = name;
    this.age = age;

    namee.add(name);
    agee.add(age);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's how I would do it.

In the main activity layout file, I'd put 2 EditText fields with ids nameEditText and ageEditText in the layout, below the listView, as well as a button to save. In your button, do not forget to add the line:

android:click="onSave"

and in your Main Activity, create a function as such:

public void onSave(View view){
    //this is the function that will activate when you click your button
}

It also goes without saying that you should hook up your EditTexts as such:

public class MainActivity extends Activity {

    //DO NOT DECLARE THIS AS STATIC, OTHERWISE YOU WON'T BE ABLE TO ADD TO IT
    ArrayList<NameAndAgeClass> nameAndAgeList = new ArrayList<NameAndAgeClass>();

    EditText nameInput;
    EditText ageInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aa);      

        //hook up your EditText as such:
        nameInput = (EditText) findViewById(R.id.nameEditText);
        ageInput = (EditText) findViewById(R.id.ageEditText);

        //more of your code here...

    }

    //more of your code here, and the onSave function

 }

What we want to do is when you click this "Save" button, we will take the input from the editText which we initialized in the onCreate function and add it to our ArrayList. Here's how we'll do it.

public void onSave(View view){

    //we get the string values from the EditText input
    Sting nameInputFromField = nameInput.getText().toString();
    Sting ageInputFromField = ageInput.getText().toString();

    //we create a class using our values
    NameAndAgeClass entry = new NameAndAgeClass(nameInputFromField, ageInputFromField);

    //then we add it to our ArrayList
    nameAndAgeList.add(entry);

    //after that, we get the customListViewAdapter (I trust that you have this one)
    //and call a neat function
    //the function is called something like that
    nameAndAgeAdapter.notifyDataSetChanged();
}

What notifyDataSetChanged does is that it "refreshes" your listView. Usually this is done after modifications are made in the ListViews Data so that the changes would be seen by the user right away.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...