我对Listview有一个问题,它扩展了Listactivity。
我有一个类扩展Listview,并且我正在使用Array<使用DomParser存储我的解析数据。
现在在列表布局中我正在给文本视图和图像视图充气。
我需要的是点击图像视图我必须保存相应的行详细信息
喜欢的最爱。这是我的代码
package com.example.marketingapp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
public class NewsXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://apps.shareholder.com/rss/rss.aspx?
channels=6323&companyid=SYNT";
// XML node keys
static final String NEWS_KEY_ITEM = "item"; // parent node
static final String NEWS_KEY_TITLE = "title";
static final String NEWS_KEY_LINK = "link";
static final String NEWS_KEY_DESCRIPTION = "description";
static final String NEWS_KEY_PUB = "pubDate";
ListView lv;
InteractiveArrayAdapter adapter;
public boolean[] checkBoxState;
String key, pair;
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
new LoadNews().execute();
}
class LoadNews extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewsXMLParsingActivity.this);
pDialog.setMessage("loading......");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(NEWS_KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(NEWS_KEY_TITLE, parser.getValue(e, NEWS_KEY_TITLE));
map.put(NEWS_KEY_LINK, parser.getValue(e, NEWS_KEY_LINK));
map.put(NEWS_KEY_DESCRIPTION,
parser.getValue(e, NEWS_KEY_DESCRIPTION));
map.put(NEWS_KEY_PUB, parser.getValue(e, NEWS_KEY_PUB));
// adding HashList to ArrayList
menuItems.add(map);
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// lv = getListView();
adapter = new InteractiveArrayAdapter(
NewsXMLParsingActivity.this, R.layout.list_item1,
menuItems);
setListAdapter(adapter);
}
});
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(NewsXMLParsingActivity.this, "hkjhkj", 500).show();
HashMap<String, String> map = menuItems.get(position);
String val = (String) map.get(NEWS_KEY_LINK);
//checkBoxState = new boolean[menuItems.size()];
Intent intent = new Intent(NewsXMLParsingActivity.this,
NewsActivity.class);
intent.setData(Uri.parse(val));
startActivity(intent);
DatabaseWrapper db = new DatabaseWrapper(this);
// add Books
db.addBookmark(new Bookmarks(val, "NEWS"));
// get all books
// List<Bookmarks> list = db.getAllBookmark();
db.getAllBookmark();
}
}
这是我的适配器类,我用图片给listview充气 这一点很重要。建议我怎么做
public class InteractiveArrayAdapter extends
ArrayAdapter<HashMap<String, String>> {
public boolean[] checkBoxState;
ViewHolder viewholder;
private LayoutInflater inflater;
private ArrayList<HashMap<String, String>> menuItems;
public InteractiveArrayAdapter(Context context, int resource,
ArrayList<HashMap<String, String>> menuItems) {
super(context, resource, menuItems);
this.menuItems = menuItems;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checkBoxState = new boolean[menuItems.size()];
}
private class ViewHolder {
// ImageView photo;
TextView title, link, published, description;
// ImageButton newsCheck;
ImageView newsCheck;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// inflater =
// LayoutInflater.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item1, null);
viewholder = new ViewHolder();
// cache the views
// viewholder.photo=(ImageView)
// convertView.findViewById(R.id.photo);
viewholder.title = (TextView) convertView
.findViewById(R.id.news_title);
// viewholder.link = (TextView) convertView.findViewById(R.id.link);
viewholder.published = (TextView) convertView
.findViewById(R.id.published);
viewholder.description = (TextView) convertView
.findViewById(R.id.description);
viewholder.newsCheck = (ImageView) convertView
.findViewById(R.id.newsCheck);
// link the cached views to the convertview
convertView.setTag(viewholder);
} else
viewholder = (ViewHolder) convertView.getTag();
HashMap<String, String> catalog_list = new HashMap<String, String>();
catalog_list = menuItems.get(position);
// viewholder.link.setText(catalog_list.get("NEWS_KEY_LINK")
//
// viewholder.link.setText(catalog_list
// .get(NewsXMLParsingActivity.NEWS_KEY_LINK));
viewholder.title.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_TITLE));
viewholder.published.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_PUB));
viewholder.description.setText(catalog_list
.get(NewsXMLParsingActivity.NEWS_KEY_DESCRIPTION));
viewholder.newsCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageView i=(ImageView) v;
i.getTag();
if (i.isClickable()) {
// viewholder.newsCheck.setClickable(checkBoxState[position]);
// checkBoxState[position] = true;
//
// if(checkBoxState[position]==true){
// viewholder.newsCheck.setImageResource(position);
// }
// Toast.makeText(getContext(),
// "checked" + checkBoxState[position], 500).show();
// }
//
// else
// checkBoxState[position] = false;
//
// }
// });
return convertView;
}
}
这是我的适配器类,我用图片给listview充气 这一点很重要。建议我怎么做 接受所有建议。以上是我的适配器类................ 请帮帮我,我被打了很多天....