我需要在ShowNews活动中显示新闻的描述,我使用了意图 并添加额外的方法将描述传递给ShowNews Activity,其中包含用于设置Description的textview。我的代码出了什么问题?
public class Downloader extends AsyncTask<Void,Void,Object> {
Context c;
String urlAddress ;
ListView lv;
ProgressDialog pd;
public Downloader(Context c,String urlAddress,ListView lv)
{
this.c = c;
this.urlAddress =urlAddress;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Fetching Data");
pd.setMessage("Fetchinf Data...please wait ");
pd.show();
}
@Override
protected Object doInBackground(Void... voids) {
return this.downloadData();
}
@TargetApi(Build.VERSION_CODES.CUPCAKE)
@Override
protected void onPostExecute(Object data) {
super.onPostExecute(data);
pd.dismiss();
if (data.toString().startsWith("Error"))
{
Toast.makeText(c,data.toString(),Toast.LENGTH_LONG).show();
}
else {
// parsing
new ReadRss(c, (InputStream) data,lv).execute();
}
}
private Object downloadData()
{
Object connection = Connector.connect(urlAddress);
if (connection.toString().startsWith("Error"))
{
return connection.toString();
}
try {
HttpURLConnection con = (HttpURLConnection) connection;
int responsecode = con.getResponseCode();
if (responsecode == con.HTTP_OK) {
InputStream is = new BufferedInputStream(con.getInputStream());
return is;
}
return ErrorTracer.RESPONSE_ERROR+con.getResponseMessage();
} catch (IOException e) {
e.printStackTrace();
return ErrorTracer.IO_ERROR;
}
}
}
public class ReadRss extends AsyncTask<Void,Void,Boolean> {
Context c;
InputStream is;
ListView lv;
ProgressDialog pd;
CustomAdapter adapter ;
ArrayList<Site> sites = new ArrayList<>();
public ReadRss(Context c,InputStream is,ListView lv)
{
this.c = c;
this.is =is;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("parsing Data");
pd.setMessage("parsing Data...please wait ");
pd.show();
}
@Override
protected Boolean doInBackground(Void... voids) {
return this.parseRss();
}
@Override
protected void onPostExecute(Boolean isparsed) {
super.onPostExecute(isparsed);
pd.dismiss();
if (isparsed)
{
//bind
lv.setAdapter(new CustomAdapter(c,sites));
}
else {
Toast.makeText(c,"Unable to parse",Toast.LENGTH_LONG).show();
}
}
private Boolean parseRss()
{
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(is,null);
int event = parser.getEventType();
String value = null;
sites.clear();
Site site = new Site();
do {
String name = parser.getName();
switch (event)
{
case XmlPullParser.START_TAG:
if (name.equals("item"))
{
site = new Site();
}
break;
case XmlPullParser.TEXT:
value = parser.getText();
break;
case XmlPullParser.END_TAG:
if (name.equals("title"))
{
site.setTitle(value);
}else if (name.equals("description"))
{
site.setDescription(value);
}else if (name.equals("pubDate"))
{
site.setData(value);
}else if (name.equals("link"))
{
site.setLink(value);
}
if (name.equals("item"))
{
sites.add(site);
}
break;
}
event = parser.next();
}while (event!=XmlPullParser.END_DOCUMENT);
return true;
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<Site> sites;
public CustomAdapter(Context c,ArrayList<Site> sites)
{
this.c = c;
this.sites = sites;
}
@Override
public int getCount() {
return sites.size();
}
@Override
public Object getItem(int i) {
return sites.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view==null)
{
view = LayoutInflater.from(c).inflate(R.layout.row,viewGroup,false);
}
TextView titletxt = (TextView) view.findViewById(R.id.textView);
TextView desctxt = (TextView) view.findViewById(R.id.textView2);
TextView datetxt = (TextView) view.findViewById(R.id.textView3);
Site site = (Site) this.getItem(i);
titletxt.setText(site.getTitle());
desctxt.setText(site.getDescription());
datetxt.setText(site.getData());
return view;
}
}
public class MainActivity extends AppCompatActivity {
String ulrAddress = "http://www.alahlytv.net/Rss_Feeds.aspx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this,ShowNews.class);
Site site = new Site();
intent.putExtra("one",site.getDescription());
startActivity(intent);
}
});
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Downloader(MainActivity.this,ulrAddress,lv).execute();
}
});
public class ShowNews extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_news);
TextView textView = (TextView) findViewById(R.id.textView4);
Intent intent = getIntent();
Bundle bd = intent.getExtras();
if(bd != null)
{
String getName = (String) bd.get("one");
textView.setText(getName);
}
}
}
答案 0 :(得分:0)
当您在intent
中添加任何额外内容时,请勿尝试从Bundle
获取该内容。因此,将您的阅读传递数据代码更改为:
Intent intent = getIntent();
String getName = intent.getStringExtra("one", "default_value_if_null");
textView.setText(getName);
答案 1 :(得分:0)
像这样更改您的代码
Intent intent = getIntent();
if(intent != null)
{
String getName = intent.getExtras().getString("one");
textView.setText(getName);
}