使用游标适配器在列表中添加项目

时间:2012-05-04 06:23:56

标签: java android

我有一个listView,其中我将一个表的列与customCursorAdapter绑定,即CoordinatorTag(如Home,Office,Factory)等来自Coordinator Table。但是对于这些标签,我还希望在名为ALL的列表中添加一个额外的项目最后。我怎么能这样做先生请帮助我。

感谢 Om Parkash

5 个答案:

答案 0 :(得分:0)

您可以使用addHeaderViewaddFooterView

答案 1 :(得分:0)

如果项目位于列表的末尾,则可以使用“页脚”视图。使用addFooterView(View v, Object data, boolean isSelectable)。确保在调用listView的setAdapter()之前调用此方法。

答案 2 :(得分:0)

是的,您可以使用addFooterView轻松完成。

lv.addFooterView(bottom_layout, null, false);

其中lv是要添加的listview,bottom_layout是要添加的视图或布局。

答案 3 :(得分:0)

如果要在数据集中添加项目,请考虑使用MatrixCursor及其方法newRow()

答案 4 :(得分:0)

看看这个......

我希望这个例子对你有帮助

创建/res/layout/row.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/listtitle"
android:textSize="22px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/listpubdate"
android:textSize="10px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

现在创建一个在ListView中使用游标适配器的类

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class AndroidRssReader extends ListActivity {

private RSSFeed myRssFeed = null;

public class MyCustomAdapter extends ArrayAdapter<RSSItem> {

 public MyCustomAdapter(Context context, int textViewResourceId,
   List<RSSItem> list) {
  super(context, textViewResourceId, list);
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  //return super.getView(position, convertView, parent);

  View row = convertView;

  if(row==null){
   LayoutInflater inflater=getLayoutInflater();
   row=inflater.inflate(R.layout.row, parent, false);
  }

  TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
  listTitle.setText(myRssFeed.getList().get(position).getTitle());
  TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
  listPubdate.setText(myRssFeed.getList().get(position).getPubdate());

  if (position%2 == 0){
   listTitle.setBackgroundColor(0xff101010);
   listPubdate.setBackgroundColor(0xff101010);
  }
  else{
   listTitle.setBackgroundColor(0xff080808);
   listPubdate.setBackgroundColor(0xff080808);
  }

  return row;
 }
}

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      try {
  URL rssUrl = new URL("http://www.gov.hk/en/about/rss/govhkrss.data.xml");
  SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
  SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
  XMLReader myXMLReader = mySAXParser.getXMLReader();
  RSSHandler myRSSHandler = new RSSHandler();
  myXMLReader.setContentHandler(myRSSHandler);
  InputSource myInputSource = new InputSource(rssUrl.openStream());
  myXMLReader.parse(myInputSource);

  myRssFeed = myRSSHandler.getFeed();

 } catch (MalformedURLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (ParserConfigurationException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (SAXException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }

 if (myRssFeed!=null)
 {
  TextView feedTitle = (TextView)findViewById(R.id.feedtitle);
  TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
  TextView feedPubdate = (TextView)findViewById(R.id.feedpubdate);
  TextView feedLink = (TextView)findViewById(R.id.feedlink);
  feedTitle.setText(myRssFeed.getTitle());
  feedDescribtion.setText(myRssFeed.getDescription());
  feedPubdate.setText(myRssFeed.getPubdate());
  feedLink.setText(myRssFeed.getLink());

  MyCustomAdapter adapter =
   new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
  setListAdapter(adapter);

 }
  }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
 // TODO Auto-generated method stub
 Intent intent = new Intent(this,ShowDetails.class);
 Bundle bundle = new Bundle();
 bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
 bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
 bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
 bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
 intent.putExtras(bundle);
      startActivity(intent);
}
}