2015-03-31 40 views
0

我正在尝试设置onitemclicklistener来注册哪个列表视图项目被点击,然后抓取保存在链接中的文章的URL,然后在新的活动中打开一个带有该URL的网页。我所尝试的一切都会导致错误,导致无法编译。目前,onitemclicklistener在holder.articleLink.setText(currentItem.get("link"));以及低于我声明最底部的持有者部分中的setText的地方发出错误。文章的列表视图项目打开url

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); 


} 
} 
+0

什么是 “L” 在L.M(E + “”);? – androidevil 2015-03-31 01:33:52

+0

这是一个基本上使用的教程,它使用'public static void m(String message){Log.d(“test”,message);}调用logcat。 } public static void s(Context context,String message)' – 2015-03-31 02:20:55

+0

为那些已经问过的人添加了日志类 – 2015-03-31 02:41:13

回答

0

编译代码后,我看你有没有做一些小修改:

  • 设定的项目点击监听的ListView,这种方式:

    articlesListView.setOnItemClickListener(this); 
    
  • 在这之后,你必须让你的活动实现了OnItemClickListener,这种方式:

    public class MainActivitySO extends ActionBarActivity implements ResultsCallback, 
    AdapterView.OnItemClickListener { 
    
  • 然后了,你会我必须在您的活动中实施方法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); 
    } 
    
+0

添加'articlesListView =(ListView)findViewById(R.id.articlesListView);'用这些代码,编译并加载文章到网页 – 2015-03-31 13:50:46

+0

你没有在你的代码的第一个实现中? – androidevil 2015-03-31 13:55:16

+0

看看它现在的编译结果如何,在if/else语句之前和之后加入2x – 2015-04-01 19:40:17

0

尝试从数据源获取链接:

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); 
} 
+0

我试过这段代码,并且在公共类MainActivity部分它说类'MainActivity'必须被声明为抽象或implment抽象方法'onItemClick(AdapterView ,View,int,long)'onItemClickListener'在底部的这个也是说adapterview不能被应用,可能是因为执行错误 – 2015-03-31 02:12:26

+0

我试图实现这个,它想让这个类抽象,所以我做了,并设置了一个view.onclicklistener。然后,我不得不将listitemclick更改为'AdapterView parent,View v,int position,long l'来满足错误,并且当我试图运行该程序时,它只是说它停止并且根本不显示listview – 2015-03-31 02:19:39

+0

刚刚拿到一堆logcat的错误,我把它放在pastebin上太长了。 http://pastebin.com/wA1a1QPF – 2015-03-31 02:25:42