文章列表视图项目打开 url
Listview item opening url of article
我正在尝试设置 onitemclicklistener 以注册单击了哪个列表视图项目,然后抓取保存在 link 中的那篇文章的 url,然后打开一个网页 url 在一个新的 activity 中。我尝试的每件事都会导致无法编译的错误。目前,onitemclicklistener 在 holder.articleLink.setText(currentItem.get("link"));
以及我在最底部的 holder 部分中声明 setText 的下方抛出错误。
public class L {
public static void m(String message)
{
Log.d("test", message);
}
public static void s(Context context, String message)
{
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
public class MainActivity extends ActionBarActivity implements ResultsCallback {
PlaceholderFragment taskFragment;
ListView articlesListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
taskFragment = new PlaceholderFragment();
getFragmentManager().beginTransaction().add(taskFragment, "MyFragment").commit();
} else {
taskFragment = (PlaceholderFragment) getFragmentManager().findFragmentByTag("MyFragment");
}
taskFragment.startTask();
articlesListView = (ListView) findViewById(R.id.articlesListView);
}
public static class PlaceholderFragment extends Fragment {
BsuNewsTask downloadTask;
ResultsCallback callback;
public PlaceholderFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callback = (ResultsCallback) activity;
if(downloadTask != null) {
downloadTask.onAttach(callback);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public void startTask()
{
if(downloadTask != null) {
downloadTask.cancel(true);
} else {
downloadTask = new BsuNewsTask(callback);
downloadTask.execute();
}
}
@Override
public void onDetach() {
super.onDetach();
callback = null;
if(downloadTask!=null) {
downloadTask.onDetach();
}
}
}
public static class BsuNewsTask extends
AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
ResultsCallback callback= null;
public BsuNewsTask(ResultsCallback callback) {
this.callback = callback;
}
public void onAttach(ResultsCallback callback) {
this.callback = callback;
}
public void onDetach() {
callback = null;
}
@Override
protected void onPreExecute() {
if (callback != null) {
callback.onPreExecute();
}
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
String downloadURL="http://cms.bsu.edu/news/rss";
ArrayList<HashMap<String, String>> results = new ArrayList<>();
try {
URL url=new URL(downloadURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
results = processXML(inputStream);
} catch (Exception e) {
L.m(e + "");
}
return results;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
if (callback != null) {
callback.onPostExecute(result);
}
}
public ArrayList<HashMap<String, String>> processXML(InputStream inputStream) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document xmlDocument = documentBuilder.parse(inputStream);
Element rootElement = xmlDocument.getDocumentElement();
//print rootelement name in logcat
//L.m("" + rootElement.getTagName());
NodeList itemsList = rootElement.getElementsByTagName("item");
NodeList itemChildren = null;
Node currentItem = null;
Node currentChild = null;
ArrayList<HashMap<String, String>> results = new ArrayList<>();
HashMap<String, String> currentMap = null;
for(int i=0; i < itemsList.getLength(); i++) {
currentItem = itemsList.item(i);
itemChildren = currentItem.getChildNodes();
currentMap = new HashMap<>();
for (int j=0; j < itemChildren.getLength(); j++) {
currentChild=itemChildren.item(j);
if(currentChild.getNodeName().equalsIgnoreCase("title")) {
//L.m(currentChild.getTextContent());
currentMap.put("title", currentChild.getTextContent());
}
if(currentChild.getNodeName().equalsIgnoreCase("description")) {
//L.m(currentChild.getTextContent());
currentMap.put("description", currentChild.getTextContent());
}
if(currentChild.getNodeName().equalsIgnoreCase("link")) {
//L.m(currentChild.getTextContent());
currentMap.put("link", currentChild.getTextContent());
}
if(currentChild.getNodeName().equalsIgnoreCase("pubDate")) {
//L.m(currentChild.getTextContent());
currentMap.put("pubDate", currentChild.getTextContent());
}
}
if(currentMap != null && !currentMap.isEmpty()) {
results.add(currentMap);
}
}
return results;
}
}
@Override
public void onPreExecute() {
}
@Override
public void onPostExecute(ArrayList<HashMap<String, String>> results) {
articlesListView.setAdapter(new MyAdapter(this, results));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onListItemClick(ListView l, View v, int position, long id) {
MyAdapter adapter = (MyAdapter)l.getAdapter();
Uri uri = Uri.parse(adapter.dataSource.get(position).get("link"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
interface ResultsCallback {
public void onPreExecute();
public void onPostExecute(ArrayList<HashMap<String, String>> results);
}
class MyAdapter extends BaseAdapter{
ArrayList<HashMap<String, String>> dataSource = new ArrayList<>();
Context context;
LayoutInflater layoutInflater;
public MyAdapter(Context context, ArrayList<HashMap<String, String>> dataSource) {
this.context=context;
this.dataSource = dataSource;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return dataSource.size();
}
@Override
public Object getItem(int position) {
return dataSource.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyHolder holder = null;
if(row == null){
row = layoutInflater.inflate(R.layout.custom_row, parent, false);
holder = new MyHolder(row);
row.setTag(holder);
}
else{
holder = (MyHolder) row.getTag();
}
HashMap<String, String> currentItem = dataSource.get(position);
holder.articleTitleText.setText(currentItem.get("title"));
holder.articleDescriptionText.setText(currentItem.get("description"));
holder.articleDateText.setText(currentItem.get("pubDate"));
return row;
}
}
class MyHolder
{
TextView articleTitleText;
TextView articleDescriptionText;
TextView articleDateText;
public MyHolder(View view) {
articleTitleText=(TextView) view
.findViewById(R.id.articleTitleText);
articleDescriptionText = (TextView) view
.findViewById(R.id.articleDescriptionText);
articleDateText = (TextView) view
.findViewById(R.id.articleDateText);
}
}
尝试从您的数据源中获取 link:
public void onListItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView l = (ListView) parent;
MyAdapter adapter = (MyAdapter)l.getAdapter();
Uri uri = Uri.parse(adapter.dataSource.get(position).get("link"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
但首先,实现 AdapterView.OnItemClickListener 接口(抱歉,我无法弄清楚您的 onListItemClick 方法是如何调用的。如果您已经以其他方式执行此操作,请忽略它)
public class MainActivity extends ActionBarActivity implements ResultsCallback,AdapterView.OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
articlesListView = (ListView) findViewById(R.id.articlesListView);
articlesListView.setOnItemClickListener(this);
}
编译你的代码后,我看到你需要做一些修改:
在ListView
中设置项目点击监听器,这样:
articlesListView.setOnItemClickListener(this);
之后,你必须让你的 activity 实现 OnItemClickListener
,这样:
public class MainActivitySO extends ActionBarActivity implements ResultsCallback,
AdapterView.OnItemClickListener {
然后,您必须在 activity 中实现方法 onItemClick
。所以,在这个方法中,你输入你已有的代码:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyAdapter adapter = (MyAdapter) articlesListView.getAdapter();
Uri uri = Uri.parse(adapter.dataSource.get(position).get("link"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
我正在尝试设置 onitemclicklistener 以注册单击了哪个列表视图项目,然后抓取保存在 link 中的那篇文章的 url,然后打开一个网页 url 在一个新的 activity 中。我尝试的每件事都会导致无法编译的错误。目前,onitemclicklistener 在 holder.articleLink.setText(currentItem.get("link"));
以及我在最底部的 holder 部分中声明 setText 的下方抛出错误。
public class L {
public static void m(String message)
{
Log.d("test", message);
}
public static void s(Context context, String message)
{
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
public class MainActivity extends ActionBarActivity implements ResultsCallback {
PlaceholderFragment taskFragment;
ListView articlesListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
taskFragment = new PlaceholderFragment();
getFragmentManager().beginTransaction().add(taskFragment, "MyFragment").commit();
} else {
taskFragment = (PlaceholderFragment) getFragmentManager().findFragmentByTag("MyFragment");
}
taskFragment.startTask();
articlesListView = (ListView) findViewById(R.id.articlesListView);
}
public static class PlaceholderFragment extends Fragment {
BsuNewsTask downloadTask;
ResultsCallback callback;
public PlaceholderFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callback = (ResultsCallback) activity;
if(downloadTask != null) {
downloadTask.onAttach(callback);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public void startTask()
{
if(downloadTask != null) {
downloadTask.cancel(true);
} else {
downloadTask = new BsuNewsTask(callback);
downloadTask.execute();
}
}
@Override
public void onDetach() {
super.onDetach();
callback = null;
if(downloadTask!=null) {
downloadTask.onDetach();
}
}
}
public static class BsuNewsTask extends
AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
ResultsCallback callback= null;
public BsuNewsTask(ResultsCallback callback) {
this.callback = callback;
}
public void onAttach(ResultsCallback callback) {
this.callback = callback;
}
public void onDetach() {
callback = null;
}
@Override
protected void onPreExecute() {
if (callback != null) {
callback.onPreExecute();
}
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
String downloadURL="http://cms.bsu.edu/news/rss";
ArrayList<HashMap<String, String>> results = new ArrayList<>();
try {
URL url=new URL(downloadURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
results = processXML(inputStream);
} catch (Exception e) {
L.m(e + "");
}
return results;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
if (callback != null) {
callback.onPostExecute(result);
}
}
public ArrayList<HashMap<String, String>> processXML(InputStream inputStream) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document xmlDocument = documentBuilder.parse(inputStream);
Element rootElement = xmlDocument.getDocumentElement();
//print rootelement name in logcat
//L.m("" + rootElement.getTagName());
NodeList itemsList = rootElement.getElementsByTagName("item");
NodeList itemChildren = null;
Node currentItem = null;
Node currentChild = null;
ArrayList<HashMap<String, String>> results = new ArrayList<>();
HashMap<String, String> currentMap = null;
for(int i=0; i < itemsList.getLength(); i++) {
currentItem = itemsList.item(i);
itemChildren = currentItem.getChildNodes();
currentMap = new HashMap<>();
for (int j=0; j < itemChildren.getLength(); j++) {
currentChild=itemChildren.item(j);
if(currentChild.getNodeName().equalsIgnoreCase("title")) {
//L.m(currentChild.getTextContent());
currentMap.put("title", currentChild.getTextContent());
}
if(currentChild.getNodeName().equalsIgnoreCase("description")) {
//L.m(currentChild.getTextContent());
currentMap.put("description", currentChild.getTextContent());
}
if(currentChild.getNodeName().equalsIgnoreCase("link")) {
//L.m(currentChild.getTextContent());
currentMap.put("link", currentChild.getTextContent());
}
if(currentChild.getNodeName().equalsIgnoreCase("pubDate")) {
//L.m(currentChild.getTextContent());
currentMap.put("pubDate", currentChild.getTextContent());
}
}
if(currentMap != null && !currentMap.isEmpty()) {
results.add(currentMap);
}
}
return results;
}
}
@Override
public void onPreExecute() {
}
@Override
public void onPostExecute(ArrayList<HashMap<String, String>> results) {
articlesListView.setAdapter(new MyAdapter(this, results));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onListItemClick(ListView l, View v, int position, long id) {
MyAdapter adapter = (MyAdapter)l.getAdapter();
Uri uri = Uri.parse(adapter.dataSource.get(position).get("link"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
interface ResultsCallback {
public void onPreExecute();
public void onPostExecute(ArrayList<HashMap<String, String>> results);
}
class MyAdapter extends BaseAdapter{
ArrayList<HashMap<String, String>> dataSource = new ArrayList<>();
Context context;
LayoutInflater layoutInflater;
public MyAdapter(Context context, ArrayList<HashMap<String, String>> dataSource) {
this.context=context;
this.dataSource = dataSource;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return dataSource.size();
}
@Override
public Object getItem(int position) {
return dataSource.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyHolder holder = null;
if(row == null){
row = layoutInflater.inflate(R.layout.custom_row, parent, false);
holder = new MyHolder(row);
row.setTag(holder);
}
else{
holder = (MyHolder) row.getTag();
}
HashMap<String, String> currentItem = dataSource.get(position);
holder.articleTitleText.setText(currentItem.get("title"));
holder.articleDescriptionText.setText(currentItem.get("description"));
holder.articleDateText.setText(currentItem.get("pubDate"));
return row;
}
}
class MyHolder
{
TextView articleTitleText;
TextView articleDescriptionText;
TextView articleDateText;
public MyHolder(View view) {
articleTitleText=(TextView) view
.findViewById(R.id.articleTitleText);
articleDescriptionText = (TextView) view
.findViewById(R.id.articleDescriptionText);
articleDateText = (TextView) view
.findViewById(R.id.articleDateText);
}
}
尝试从您的数据源中获取 link:
public void onListItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView l = (ListView) parent;
MyAdapter adapter = (MyAdapter)l.getAdapter();
Uri uri = Uri.parse(adapter.dataSource.get(position).get("link"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
但首先,实现 AdapterView.OnItemClickListener 接口(抱歉,我无法弄清楚您的 onListItemClick 方法是如何调用的。如果您已经以其他方式执行此操作,请忽略它)
public class MainActivity extends ActionBarActivity implements ResultsCallback,AdapterView.OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
articlesListView = (ListView) findViewById(R.id.articlesListView);
articlesListView.setOnItemClickListener(this);
}
编译你的代码后,我看到你需要做一些修改:
在
ListView
中设置项目点击监听器,这样:articlesListView.setOnItemClickListener(this);
之后,你必须让你的 activity 实现
OnItemClickListener
,这样:public class MainActivitySO extends ActionBarActivity implements ResultsCallback, AdapterView.OnItemClickListener {
然后,您必须在 activity 中实现方法
onItemClick
。所以,在这个方法中,你输入你已有的代码:@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MyAdapter adapter = (MyAdapter) articlesListView.getAdapter(); Uri uri = Uri.parse(adapter.dataSource.get(position).get("link")); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); }