Dynamic Custom ListView?

纵然是瞬间 提交于 2019-12-13 23:34:02

问题


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;
}
}

回答1:


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.




回答2:


Hopefully this will help someone even though i'm sure it may not be the corrrect way to do it, or there is a better way. But it's something to tinker with, and maybe it will save someone the headache I had to get a 2 column dynamic listview working. Thanks and credit to @Razgriz for helping me!

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 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       

   //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++;

         NameAndAgeClass entry = new NameAndAgeClass(nn,ihg);
         nameAndAgeList.add(entry);
    }   
}

public class NameAndAgeClass {


 String name;
int age;

public NameAndAgeClass(String name, int age) {
    this.name = name;
    this.age = 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;
}
}


来源:https://stackoverflow.com/questions/30406381/dynamic-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!