0
그래서 저는 BBC에서 온 뉴스와 함께 ListView를 가지고 있습니다. ListView의 각 Item에는 뉴스의 제목과 작은 설명이 있습니다. 그러나 항목 중 하나를 클릭하면 URL이 필요합니다. 나는 그런 일이 일어나기를 원하지 않는다. 그래서 아이템이 클릭되었을 때 나는 뉴스가 제목 인 Image와 뉴스의 몸체로 표시되는 anothe 레이아웃으로 나를 데려 가기를 원한다.TextView에 HTML 추가
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/row_view" >
<TextView
android:id="@+id/title"
android:inputType="none"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:id="@+id/description"
android:inputType="none"
android:maxLines="2"
android:minLines="1"
android:ellipsize="end"
android:gravity="left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:textSize="14sp" />
이리스트 뷰 레이아웃입니다 : 이것은 레이아웃에 내 코드가
public class Main extends Activity implements OnItemClickListener {
private class RowItem {
String title = null;
String description = null;
String url = null;
public RowItem(String title, String description, String url) {
this.title = title;
this.description = description;
this.url = url;
}
}
private class DownloadXMLTask extends AsyncTask<String, Void, String> {
ArrayList<RowItem> items = null;
public DownloadXMLTask(ArrayList<RowItem> items) {
this.items = items;
}
// local helper method
private String getNodeValue(Element entry, String tag) {
Element tagElement = (Element) entry.getElementsByTagName(tag).item(0);
return tagElement.getFirstChild().getNodeValue();
}
private String downloadAndParseXML(String url) {
try {
InputStream in = new java.net.URL(url).openStream();
// build the XML parser
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// parse and get XML elements
Document dom = db.parse(in);
Element documentElement = dom.getDocumentElement();
// we want all XML children called 'item'
NodeList nodes = documentElement.getElementsByTagName("item");
// for each 'item' do the following
for (int i = 0; i < nodes.getLength(); i++) {
Element entry = (Element) nodes.item(i);
// get the nodes from 'item' that you need
String title = getNodeValue(entry, "title");
String description = getNodeValue(entry, "description");
String link = getNodeValue(entry, "link");
// add them to your list
items.add(new RowItem(title, description, link));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected String doInBackground(String... urls) {
if (urls.length <= 0)
return null;
return downloadAndParseXML(urls[0]);
}
protected void onPostExecute(String result) {
updateList(items);
}
}
public void updateList(ArrayList<RowItem> items) {
CustomArrayAdapter adapter = new CustomArrayAdapter(this,
R.layout.item, items);
listView.setAdapter(adapter);
}
// class used to have custom view for the list item
private class CustomArrayAdapter extends ArrayAdapter<RowItem> {
Context context;
List<RowItem> items;
private class ViewHolder {
public TextView title;
public TextView description;
}
public CustomArrayAdapter(Context context, int resource,
List<RowItem> items) {
super(context, resource, items);
this.context = context;
this.items = items;
}
@Override
public View getView(int position, View rowView, ViewGroup parent) {
// reuse the rowView if possible - for efficiency and less memory consumption
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.item, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) rowView.findViewById(R.id.title);
viewHolder.description = (TextView) rowView.findViewById(R.id.description);
rowView.setTag(viewHolder);
}
// set the view
ViewHolder holder = (ViewHolder) rowView.getTag();
RowItem item = items.get(position);
holder.title.setText(item.title);
holder.description.setText(item.description);
return rowView;
}
}
ListView listView;
ArrayList<RowItem> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// get the listView and set this to be the onItemClickListener
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
// create the RowItem list - used for storing one news feed
items = new ArrayList<RowItem>();
// start the background task to get the news feed
new DownloadXMLTask(items).execute("http://feeds.bbci.co.uk/news/rss.xml");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
RowItem item = items.get(position);
// start browser with the url.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.url));
startActivity(browserIntent);
}
}
입니다 :
이
내가 가지고 무엇을 제발 도와주세요
답변을 유용하게 찾았 으면이를 수락하고/또는 upvote하십시오. –