Android Blog client (6) OnItemLongClickListener for Blog, News and Comment, android Blog

Source: Internet
Author: User

Android Blog client (6) OnItemLongClickListener for Blog, News and Comment, android Blog

Project address: https://github.com/ZhangTingkuo/AndroidCnblogs

Add a long-press event for the ListView in the blog, news, and comments, and quickly go to the corresponding page.

1. Blog: View content, comments, and authors.

2. News: View content and comments.

3. Comments: Reference and reply. (Function not implemented yet)

1 package com. arlen. cnblogs. view; 2 3 import java. io. unsupportedEncodingException; 4 import java.net. URLEncoder; 5 import java. util. list; 6 7 import android. app. dialog; 8 import android. content. context; 9 import android. content. intent; 10 import android. OS. bundle; 11 import android. util. log; 12 import android. view. view; 13 import android. widget. adapterView; 14 import android. widget. adapterView. onIt EmClickListener; 15 import android. widget. arrayAdapter; 16 import android. widget. listView; 17 18 import com. arlen. cnblogs. blogActivity; 19 import com. arlen. cnblogs. commentActivity; 20 import com. arlen. cnblogs. newsActivity; 21 import com. arlen. cnblogs. r; 22 import com. arlen. cnblogs. userActivity; 23 import com. arlen. cnblogs. entity. blog; 24 import com. arlen. cnblogs. entity. comment; 25 import com. arlen. Cnblogs. entity. news; 26 import com. arlen. cnblogs. entity. user; 27 import com. arlen. cnblogs. utils. appUtils; 28 import com. arlen. cnblogs. utils. config; 29 30 public class ItemDialog extends Dialog implements OnItemClickListener {31 32 private Context context; 33 private String [] items; 34 private ListView listView; 35 public int selectIndex = 0; 36 37 public Blog blogEntry; 38 public News newsEntry; 39 public Comment commentEntry; 40 public String commentString; 41 public String TAG; 42 43 public ItemDialog (Context context, int theme) {44 super (context, theme); 45 this. context = context; 46} 47 48 public ItemDialog (Context context) {49 super (context); 50 this. context = context; 51} 52 53 public ItemDialog (Context context, String [] items) {54 super (context); 55 this. context = context; 56 This. items = items; 57} 58 59 @ Override 60 protected void onCreate (Bundle savedInstanceState) {61 super. onCreate (savedInstanceState); 62 this. setContentView (R. layout. item_dialog); 63 64 // this. setCanceledOnTouchOutside (false); 65 66 listView = (ListView) findViewById (R. id. listViewBlogDialog); 67 listView. setAdapter (new ArrayAdapter <String> (context, 68 android. r. layout. simple_list_item_1, items ); 69 listView. setOnItemClickListener (this); 70} 71 72 @ Override 73 public void onItemClick (AdapterView <?> Parent, View view, int position, 74 long id) {75 selectIndex = position; 76 Log. I ("ItemDialog", "selectIndex ---" + selectIndex); 77 78 String selectItem = items [position]; 79 80 if (selectItem. equals ("View content") {81 this. hide (); 82 if (TAG. equals ("blog") {83 viewBlogContent (); 84} else if (TAG. equals ("news") {85 viewNewsContent (); 86} 87} else if (selectItem. equals ("view comments") {88 this. hide (); 89 if (TAG. equals ("blog") {90 viewComment (blogEntry. getBlogId (), "blog"); 91} else if (TAG. equals ("news") {92 viewComment (newsEntry. getNewsId (), "news"); 93} 94} else if (selectItem. equals ("view Author") {95 this. hide (); 96 viewAuthor (); 97} else if (selectItem. equals ("reply") {98 this. hide (); 99 addComment (commentString, "reply"); 100} else if (selectItem. equals ("Reference") {101 this. hide (); 102 addC Omment (commentString, "Reference"); 103} else if (selectItem. equals ("cancel") {104 this. hide (); 105} 106 107 return; 108} 109 110 private void viewBlogContent () {111 Intent intent = new Intent (context, BlogActivity. class); 112 113 if (blogEntry. getAuthorAvatar ()! = Null) {114 intent. putExtra ("avatar", blogEntry. getAuthorAvatar (). toString (); 115} else {116 intent. putExtra (117 "avatar", 118 "https://github.com/ZhangTingkuo/AndroidCnblogs/blob/master/res/drawable-hdpi/ic_launcher.png"); 119} 120 intent. putExtra ("title", blogEntry. getBlogTitle (); 121 intent. putExtra ("author", blogEntry. getAuthorName (); 122 intent. putExtra ("published", 123 AppUtils. parseDateToStri Ng (blogEntry. getPublishedDateDate (); 124 intent. putExtra ("id", blogEntry. getBlogId (); 125 intent. putExtra ("link", blogEntry. getBlogTitle (); 126 127 context. startActivity (intent); 128} 129 130 private void viewNewsContent () {131 Intent intent = new Intent (context, NewsActivity. class); 132 if (newsEntry. getTopicIcon ()! = Null) {133 intent. putExtra ("topicIcon", newsEntry. getTopicIcon (). toString (); 134} else {135 intent. putExtra (136 "topicIcon", 137 "https://github.com/ZhangTingkuo/AndroidCnblogs/blob/master/res/drawable-hdpi/ic_launcher.png"); 138} 139 140 intent. putExtra ("title", newsEntry. getNewsTitle (); 141 intent. putExtra ("sourceName", newsEntry. getSourceName (); 142 intent. putExtra ("published", 143 AppUtils. pars EDateToString (newsEntry. getPublishedDate (); 144 intent. putExtra ("id", newsEntry. getNewsId (); 145 146 context. startActivity (intent); 147} 148 private void viewAuthor () {149 150 new Thread (new Runnable () {151 152 153 @ Override154 public void run () {155 String authorName = blogEntry. getAuthorName (); 156 String path = Config. SEARCH_AUTHOR_BY_NAME; 157 Log. I ("SearchActivity", "textAuthor:" + authorName ); 158 try {159 authorName = URLEncoder. encode (authorName, "UTF-8"); 160} catch (UnsupportedEncodingException e) {161 e. printStackTrace (); 162} 163 path = path. replace ("{TERM}", authorName); 164 165 Log. I ("SearchActivity", "path:" + path); 166 List <User> userList = AppUtils. getUserList (path); 167 User userEntry = userList. get (0); 168 169 Intent intent = new Intent (context, UserActivity. class); 170 intent. p UtExtra ("blogapp", userEntry. getBlogapp (); 171 intent. putExtra ("link", userEntry. getUserLink (). toString (); 172 173 if (userEntry. getUserAvatar ()! = Null) {174 intent. putExtra ("avatar", userEntry. getUserAvatar () 175. toString (); 176} else {177 intent. putExtra (178 "avatar", 179 "https://github.com/ZhangTingkuo/AndroidCnblogs/blob/master/res/drawable-hdpi/ic_launcher.png"); 180} 181 182 intent. putExtra ("postcount", userEntry. getPostCount (); 183 intent. putExtra ("updated", 184 AppUtils. parseDateToString (userEntry. getUpdatedDate (); 185 intent. putExtra ("title", userEntry. getTitle (); 186 context. startActivity (intent); 187} 188 }). start (); 189} 190 191 private void addComment (String string, String type) {192 193} 194 195 private void viewComment (int id, String type) {196 Intent intent = new Intent (context, CommentActivity. class); 197 intent. putExtra ("id", id); 198 intent. putExtra ("type", type); 199 context. startActivity (intent); 200} 201}

 

 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.