单击搜索项时数据不会打开

时间:2015-02-10 14:51:11

标签: android database sqlite search

您好我有这个Android项目,在应用程序中搜索作者并点击结果后,我得到一个空视图,而不是作者显示他或她的报价

这是我正在使用的代码...谢谢

package com.dennisboga.successquotes;

 import java.util.ArrayList;
 import java.util.HashMap;

 import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.AdView;

   import android.widget.AdapterView.OnItemClickListener;
    import android.os.Bundle;
      import android.app.Activity;
  import android.content.Intent;
 import android.database.Cursor;
  import android.text.Editable;
   import android.text.TextWatcher;
   import android.view.View;
    import android.widget.AdapterView;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.ListView;

  public class AuthorsActivity extends Activity {

ListView list;
LazyAuthorsAdapter adapter;
LazyAuthorsAdapter adapterSort;

DAO db;
Cursor cu;
static final String KEY_ID = "_auid";
static final String KEY_NAME = "au_name";
static final String KEY_PICTURE = "au_picture";
static final String KEY_PICTURE_SDCARD = "au_picture_sdcard";
static final String KEY_WEB_ID = "au_web_id";
static final String KEY_COUNT = "count";
static final String KEY_QUOTES_NUM = "quotes_num";
ImageButton search;
EditText inputSearch;
int textlength = 0;
ArrayList<HashMap<String, String>> authorsList;
ArrayList<HashMap<String, String>> authorsListSort = new ArrayList<HashMap<String, String>>();;
HashMap<String, String> map;

  //     =============================================================================    =

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new DAO(this);
    db.open();

    //addAuthors();
    //addQuotes();
    Cursor c = db.getAllAuthors();
    if (c.getCount() != 0) {
        setContentView(R.layout.activity_authors);

        authorsList = new ArrayList<HashMap<String, String>>();

        do {
            map = new HashMap<String, String>();

            // adding each child node to HashMap key =&gt; value
            map.put(KEY_ID, c.getString(c.getColumnIndex(KEY_ID)));
            map.put(KEY_NAME, c.getString(c.getColumnIndex(KEY_NAME)));
            map.put(KEY_PICTURE,  c.getString(c.getColumnIndex(KEY_PICTURE)));
            map.put(KEY_PICTURE_SDCARD, String.valueOf(c.getInt(c
                    .getColumnIndex(KEY_PICTURE_SDCARD))));
            map.put(KEY_WEB_ID,
                    String.valueOf(c.getInt(c.getColumnIndex(KEY_WEB_ID))));
            map.put(KEY_QUOTES_NUM,
                    c.getString(c.getColumnIndex(KEY_COUNT)));

            // adding HashList to ArrayList
            authorsList.add(map);
        } while (c.moveToNext());

        list = (ListView) findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter = new LazyAuthorsAdapter(this, authorsList);
        list.setAdapter(adapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if (authorsListSort.size() != 0) {
                    map = authorsListSort.get(position);
                } else {
                    map = authorsList.get(position);
                }

                Intent intent = new Intent(AuthorsActivity.this,
                        QuotesActivity.class);
                intent.putExtra("AuthorId", map.get(KEY_WEB_ID));
                intent.putExtra("quotesType", 3);
                startActivity(intent);

            }
        });

        search = (ImageButton) findViewById(R.id.search);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        search.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if (inputSearch.getVisibility() == View.GONE) {
                    inputSearch.setVisibility(View.VISIBLE);
                } else {
                    inputSearch.setText("");
                    inputSearch.setVisibility(View.GONE);
                }

            }
        });

        inputSearch = (EditText) findViewById(R.id.inputSearch);

        inputSearch.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {

            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {

                authorsListSort.clear();

                for (int i = 0; i < authorsList.size(); i++) {
                    map = authorsList.get(i);
                    String auID = map.get(KEY_ID);
                    String auName = map.get(KEY_NAME);
                    String auPicture = map.get(KEY_PICTURE);
                    String auPictureSDCard = map.get(KEY_PICTURE_SDCARD);
                    String auQuotesNum = map.get(KEY_QUOTES_NUM);

                    if (auName.toLowerCase().startsWith(
                            s.toString().toLowerCase())
                            || auName.toLowerCase().contains(
                                    " " + s.toString().toLowerCase())) {
                        map = new HashMap<String, String>();

                        // adding each child node to HashMap key =&gt;
                        // value
                        map.put(KEY_ID, auID);
                        map.put(KEY_NAME, auName);
                        map.put(KEY_PICTURE, auPicture);
                        map.put(KEY_PICTURE_SDCARD, auPictureSDCard);
                        map.put(KEY_QUOTES_NUM, auQuotesNum);

                        // adding HashList to ArrayList
                        authorsListSort.add(map);
                    }
                }
                // }
                adapterSort = new LazyAuthorsAdapter(AuthorsActivity.this,
                        authorsListSort);
                list.setAdapter(adapterSort);

            }
        });
    } else {

    }

    AdView ad = (AdView) findViewById(R.id.adView);
    ad.loadAd(new AdRequest.Builder().build());

    }

   //    =============================================================================   =

  @Override
  protected void onResume() {
    db.open();
    super.onResume();
    }

    //     =============================================================================    =

  @Override
  protected void onRestart() {
    // TODO Auto-generated method stub
    Intent intent = getIntent();
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    startActivity(intent);
    super.onRestart();
  }

    //     =============================================================================    =

 @Override
 protected void onPause() {
    db.closeDatabase();
    super.onPause();
 }

//   private void addAuthors() {
//   cu = db.getAuthors2();
//  
//   if (cu.getCount() != 0) {
   //   
   //    do {
   //    db.addAuthors2(cu.getString(cu.getColumnIndex("au_name")),
  //     cu.getInt(cu.getColumnIndex("_auid")));
   //   
     //  } while (cu.moveToNext());
   //    }
  //     }
  //
  //          =============================================================================       =
 //  private void addQuotes() {
  //     cu = db.getQuotes2();
 // 
 //  if (cu.getCount() != 0) {
//  
  //     do {
 //  db.addQuotes2(cu.getString(cu.getColumnIndex("qu_text")),
//   cu.getInt(cu.getColumnIndex("qu_author")),
//   cu.getInt(cu.getColumnIndex("_quid")));
 // 
//   } while (cu.moveToNext());
  //     }
 //  }

}

quotesactivity

package com.dennisboga.successquotes;

 import java.util.ArrayList;
  import java.util.HashMap;

import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.AdView;

 import android.widget.AdapterView.OnItemClickListener;
 import android.os.Bundle;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.database.Cursor;
  import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ImageButton;
  import android.widget.ListView;

public class QuotesActivity extends Activity {

  ListView list;
LazyQuotesAdapter adapter;

AlertDialog PageDialog;

DAO db;
Cursor c;
static final String KEY_ID = "_quid";
static final String KEY_TEXT = "qu_text";
static final String KEY_AUTHOR = "au_name";
static final String KEY_PICTURE = "au_picture";
static final String KEY_PICTURE_SDCARD = "au_picture_sdcard";
static final String KEY_WEB_ID = "au_web_id";
static final String KEY_FAVORITE = "qu_favorite";

ArrayList<HashMap<String, String>> quotesList;
HashMap<String, String> map;

ArrayList<HashMap<String, String>> pages;
Integer quType;
ImageButton page;
int itemSelected;

// ==============================================================================

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new DAO(this);
    db.open();
    setContentView(R.layout.activity_quotes);

    AdView ad = (AdView) findViewById(R.id.adView);
    ad.loadAd(new AdRequest.Builder().build());

    quType = getIntent().getIntExtra("quotesType", 0);
    page = (ImageButton) findViewById(R.id.pager);
    if (quType != 0) {
        switch (quType) {
        case 1:
            if (getIntent().getStringExtra("startFrom") == null) {
                c = db.getQuotes("0");
            } else {
                c = db.getQuotes(getIntent().getStringExtra("startFrom"));
            }

            break;
        case 2:
            c = db.getFavoriteQuotes();
            page.setVisibility(View.GONE);
            break;
        case 3:
            c =  db.getAuthorQuotes(getIntent().getStringExtra("AuthorId"));
            page.setVisibility(View.GONE);
            break;
        }
    }

    if (c.getCount() != 0) {

        if (getIntent().getStringExtra("startLabel") != null) {
            page = (ImageButton) findViewById(R.id.pager);
        }
        quotesList = new ArrayList<HashMap<String, String>>();

        list = (ListView) findViewById(R.id.list);

        do {
            map = new HashMap<String, String>();

            // adding each child node to HashMap key =&gt; value
            map.put(KEY_ID, c.getString(c.getColumnIndex(KEY_ID)));
            map.put(KEY_TEXT, c.getString(c.getColumnIndex(KEY_TEXT)));
            map.put(KEY_AUTHOR, c.getString(c.getColumnIndex(KEY_AUTHOR)));
            map.put(KEY_PICTURE, c.getString(c.getColumnIndex(KEY_PICTURE)));
            map.put(KEY_PICTURE_SDCARD, String.valueOf(c.getInt(c
                    .getColumnIndex(KEY_PICTURE_SDCARD))));
            map.put(KEY_WEB_ID,
                    String.valueOf(c.getInt(c.getColumnIndex(KEY_WEB_ID))));

            // adding HashList to ArrayList
            quotesList.add(map);

        } while (c.moveToNext());

        // Getting adapter by passing xml data ArrayList
        adapter = new LazyQuotesAdapter(this, quotesList);
        list.setAdapter(adapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                map = quotesList.get(position);

                Intent intent = new Intent(QuotesActivity.this,
                        QuoteDialogActivity.class);
                intent.putExtra("QuoteId", map.get(KEY_ID));
                intent.putExtra("quotesType", quType);
                startActivity(intent);

            }
        });

        page.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Strings to Show In Dialog with Radio Buttons
                final CharSequence[] items = { "01 - 50", "51 - 100",
                        "101 - 150", "151 - 200", "201 - 250", "251 - 300",
                        "301 - 350", "351 - 400", "401 - 450", "451 - 500",
                        "501 - 550", "551 - 600", "601 - 650", "651 - 700",
                        "701 - 750", "751 - 800", "801 - 850", "851 -  900",
                        "901 - 950", "951 - 1000", "1001 - ..." };

                pages = new ArrayList<HashMap<String, String>>();
                for (int i = 0; i <= 1000; i += 50) {
                    HashMap<String, String> pageMap = new  HashMap<String, String>();
                    pageMap.put("start", String.valueOf(i));
                    pages.add(pageMap);

                }

                // Creating and Building the Dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        QuotesActivity.this);
                builder.setTitle("Display Quotes");

                itemSelected = getIntent().getIntExtra("itemSelected",  0);

                builder.setSingleChoiceItems(items, itemSelected,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int item) {

                                Intent intent = getIntent();
                                intent.putExtra("startFrom", pages
                                        .get(item).get("start"));
                                intent.putExtra("startLabel",  items[item]);
                                intent.putExtra("itemSelected", item);
                                  intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                                finish();
                                startActivity(intent);
                            }
                        });

                builder.setNegativeButton("Dismiss",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {

                                // action on dialog close
                            }

                        });
                PageDialog = builder.create();
                PageDialog.show();

            }
        });

    } else {

    }

}

  //  =============================================================================  =

@Override
 protected void onResume() {
     db.open();
    super.onResume();
     }

    //   =============================================================================    =

@Override
protected void onPause() {
    db.closeDatabase();
    super.onPause();
  }

}

0 个答案:

没有答案