我设置了设计,我需要知道如何使用用户在第一个活动中输入的RSS源填充列表视图。然后onClick它打开第二个活动,打开一个列表,我需要用用户输入的URL RSS提要填充。之后,我需要单击列表中的项目,然后在另一个显示RSS新闻的活动或片段中打开它。下面是该程序的工作基础代码,我还没有添加第三个活动或片段,但我相信一旦我弄清楚这部分,我就能做到这一点。
public class MainActivity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, ListActivity.class);
startActivity(myIntent);
}
});
}
}
第二项活动
public class ListActivity extends MainActivity {
ListView rssList;
private RssData[] listData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
this.generateData();
rssList = (ListView) this.findViewById(R.id.RssListView);
RssAdapter listAdapter = new RssAdapter(this, android.R.layout.simple_list_item_1, listData);
rssList.setAdapter(listAdapter);
}
//rssNews = (ListView) findViewById(R.id.rssListView);
//rssNews.setAdapter(new RssAdapter(this,rd));
private void generateData() {
RssData data = null;
listData = new RssData[10];
for (int i = 0; i < 10; i++) {
data = new RssData();
data.postTitle = "Post " + (i + 1) + " Title: This is the Post Title from RSS Feed";
listData[i] = data;
}
}
这是我试图实现的一个类,这是在第二个Activity中的onCreate
Downloader d = new Downloader();
d.execute("https://www.google.com");
try {
src.setText(d.get());
}
catch(Exception e)
{
Log.e("ERROR IN THREAD", e.toString());
}
}
我尝试的另一个课程,这将是第二个活动的结束
class Downloader extends AsyncTask<String, Void, String> {
public String doInBackground(String... urls) {
String result = null;
try
{
URL url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
result = result + line;
}
conn.disconnect();
} catch (Exception e) {
Log.e("ERROR RETRIEVING", e.toString());
}
return result;
}
}
//on item selected from listView open new fragment/activity/layout
适配器
public class RssAdapter extends ArrayAdapter <RssData>
{
private Activity myContext;
private RssData[] datas;
public RssAdapter(Context context, int textViewResourceId,RssData[]objects)
{
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView;
LayoutInflater myInflater;
if(convertView ==null)
{
myInflater = myContext.getLayoutInflater();
convertView = myInflater.inflate(R.layout.list_item_layout, null);
}
rowView = convertView;
TextView postTitleView = (TextView) rowView.findViewById(R.id.readerViewDescription);
postTitleView.setText(datas[position].postTitle);
return rowView;
}
}
数据
public class RssData
{
public String postThumbUrl;
public String postTitle;
public String postDate;
}
XML布局文件 activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cj.rssreaderfi.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter URL for the desired RSS feed."
android:id ="@+id/URLTxtBox"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id ="@+id/URLEditTxt"
android:hint="www.Example.com"
android:layout_below="@+id/URLTxtBox"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrieve"
android:id="@+id/button"
android:layout_below="@+id/URLTxtBox"
android:layout_toEndOf="@+id/URLTxtBox"
android:onClick="retrieve"/>
</RelativeLayout>
list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/readerViewDescription"/>
</LinearLayout>
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/RssListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
</LinearLayout>