动态自定义列表视图? [英] Dynamic Custom ListView?

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

问题描述

我正在尝试制作一个动态的自定义 ListView,用户可以在其中输入姓名和年龄,次数未知.感谢@Razgriz,他帮助我让自定义 ListView 正常工作.我现在正试图让它变得动态.我的问题是,当我通过构造函数实例化 NameAndAgeClass 对象时,我的数组列表将显示我通过 onclick 输入的内容,但它也多次显示原始实例化,在 NameAndAgeClass 类中,我尝试为它创建 2 个数组列表姓名和年龄,但我遇到了内存不足错误.在 M 类的 for 循环中向 ArrayList nameAndAgeList 添加一个条目,我现在如何获得 NameAndAgeClass 对象的大小,而我正在使用,而我 <10.

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

推荐答案

这就是我的做法.

在主活动布局文件中,我将 2 个 ID 为 nameEditTextageEditText 的 EditText 字段放在布局中,位于 listView 下方,以及一个保存按钮.在您的按钮中,不要忘记添加以下行:

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
}

不用说,您应该像这样连接您的 EditText:

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

 }

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

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

notifyDataSetChanged 的作用是刷新"您的列表视图.通常这是在对 ListViews 数据进行修改之后完成的,以便用户可以立即看到更改.

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.

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

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