Hey all..I am struck here, I have to display news in a separate list view from a news link web site, but when I debug the cursor goes null. How do I resolve this? Here is my code
package adn.GoMizzou.NB;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org开发者_StackOverflow.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class NB extends ListActivity {
public static final String URL = "http://nbsubscribe.missouri.edu/news-releases/feed/atom/";
private String msg;
private boolean success;
private int scrollIndex;
private int scrollTop;
private HttpClient httpClient;
private NBDBAdapter dbAdapter;
private ProgressDialog pDialog;
private Context ctx = this;
private SharedPreferences prefs;
private SharedPreferences.Editor prefsEditor;
//private Cursor cur;
private Cursor q;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg){
pDialog.dismiss();
fillList();
}
};
/* ACTIVITY METHODS */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nb);
setTitle("News");
q=null;
scrollIndex = 0;
dbAdapter = new NBDBAdapter(this);
dbAdapter.open();
registerForContextMenu(getListView());
getData();
}
public void getData(){
pDialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
new Thread(){
public void run(){
dbAdapter.deleteAll();
dbAdapter.close();
doPost(URL, "");
dbAdapter.open();
handler.sendEmptyMessage(0);
}
}.start();
}
public boolean doPost(String url, String postMsg){
HttpResponse response = null;
createHttpClient();
try {
URI uri = new URI(url);
HttpPost httppost = new HttpPost(uri);
StringEntity postEntity = new StringEntity(postMsg);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
postEntity.setContentType("application/x-www-form-urlencoded");
httppost.setEntity(postEntity);
response = httpClient.execute(httppost);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(response == null){
msg = "No internet connection.";
return false;
}
if(response.getStatusLine().getStatusCode()!= 200){
msg = "Server error.";
return false;
}
return doParse(response);
}
public void createHttpClient(){
if(httpClient == null){
httpClient = new DefaultHttpClient();
}
}
public boolean doParse(HttpResponse response){
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
NBParser respHandler = new NBParser(ctx);
xr.setContentHandler(respHandler);
HttpEntity entity = response.getEntity();
BufferedHttpEntity buffEntity = new BufferedHttpEntity(entity);
xr.parse(new InputSource(buffEntity.getContent()));
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
if(e.getMessage().toString().contains("nothing found")){
msg = e.getMessage().toString();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = q;
q.moveToPosition(position);
//Intent i = new Intent(this, NB.class);
//i.putExtra("link", c.getString(c.getColumnIndexOrThrow(NBDBAdapter.NB_LINK)));
String newsLinkString = q.getString(q.getColumnIndexOrThrow(NBDBAdapter.NB_LINK));
TextView linkTV = (TextView) v.findViewById(R.id.rowlink);
newsLinkString = linkTV.getText().toString();
if (newsLinkString.startsWith("http://")){
Uri uri = Uri.parse(newsLinkString);
Intent i = new Intent(this, NB.class);
// Save ListView position
i.putExtra("link", c.getString(c.getColumnIndexOrThrow(NBDBAdapter.NB_LINK)));
startActivity(i);
}
}
//else {
//showLinkError();
//}
// Sends an error message to the user if the news item link is malformed
//public void showLinkError() {
//Toast.makeText(getApplicationContext(), "Error - Link to story unavailable with news source could not load the News Story", Toast.LENGTH_SHORT).show();
//}
private void showLinkError() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Error - Link to story unavailable with news source could not load the News Story", Toast.LENGTH_SHORT).show();
}
public void fillList(){
if(!success) {
TextView emptyView = (TextView) findViewById(android.R.id.empty);
emptyView.setText(msg);
msg = "";
}
if(!dbAdapter.isOpen()) dbAdapter.open();
Cursor cursor = dbAdapter.fetchAll();
startManagingCursor(cursor);
String [] from = new String[] { dbAdapter.NB_AUTHOR, dbAdapter.NB_TITLE,dbAdapter.NB_LINK };
int[] to = new int[] { R.id.rowAuthor, R.id.rowTitle, R.id.rowlink };
SimpleCursorAdapter list = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
setListAdapter(list);
}
}
Which cursor are you talking about?
If it's when you click on an item it's because you assign the cursor q null in onCreate and then call moveToPosition on it in onListItemClick.
精彩评论