无法在自定义ListView中显示SQLite数据库项

时间:2011-11-16 10:45:06

标签: android database listview sqlite

我从sqlite数据库获取数据并尝试在CustomeListView中显示它但是它显示了一些项目示例我一次可以看到我的listView中的六个项目然后当我滚动它时它会显示六个项目,它将重复显示相同的项目这里是我的代码

我在这里查询数据库

public ArrayList<GetEmailFromDatabase> emails_From_Database() {
    SQLiteDatabase db = helper.getReadableDatabase();
    try {
        GetEmailFromDatabase gefd;
        ArrayList<GetEmailFromDatabase> results = new ArrayList<GetEmailFromDatabase>();
        Cursor c = db.rawQuery("select * from Emailreceived", null);
        if (c.getCount() > 0) {
            int i=0;
            c.moveToPosition(i);
            do {
                gefd = new GetEmailFromDatabase();
                gefd.setMessage_No(c.getInt(c.getColumnIndex("_Id")));
                gefd.setFrom(c.getString(c.getColumnIndex("EmailFrom")));
                gefd.setDate(c.getString(c.getColumnIndex("EmailDate")));
                gefd.setSubject(c.getString(c.getColumnIndex("Subject")));
                results.add(gefd);
                gefd = null;
                i++;
            } while (c.moveToPosition(i));
            c.close();
        }
        return results;
    } finally {
        if (db != null)
            db.close();
        }
}

这是我的listActivity with custom list

public class Email_listActivity extends MenuOptions{

//Database getEmails = new Database(getApplicationContext());
String items[]={"red","white","Black","green","brown","yellow","blue","pink","orange"}; 
Database database;
String mData;

ArrayList<GetEmailFromDatabase> emailResults;

Database db1;
private EmailListFeedAdapter emailListFeedAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email_list_main);

    emailResults = new ArrayList<GetEmailFromDatabase>();

    emailListFeedAdapter = new EmailListFeedAdapter(this, R.layout.email_listview_row, emailResults);
    setListAdapter(this.emailListFeedAdapter);

        getResults();
        if(emailResults != null && emailResults.size() > -1){
            emailListFeedAdapter.notifyDataSetChanged();
        for(int i=0;i< emailResults.size();i++){
            emailListFeedAdapter.add( emailResults.get(i));
            Log.e("%%%%%%%%%%%%%%%%%%%", "emailResults -------->"+emailListFeedAdapter.areAllItemsEnabled());
        }
        }
}
@Override

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    int pos = position;
    String s = (String) ((TextView) v.findViewById(R.id.item_number)).getText();
    database = new Database(getApplicationContext());
    mData = database.getDate(s);

    Intent i = new Intent(Email_listActivity.this, EmailWebView.class);
    i.putExtra("webData", mData);
    startActivity(i);
    Log.e("$$$$$$$$$$$$", "POSITION--------->"+position+"   View-------->"+v);
    Toast.makeText(getApplicationContext(), "Hello"+v, Toast.LENGTH_SHORT).show();
}
public void getResults(){
    db1 = new Database(getBaseContext());
    emailResults = new ArrayList<GetEmailFromDatabase>();
    //ArrayList<GetEmailFromDatabase> fromdatabase = db1.emails_From_Database();
    emailResults = db1.emails_From_Database();

}

public class EmailListFeedAdapter extends ArrayAdapter<GetEmailFromDatabase>{

    private ArrayList<GetEmailFromDatabase> itemss;
    public EmailListFeedAdapter(Context context, int textViewResourceId, ArrayList<GetEmailFromDatabase> items) {
        super(context, textViewResourceId, items);
        this.itemss = items; 
    }

    @Override
    public View getView(int position, View emailView, ViewGroup parent) {
        if(emailView == null){
        LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        emailView= inflater.inflate(R.layout.email_listview_row, null);

        GetEmailFromDatabase o = itemss.get(position);
        if(o!=null){
        TextView  messageNo = (TextView)emailView.findViewById(R.id.item_number);
        TextView  subject= (TextView)emailView.findViewById(R.id.emaillistrow_txt_partofemail);
        TextView  sender= (TextView)emailView.findViewById(R.id.emaillistrow_txt_sender);
        TextView  datetime= (TextView)emailView.findViewById(R.id.emaillistrow_txt_datetime);
        ImageView icon= (ImageView)emailView.findViewById(R.id.emaillistrow_icon_indicator);
        icon.setImageResource(R.drawable.y_icon);

        if(messageNo!=null){
        messageNo.setText(Integer.toString(o.getMessage_No()));
        }
        if(subject!=null){
        subject.setText(o.getSubject());
        }
        if(sender!=null){
        sender.setText(o.getFrom());
        }
        if(datetime!=null){
        datetime.setText(o.getDate());
        }
        }
      }
      return emailView;

    }

}

}

1 个答案:

答案 0 :(得分:2)

好的,你可以用我的替换你的适配器类的getView()方法,

@Override
    public View getView(int position, View emailView, ViewGroup parent) {
        if(emailView == null){
        LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        emailView= inflater.inflate(R.layout.email_listview_row, null);
        }
        GetEmailFromDatabase o = itemss.get(position);
        if(o!=null){
        TextView  messageNo = (TextView)emailView.findViewById(R.id.item_number);
        TextView  subject= (TextView)emailView.findViewById(R.id.emaillistrow_txt_partofemail);
        TextView  sender= (TextView)emailView.findViewById(R.id.emaillistrow_txt_sender);
        TextView  datetime= (TextView)emailView.findViewById(R.id.emaillistrow_txt_datetime);
        ImageView icon= (ImageView)emailView.findViewById(R.id.emaillistrow_icon_indicator);
        icon.setImageResource(R.drawable.y_icon);

        if(messageNo!=null){
        messageNo.setText(Integer.toString(o.getMessage_No()));
        }
        if(subject!=null){
        subject.setText(o.getSubject());
        }
        if(sender!=null){
        sender.setText(o.getFrom());
        }
        if(datetime!=null){
        datetime.setText(o.getDate());
        }
        }

      return emailView;

    }
替换后让我知道发生了什么......