2016-01-07 20 views
0

我想開發一個android手機應用程序,我需要顯示一部分至少有100個項目的列表視圖。 現在我作爲一所大學的管理人員,列出了100門課程(所有課程都列在listview中)。所有課程都有一個要點,並且爲了讓申請人申請此課程,他/她需要有準確的高points.An的指示樣品低於: 英語:24 西班牙:16個 數學:28 科學:26 法國:16 管理:22 法:30 亞洲語言:10如何根據用戶的不同要求只選擇部分列表視圖?

現在,學生需要輸入他/她的觀點,並根據(確切或較低點),他/她得到他/她有資格申請的科目清單

例如,輸入您的積分:24

輸出在ListView應給出如下: 法國:16 管理:22 英語:24 亞洲語言:10

我試過,但我堅持和無法完成代碼:

Course.java 

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

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


Button searchButton = (Button) findViewById(R.id.btn_search); 
searchButton.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 
List<Course> courseList = new ArrayList<Course>(Arrays.asList(courses)); 
// initialize the adapter with 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); 
} 
} 




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

回答

0

我的解決方案如下 -

  1. MainActivity

    public class MainActivity extends AppCompatActivity { 
    
    ListView myList; 
    EditText edtSearch; 
    Button btnSearch; 
    
    listAdapter adapter; 
    ArrayList<Course> alSearchCourses; 
    Course[] courses; 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    
    myList = (ListView) findViewById(R.id.myList); 
    edtSearch = (EditText) findViewById(R.id.edtSearch); 
    btnSearch = (Button) findViewById(R.id.btnSearch); 
    
    courses = new Course[]{ 
         new Course("Medicine", 30), 
         new Course("Physics", 28), 
         new Course("Math", 24), 
         new Course("English", 20) 
    }; 
    
    adapter = new listAdapter(this, courses); 
    myList.setAdapter(adapter); 
    
    edtSearch.addTextChangedListener(new TextWatcher() { 
        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
        } 
    
        @Override 
        public void onTextChanged(CharSequence s, int start, int before, int count) { 
         if (count == 0) { 
          adapter = new listAdapter(MainActivity.this, courses); 
          myList.setAdapter(adapter); 
         } 
        } 
    
        @Override 
        public void afterTextChanged(Editable s) { 
        } 
    }); 
    
    btnSearch.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
    
         alSearchCourses = new ArrayList<>(); 
    
         int points = Integer.parseInt(edtSearch.getText().toString()); 
         for (int i = 0; i < courses.length; i++) { 
    
          Course c2 = courses[i]; 
    
          if (c2.getPoints() <= points) { 
           alSearchCourses.add(c2); 
          } 
    
         } 
    
         Course searchCourses [] = new Course[alSearchCourses.size()]; 
         searchCourses = alSearchCourses.toArray(searchCourses); 
    
         adapter = new listAdapter(MainActivity.this, searchCourses); 
         myList.setAdapter(adapter); 
    
        } 
    
    }); 
    
    } 
    } 
    
  2. listAdapter

    public class listAdapter extends BaseAdapter { 
    
    Context context; 
    Course[] courses; 
    LayoutInflater inflater; 
    
    public listAdapter(Context context, Course[] courses) { 
    this.context = context; 
    this.courses = courses; 
    inflater = LayoutInflater.from(context); 
    } 
    
    @Override 
    public int getCount() { 
    return courses.length; 
    } 
    
    @Override 
    public Object getItem(int position) { 
    return null; 
    } 
    
    @Override 
    public long getItemId(int position) { 
    return 0; 
    } 
    
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    
    if(convertView == null){ 
        convertView = inflater.inflate(R.layout.list_item_layout, null); 
    } 
    
    TextView txtCourse = (TextView) convertView.findViewById(R.id.txtCourse); 
    txtCourse.setText(courses[position].getName() + "-" +  courses[position].getPoints()); 
    
    return convertView; 
    } 
    } 
    

希望它能幫助:)

相關問題