2011-11-16 147 views
0

我從sqlite數據庫中獲取數據並嘗試在CustomeListView中顯示它但它顯示了一些項目示例我可以在我的列表視圖中看到六個項目,然後它顯示了六個項目當我把它的滾動,它會反覆顯示相同的項目在這裏是我的代碼無法在自定義ListView中顯示SQLite數據庫項目

我在這裏從數據庫

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與自定義列表

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; 

    } 

} 

} 

回答

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; 

    } 
更換,讓我知道發生後

..

+0

感謝名單兄弟。問題解決了 – Suru

+0

我得到了我再次犯錯thanx的地步。 – Suru

+0

它在if條件的子句中。 – user370305

相關問題