Home > Mobile >  Listview click jump value problem
Listview click jump value problem

Time:01-05

 public class MainActivity extends AppCompatActivity {

ListView LVS.
Map Map3;
List & gt; Itmes=new ArrayList<> (a);
Private List NewsList=new ArrayList<> (a);


@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
setContentView(R.layout.activity_main);
The LVS=MainActivity. Enclosing the findViewById (R.i d.l v);//get the ListView control
NewsAdapter adapter=new NewsAdapter (MainActivity. This, R.l ayout. Lv2, newsList);
/* ListView ListView=the findViewById (R.i d.l v);
ListView. SetAdapter (adapter); */
BTN ();
}

//defines methods button click on the button for the selected url assigned to download () method
Public static String url;
Public void BTN () {//domestic news (by default)
Itmes. The clear ();
Url="http://www.people.com.cn/rss/politics.xml";
The download ();

};
Public void btn1 (View View) {//domestic news
Itmes. The clear ();
Url="http://www.people.com.cn/rss/politics.xml";
The download ();

};
Public void btn2 View (View) {//sports news
Itmes. The clear ();
Url="http://www.people.com.cn/rss/sports.xml";
The download ();
//"http://www.people.com.cn/rss/haixia.xml"
};
Public void btn3 View (View) {//Taiwan news
Itmes. The clear ();
Url="http://www.people.com.cn/rss/haixia.xml";
The download ();

};


Handler Handler=new Handler () {//to create an Android message processing mechanism to rewrite handleMessage method
@ Override
Public void handleMessage (Message MSG) {//process the Message, the method of using handleMessage to process the Message, the inside of the parameter Message
//the message object can accept any type of object and can be through a reverse casts back
String title=((News) MSG. Obj). GetTitle ();
A String for its pubDate=((News) MSG. Obj). GetPubDate ();
String author=((News) MSG. Obj). GetAuthor ();
A String description=((News) MSG. Obj). The getDescription ();

Map3=new HashMap<> (a);//create a map object replication transfer
Map3. Put (" title ", the title);
Map3. Put (" for its pubDate, "for its pubDate);
Map3. Put (" author ", the author);
Map3. Put (" description ", the description);

Itmes. Add (map3);//put the map object in the list collection inside


The LVS. SetAdapter (new SimpleAdapter (MainActivity. This itmes, R.l ayout. Lv2, new String [] {" title ", "for its pubDate", "the author"}
, new int [] {R.i which xt_count, R.i which xt_Date, R.i which xt_author}));
The LVS. SetOnItemClickListener ((the parent, the view, position, id) - & gt; {
News News=newsList. Get (position);
Bundle bundle=new Bundle();
Bundle. PutString (" title ", news getTitle ());
Bundle. PutString (" time ", for its pubDate);
Bundle. PutString (" author ", the author);
Bundle. PutString (" description ", the description);

Intent Intent=new Intent (MainActivity. This, NewsDescription. Class);
Intent. PutExtras (bundle);
startActivity(intent);
});
}
};




//create a method of the download file
Public void the download () {

//create a worker Thread, inherit the Thread to run method, processing time consuming operation
New Thread () {//create the child Thread quotation download process is a slow process, the main Thread can not use a

//rewrite the run () method,
@ Override
Public void the run () {
Try {
The Context Context=MainActivity. This. GetApplication ();//get a Context object
String path=context. GetFilesDir () + "/test. The XML";//purpose in order to obtain the android file directory
String url1=url;//create a download link
URL the URL=new URL (url1);//create a URL
HttpURLConnection con=(HttpURLConnection) url. OpenConnection ();//get an HttpURLConnection object con
Con. SetConnectTimeout (3 * 1000);
//to prevent blocking fetching and returns a 403 error
Con. SetRequestProperty (" the user-agent ", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT. DigExt) ");

InputStream iso=con. GetInputStream ();//get an input stream iso object by con
File file=new File(path);//create a file object to write path into the
FileOutputStream fos=new FileOutputStream (file);
Byte [] b=new byte [1024].
int count=0;
While ((count=iso. Read (b, 0102 (4)).=1) {
Fos. Write (b, 0, count);
System. The out. Print (b + "/KB");
}
fos.close();
Iso. The close ();

List Pull pull=();//call the Pull parse () method to parse the XML document and returns List Object

For (News n: pull) {//traverse the list collection
The Message MSG=new Message ();//create a Message object M
MSG. Obj=n;//the message object can accept any type of object and can be through a reverse casts back
Handler. SendMessage (MSG);
}

}
The catch (Exception e) {
System. The out. Println (um participant etMessage ());
}
}
}.start();
}

Public List Pull () {
Try {

The Context Context=MainActivity. This. GetApplication ();
The String url=context. GetFilesDir () + "/test. The XML";
InputStream fis=new FileInputStream (url);

List List=NewsPull. The getNews (fis);
For (News n: list) {
System.out.println(n);
}
return list;
} the catch (Exception e) {
e.printStackTrace();
Return null;
{} the catch (Throwable Throwable)
Throwable. PrintStackTrace ();
Return null;
}

}

}

Which how to obtain the LVS Settings click event to the corresponding title, date, author and description to the next interface
?As the current News News=newsList. Get (position); Don't know why complains about this for a Java lang. IndexOutOfBoundsException: Index: 3, Size: 0
null
  • Related