动态自定义ListView?

时间:2015-05-22 21:51:32

标签: android listview arraylist android-arrayadapter

我试图创建一个动态的自定义列表视图,用户可以输入一个名称和年龄,未知的次数。归功于@Razgriz他帮我定制ListView工作。我现在正试图让它充满活力。我的问题是当我通过构造函数实例化NameAndAgeClass对象时,我的arraylist将显示我通过onclick输入的内容,但它也显示了原始实例化的一堆次数,在NameAndAgeClass类中我尝试创建2个arraylists for名字和年龄,但我得到一个内存不足的错误。在M类的for循环中,为ArrayList nameAndAgeList添加一个条目,如何才能获得NameAndAgeClass对象的大小,我正在使用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;
}
}

2 个答案:

答案 0 :(得分:0)

希望这会对某人有所帮助,即使我确定它可能不是正确的做法,或者有更好的方法。但是要修补一下,也许这会让别人头疼,我必须得到一个2列动态列表视图。感谢@Razgriz帮助我!

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

答案 1 :(得分:0)

这是我将如何做到的。

在主活动布局文件中,我将2个带有ids nameEditTextageEditText的EditText字段放在listView下方的布局中,以及一个要保存的按钮。在按钮中,不要忘记添加以下行:

android:click="onSave"

并在您的主活动中,创建一个函数:

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

不言而喻,您应该将EditTexts连接起来:

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

 }

我们想要做的是当你点击这个“保存”按钮时,我们将从我们在onCreate函数中初始化的editText中获取输入,并将其添加到我们的ArrayList中。我们将如何做到这一点。

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

notifyDataSetChanged做的是“刷新”你的listView。通常,这是在ListViews数据中进行修改后完成的,这样用户就可以立即看到更改。