2015-04-03 46 views
0

我遇到了我的listView问题,并从中获取正确的项目。 我的应用程序是一个管理员应用程序,允许教师为特定学生添加课程笔记。我的情况如下:Android:尝试使用listView访问独特的ID

当老师增加了一个新的教训音符,它会自动给它一个唯一的ID

使学生1(ID 1)具有课名单与IDS指出:1,3,4 4,5和6

学生2(ID 2)具有课注意到IDS:2,7,8和9

学生1具有的列表视图:1,2,3,4,5

学生2有列表视图:1,2,3,4,5

所以,当学生1访问注意事项1,查询如下:SELECT * FROM table_lessons其中lessonID = 1和StudentID = 1

注2:SELECT * FROM table_lessons其中lessonID = 2和StudentID = 1

注3:从table_lessons选择*其中lessonID = 3和StudentID = 1

等等....

但是你看那个学生1不具备的教训记 '2' - 这样的程序崩溃

我觉得ListView控件的位置离扔程序...

这是在我的lessonNotesList类:(修订版)

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_lesson_notes_list); 
    Button backToMain = (Button)findViewById(R.id.backToMain); 
    backToMain.setOnClickListener(this); 
    Bundle extras = getIntent().getExtras(); 
    dyslexiaDb = new DBHelper(this); 
    ArrayList array_list = dyslexiaDb.getAllStudents(); 
    //Creating an ArrayList of contacts data 
    //ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.list_layout, array_list); 
    LessonAdapter adapter = new LessonAdapter(array_list); 
    ListView listView = new ListView(this); 
    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      // the id parameter returns the adapter's getItemId method 
      //intent.putExtra("lesson_id", id); 
     } 
    }); 
} 

LessonAdapter类:

public class LessonAdapter extends BaseAdapter{ 
    private ArrayList<LessonNotesData> lessonNotes; 

    public LessonAdapter(ArrayList<LessonNotesData> lessonNotes) { 
     this.lessonNotes = lessonNotes; 
    } 
    @Override 
    public int getCount(){ 
     // get the number of lessons stored in the array 
     return lessonNotes.size(); 
    } 
    @Override 
    public LessonNotesData getItem(int item){ 
     // return the lesson object 
     return lessonNotes.get(item); 
    } 
    @Override 
    public long getItemId(int item){ 
     // return the lessonNotes id 
     return getItem(item).getLessonID(); 
    } 
    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     // inflate the layout item you want to use 
     view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_lesson_notes_list, viewGroup, false); 
     //((TextView)  view.findViewById(R.id.listViewMain)).setText(getItem(i).getDate()); 
     return view; 
    } 
} 

activity_lesson_notes_list .xml

谢谢

回答

1

我假设你正在使用对象作为你的课程,所以我做了一个模拟课程,以课程对象为例。下面是一些代码片段,应该让你在正确的轨道上。如果您有更多问题,请随时询问。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    LessonAdapter adapter = new LessonAdapter(getStudentsLessons()); 
    ListView listView = new ListView(this); 
    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      // the id parameter returns the adapter's getItemId method 
      intent.putExtra("lesson_id", id); 
     } 
    }); 

} 

public class LessonAdapter extends BaseAdapter { 

    private ArrayList<Lesson> lessons; 

    public LessonAdapter(ArrayList<Lesson> lessons) { 
     this.lessons = lessons; 
    } 

    @Override 
    public int getCount() { 
     // get the number of lessons 
     return lessons.size(); 
    } 

    @Override 
    public Lesson getItem(int i) { 
     // return the lesson object 
     return lessons.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     // return the lesson's id 
     return getItem(i).getId(); 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     // inflate the layout item you want to use 
     // below is using the build in simple list item, but you can sub in a custom one if you want 
     view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false); 
     ((TextView) view.findViewById(android.R.id.text1)).setText(getItem(i).getName()); 
     return view; 
    } 
} 

public class Lesson { 
    private int id; 
    private String name; 

    public int getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 
} 
0

你的问题是不是与ListView但为什么dyslexiaDb.getAllLessonNotes(studentID)回报2时明确这一教训还没有被分配到学生1

理想的情况下,内部getAllLessonNotes()你的SQL应该像

SELECT * FROM table_lessons WHERE StudentID = 1 

这将返回定制的List它仍然可以与正常ArrayAdapter使用如果重写Lesson#toString()适当Lesson对象。

这也有助于避免第二个数据库调用来检索所有的教训细节与

select * from table_lessons where lessonID = 2 and StudentID = 1 

,因为你可以直接通过IntentLesson对象传递给您的详细活动。