如何筛选的Andr​​oid对象名单 [英] How to filter a List of objects in Android

查看:178
本文介绍了如何筛选的Andr​​oid对象名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Android应用程序(新的话)!

我的问题是:结果
假如我是​​一所大学的X.结果的管理员
我需要把具有不同点的每个课程列表。

例如:


  • 医药:30

  • 物理:28

  • 数学:24

  • 英语:20

现在假设一个学生进入点28。

我的应用应能检测具有28分,而且该下点的过程。那是;物理,数学和英语,但不是药!

谁能帮我这个?我已经找遍,但没有结果呢!

请家伙我有一个很难做这个!谢谢!

MainActivity.java

 进口android.os.Bundle;
进口android.app.Activity;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.EditText;
进口android.widget.ListView;
 公共类MainActivity延伸活动{
  的String [] =课程,新的String [] {        数学,
        化学工程,
        土木工程,
        农业,
        法律与管理,
        社会学,
        信息和通信技术
        计算机科学,
        信息系统,
        软件工程,
};  @覆盖
  公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.event2icon);
    EDITTEXT的EditText = NULL;
    EDITTEXT =(EditText上)findViewById(R.id.edit_text);
    最后串点= editText.getText()的toString()。
    最终诠释point_integer =的Integer.parseInt(点);
    最终的ListView courselist =(ListView控件)findViewById(R.id.courseNames);
    LayoutInflater吹气= getLayoutInflater();
    ViewGroup中头=(ViewGroup中)inflater.inflate(R.layout.listviewheader,courselist,FALSE);
    courselist.addHeaderView(头,空,假);
    最后ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,课程);
    courselist.setAdapter(适配器);
    courselist.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
            // TODO自动生成方法存根            //我们用列表视图的项目作为下一个活动名称
            如果((的Integer.parseInt(将String.valueOf(point_integer))≤= 10))
            {
            }
        }      });  }
 }


event2icon.xml

 <?XML版本=1.0编码=UTF-8&GT?;
 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直的android:layout_width =match_parent
机器人:layout_height =match_parent>    <的EditText
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / edit_text
    机器人:文字=请输入您的观点:/>
     < ListView控件
    机器人:ID =@ + ID / courseNames
    机器人:layout_width =match_parent
    机器人:layout_below =@ + ID / edit_text
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:dividerHeight =15.0sp
        >
   < /&的ListView GT;
  < / LinearLayout中>


listviewheader.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
>
< / LinearLayout中>


解决方案

您应该能够执行他的回答韦斯顿提到的所有步骤,否则,我怕我只是给你的code不会教你十分。

相反,我会为您提供在对方的回答中提到的部分。


  

您想要一类重presents的过程中,有名称和分领域


这是一个简单的模型类。到Java课程介绍教这些。

 公共类课程{    私人字符串名称;
    私人诠释点;    公共课程(字符串名称,诠释点){
        this.name =名称;
        this.points =分;
    }    公共字符串的getName(){
        返回名称;
    }    公众诠释getPoints(){
        返回点;
    }    @覆盖
    公共字符串的toString(){
        返回的getName();
    }
}


  

这包含了这些课程的情况下,一个List


您刚刚已经于您的code中的字符串,而不是点,那么你是怎么想到用数字来筛选课程?这里是你列为阵列样品课程的例子。

 课程[] =课新课程[] {
        新课程(药,30)
        新课程(物理学,28)
        新课程(数学,24)
        新课程(英语,20)
};


  

您需要,其中包括一个文本框,学生输入数字到活动


这需要你写一些XML,这已在 event2icon.xml 已经完成。我建议你​​添加一个按钮,虽然因为打内输入的EditText 这将插入一个换行符。要创建此同时,你也可以只接受数字。下面是完整的XML,我建议。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:layout_width =match_parent
              机器人:layout_height =match_parent
              机器人:方向=垂直>    <的LinearLayout的android:layout_width =match_parent
                  机器人:layout_height =WRAP_CONTENT
                  机器人:方向=横向>
        <的EditText
                机器人:layout_width =0dp
                机器人:layout_weight =1
                机器人:MAXLINES =1
                安卓的inputType =数字
                机器人:layout_height =WRAP_CONTENT
                机器人:提示=输入你的观点
                机器人:ID =@ + ID / EDITTEXT/>
        <按钮
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字=搜索
                机器人:ID =@ + ID / btn_search/>
    < / LinearLayout中>    < ListView控件
            机器人:ID =@ + ID / courseNames
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>


  

您需要将文本解析为整数


您知道如何做到这一点 - 的问题,虽然是的的做呢?你立即发布到code获取文本出来的的EditText 一旦活动被加载,这将是空的。而应该从中提取文本的EditText 当您单击该按钮。这将让你开始。

 按钮搜索按钮=(按钮)findViewById(R.id.btn_search);
sea​​rchButton.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(查看视图){


  

您需要遍历你定义的列表。并通过使用小于或等于运营商的LT =,你会发现你所需要的课程


这仅仅是for-each循环在课程阵列已经定义了一个简单的。一旦你找到了课程目标,将它添加到适配器。你也应该知道,你需要转换的课程[] 列表<课程> 为了要做到这一点,否则你会得到一个错误。

 列表<课程和GT; courseList =新的ArrayList<课程和GT;(Arrays.asList(课程));
//初始化courseList适配器,而不是课程
最后ArrayAdapter<课程和GT;适配器=新ArrayAdapter<课程和GT;(这一点,android.R.layout.simple_list_item_1,courseList);//按钮的onClick这里面code
INT点=的Integer.parseInt(editText.getText()的toString());
对于(课程C:课程){
    如果(c.getPoints()< =点){
        adapter.add(C);
    }
}


  

使用一个ListAdapter很好地显示列表


您已经有一个 ArrayAdapter ,所以你是好人在这里。最后一个细节,不过,是当您单击按钮,你应该先的 明确 适配器添加课程回到它之前。否则,你会得到重复。

I am trying to create an android application (New to it)!

My question is as follows:
Suppose I am an administrator of a university X.
I need to put a list of courses each with different points.

E.g:

  • Medicine: 30
  • Physics: 28
  • Maths: 24
  • English: 20

Now suppose a student enter the point 28.

My app should be able to detect the course that has 28 points but also that of lower points. That is; Physics, Maths and English BUT NOT Medicine!!!

Can anyone help me on this? I have searched everywhere but no results yet!

Please guys am having a hard time doing this! Thank you!

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater; 
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;


 public class MainActivity extends  Activity {


  String[] courses = new String[]{

        "Mathematics",
        "Chemical Engineering",
        "Civil Engineering",
        "Agriculture",
        "Law and Management",
        "Sociology",
        "Information and Communication Technologies",
        "Computer Science",
        "Information Systems",
        "Software Engineering",


};

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event2icon);
    EditText editText = null;
    editText=(EditText)findViewById(R.id.edit_text);
    final String point=editText.getText().toString();
    final int point_integer=Integer.parseInt(point);


    final ListView courselist = (ListView)findViewById(R.id.courseNames);


    LayoutInflater inflater = getLayoutInflater();
    ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader,  courselist, false);
    courselist.addHeaderView(header, null, false);


    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, courses);
    courselist.setAdapter(adapter);
    courselist.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int   position, long id) {
            // TODO Auto-generated method stub

            //we use the items of the listview as title of the next activity
            if((Integer.parseInt(String.valueOf(point_integer))<=10))
            {


            }
        }

      });

  }
 }


event2icon.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edit_text"
    android:text="Enter your points:"/>
     <ListView
    android:id="@+id/courseNames"
    android:layout_width="match_parent"
    android:layout_below="@+id/edit_text"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:dividerHeight="15.0sp"
        >
   </ListView>


  </LinearLayout>


listviewheader.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>


</LinearLayout>

解决方案

You should be able to perform all the steps weston mentioned in his answer, otherwise, I'm afraid me just giving you the code won't teach you very much.

Instead, I will provide you the pieces mentioned in the other answer.

You want a class that represents the course, with name and points fields

This is a simple model class. An introduction to Java course teaches these.

public class Course {

    private String name;
    private int points;

    public Course(String name, int points) {
        this.name = name;
        this.points = points;
    }

    public String getName() {
        return name;
    }

    public int getPoints() {
        return points;
    }

    @Override
    public String toString() {
        return getName();
    }
}

A list that contains the instances of these courses

You just have Strings, not points listed in your code, so how did you expect to use numbers to filter the courses? Here is an example of the sample courses you listed as an array.

Course[] courses = new Course[]{
        new Course("Medicine", 30),
        new Course("Physics", 28),
        new Course("Math", 24),
        new Course("English", 20)
};

You need an activity that includes a text box for student to enter number into

This requires you to write some XML, which you have already done in event2icon.xml. I suggest you add a Button, though because hitting Enter within the EditText this will insert a newline. You could also accept only numbers while you are creating this. Here is the full XML I suggest.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
        <EditText
                android:layout_width="0dp"
                android:layout_weight="1"
                android:maxLines="1"
                android:inputType="number"
                android:layout_height="wrap_content"
                android:hint="Enter your points"
                android:id="@+id/editText"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Search"
                android:id="@+id/btn_search"/>
    </LinearLayout>

    <ListView
            android:id="@+id/courseNames"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

You need to parse the text to an integer

You know how to do this - the question, though is when do you do this? The code you posted immediately gets the text out of the EditText as soon as the Activity is loaded, which will be empty. You should instead extract the text from the EditText when you click the button. This will get you started.

Button searchButton = (Button) findViewById(R.id.btn_search);
searchButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

You need to iterate over list you defined. And by using the less than or equal operator <=, you'll find the courses you need

This is just a simple for-each loop over the Courses array you have defined. Once you have found the Course object, add it to the adapter. You should also be aware that you'll need to convert the Course[] to a List<Course> in order to do this, otherwise you'll get an error.

List<Course> courseList = new ArrayList<Course>(Arrays.asList(courses));
// initialize the adapter with courseList instead of courses 
final ArrayAdapter<Course> adapter = new ArrayAdapter<Course>(this, android.R.layout.simple_list_item_1, courseList);

// this code inside the Button's onClick
int points = Integer.parseInt(editText.getText().toString());
for (Course c : courses) {
    if (c.getPoints() <= points) {
        adapter.add(c);
    }
}

Use a ListAdapter to display the list nicely

You already have an ArrayAdapter, so you are good here. One last detail, though, is that when you click the Button, you should first clear the adapter before adding courses back into it. Otherwise, you'll get duplicates.

这篇关于如何筛选的Andr​​oid对象名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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