动态自定义的ListView? [英] Dynamic Custom ListView?

查看:156
本文介绍了动态自定义的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个动态自定义的ListView,用户可以在其中输入姓名和年龄,时代金额不详。信贷@Razgriz他帮我把自定义的ListView工作。现在我试图使它的动态。我的问题是,当我实例化NameAndAgeClass对象THRU的构造,我的ArrayList中会显示什么我进入直通的onclick,但也呈现出原来的实例一堆时间以及在NameAndAgeClass类我试图创建2的ArrayList的的名字和年龄,但我得到一个内存不足的错误。在对M级循环添加到ArrayList nameAndAgeList一个条目我怎么才能NameAndAgeClass对象的大小,现在我使用的同时I< 10。

 公共类MainActivity延伸活动{中号GG =新M();@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.aa);
}
公共无效SS(视图v){
    意图int​​ent2 =新意图(MainActivity.this,M.class);
    startActivity(intent2);
 }
    公共无效SA(视图v){
    gg.addit(菲尔);
   }} 公共M级延伸活动{静态的ArrayList< NameAndAgeClass> nameAndAgeList =新
ArrayList的< NameAndAgeClass>();
静态NameAndAgeClass nandc =新NameAndAgeClass(条例,88);
 静态INT IHG = 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    ListView控件nameAndAgeListView =(ListView控件)findViewById(R.id.listView);
   //你的自定义对象创建的ListView
   / *
    得到任何错误与此正好装载做我想取消不说    的for(int i = 1; I< nameAndAgeList.size();我++){
        NameAndAgeClass进入=新NameAndAgeClass(楼,23);
        nameAndAgeList.add(输入);
    }
    * /    的for(int i = 1;我小于10;我++){
        NameAndAgeClass进入=新NameAndAgeClass(楼,23);
        nameAndAgeList.add(输入);
    }   //创建您的适配器,使用ArrayList的nameAndAgeList
    CustomListViewAdapterNameAndAge nameAndAgeAdapter =新
   CustomListViewAdapterNameAndAge(这一点,nameAndAgeList);   //获取你的ListView和使用您的适配器
    nameAndAgeListView.setAdapter(nameAndAgeAdapter);    nameAndAgeListView.setOnItemClickListener(新
    AdapterView.OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>适配器视图,视图来看,INT
        我,长L){
            / *
                你什么都想要这个onItemClick函数内
             * /
        }
    });}
公共无效ADDIT(字符串NN){
     IHG ++;     nameAndAgeList.add((新NameAndAgeClass(菲尔,IHG)));
    }
}公共类NameAndAgeClass {静态公共的ArrayList<串GT; namee =新的ArrayList<串GT;();
静态公共的ArrayList<整数GT;阿吉=新的ArrayList<整数GT;(); 字符串名称;
INT年龄;公共NameAndAgeClass(字符串名称,INT年龄){
    this.name =名称;
    this.age =年龄;    namee.add(名);
    agee.add(年龄);
}公共字符串的getName(){
    返回名称;
}公共无效setname可以(字符串名称){
    this.name =名称;
}公众诠释getAge(){
    返回年龄;
}公共无效setAge(INT年龄){
    this.age =年龄;
}
}


解决方案

下面是我会怎么做。

在主要活动布局文件,我把在与IDS 2场的EditText nameEditText ageEditText 布局,的ListView低于,以及一键拯救。在您的按钮,不要忘了添加一行:

 的android:点击=的onSave

和你的主要活动中,创建​​一个功能,例如:

 公共无效的onSave(查看视图){
    //这是当您点击按钮,将激活该功能
}

这也不用说,你应该挂钩的EditTexts这样:

 公共类MainActivity延伸活动{    //不要声明此为静态,否则你将无法添加到它
    ArrayList的< NameAndAgeClass> nameAndAgeList =新的ArrayList< NameAndAgeClass>();    的EditText nameInput;
    的EditText ageInput;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.aa);        //挂钩您的EditText这样:
        nameInput =(EditText上)findViewById(R.id.nameEditText);
        ageInput =(EditText上)findViewById(R.id.ageEditText);        //您更多的code在这里...    }    //您更多的code在这里,和的onSave功能 }

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

 公共无效的onSave(查看视图){    //我们从输入的EditText获取字符串值
    。蜇nameInputFromField = nameInput.getText()的toString();
    。蜇ageInputFromField = ageInput.getText()的toString();    //我们使用我们的价值观创建一个类
    NameAndAgeClass进入=新NameAndAgeClass(nameInputFromField,ageInputFromField);    //那么我们把它添加到我们的ArrayList
    nameAndAgeList.add(输入);    //在这之后,我们得到customListViewAdapter(我相信你有这样的一个)
    //并调用一个函数整齐
    //调用函数时类似的东西
    nameAndAgeAdapter.notifyDataSetChanged();
}

什么 notifyDataSetChanged 做的是,它刷新您的ListView。通常,这是在列表视图的数据进行修改后进行,这样的变化可能使用户马上可以看到。

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

解决方案

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.

这篇关于动态自定义的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆