i am not able to display the detailed form information specific to the title user clicked on form1 Screen,when i click on any itemlist on form1 screen,i am able to display the detail of first item only(in my code int index=myNewsList.getSelectedIndex() returns always 0 as value)
Here my Detailed Code for Rss App:
//method called by the parsing thread
public void addNews(News newsItem) {
newsVector.addElement(newsItem);//initialsed list with vector
myNewsList = new List(newsVector);
myNewsList.setListCellRenderer(new NewsListCellRenderer());
form1.addComponent(myNewsList);
form1.show();
myNewsList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int selectedIndex = myNewsList.getSelectedIndex();
if(selectedIndex != -1){
newsItem1 = (News)news.elementAt(selectedIndex);
Label l=new Label();
l.setText(newsItem1.getPubDate());
Form detailedForm=new Form();
detailedForm.addCommand(m_backCommand);
detailedForm.addCommandListener(this);
detailedForm.addComponent(l);
detailedForm.show();
}
}
});
}
Can you help?
Add action listener to the list. It is called only if you click any item of the list. In that action listener, get the selected item and cast it to the News class object because you added News class objects in the list. From that object, get the unique property like news id. Pass it to another screen with the current form object (form1).
myNewsList = new List(news);
myNewsList.setListCellRenderer(new NewsListRenderer());
myNewsList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
News allNewsClassObjs = (News) myNewsList.getSelectedItem();
int newsid = allNewsClassObjs.getNewsId();
displayCompleteNewsScreen(form1,newsid);
}
});
form1.addComponent(myNewsList);
form1.addCommand(cmdDetails);
form1.setScrollable(true);
form1.setTransitionInAnimator(Transition3D.createRotation(250, true));
form1.show();
With the news id, you can display the related data in another screen. Add back command to it. In the back command, just show the form1 object.
public void displayCompleteNewsScreen(Form form1,int newsid){
// Get the related data and add it to another form object(form2).
form2.addCommand("Back");
form2.addCommandListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
form1.show();
}
});
form2.show();
}
Instead of using
int selectedIndex = myNewsList.getSelectedIndex();
if(selectedIndex != -1){
newsItem1 = (News)news.elementAt(selectedIndex);
}
Use the below code
newsItem1 = (News)myNewsList.getSelectedItem();