2011-05-06 18 views
1

我有幾個方法在ListAC ListActivity,我想重用,並希望把放在單獨的類(如果可能??)。我將有幾十個ListView活動(ListActivities,即:ListAD,ListTCDS,listSFAR等等),這些活動將調用這些活動中完全相同的方法,但是'Cursor databaseCursor'中的唯一更改(即。每個ListActivity的表名)。我打算將這些類和相關的代碼放到它們自己的包中,而不是在應用程序中(即:com.myCompany.myApp.AC)。另外,這些活動需要多個查詢的'ORDER BY'。 :「...」列出「ASC | DESC」,「...」標題「ASC | DESC」等)。 B4遊標被調用,我也有檢查ExternalStorageState的方法。我得到了來自LeffelMania(THNX!)的類StorageStateChecker(請參閱下面的內容)重用ExternalStorageState,我仍想使用它 - 即使它未在下面的代碼中實現。我決定退後一步*重新考慮這個問題(我把自己搞砸了 - LOL),讓你們和gals更容易閱讀代碼。如何重新使用ListViews的方法?

如您所見,這會在設備上導致大量不必要的冗餘和字節空間。我需要擠壓設備上的所有可能的空間,因爲這個應用程序將是這些設備用戶唯一的唯一目的(如果項目向前發展 - LOL)。因此,我需要一種方法,通過分開的類調用這些方法,儘可能使用我寫這些方法和我的適配器的方式來減少它。任何有關建議,方法,如何以及代碼的幫助都會對我學習Java/Android非常感激和有幫助。日Thnx!

*本帖與HEREHERE有關並正在繼續。

更新:已完成的類位於REVISED塊的下方。 Thnx對所有幫助我,現在我正在更好地理解類&方法的用法。

ListAC活動:

public class ListAC extends ListActivity { 
/** 
* -- Called when the activity is first created 
* =================================================================== 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.list_view2); 

    activityTitle = (TextView) findViewById(R.id.titleBarTitle); 
    activityTitle.setText("ADVISORY CIRCULATORS"); 

    storageState(); 
    searchList(); 
    nextActivity(); 
} 

/** 
* -- Check to See if the SD Card is Mounted & Loads Default List Order 
* ====================================================================== 
**/ 
private void storageState() { 
    if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED)) { 

     orderASC_Label();// Loads the list 

    } else if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_UNMOUNTED)) { 
     Alerts.sdCardMissing(this); 
    } 
} 

/** 
* -- Default List Order (Ascending) 
* ===================================================================== 
**/ 
public void orderASC_Label() { 
    Cursor databaseCursor = db.rawQuery(
      "SELECT * FROM AC_list ORDER BY `label` ASC", null); 

    Adapter_AC databaseListAdapter = new Adapter_AC(this, 
      R.layout.list_item, databaseCursor, new String[] { "label", 
        "title", "description", "gotoURL" }, new int[] { 
        R.id.label, R.id.listTitle, R.id.caption, R.id.dummy }); 

    databaseListAdapter.notifyDataSetChanged(); 
    this.setListAdapter(databaseListAdapter); 
} 

/** 
* -- Starts the Next Activity 
* ===================================================================== 
**/ 
public void nextActivity() { 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setClickable(true); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> a, View v, int pos, long id) { 

      String url = ""; 
      TextView tv = (TextView) v.findViewById(R.id.dummy); 
      url = (String) tv.getTag(); 

      String lbl = ""; 
      TextView tv2 = (TextView) v.findViewById(R.id.label); 
      lbl = (String) tv2.getTag(); 

      Intent i = new Intent(List_AC.this, DocView.class); 
      i.putExtra("url", url); 
      i.putExtra("label", lbl); 
      startActivity(i); 
     } 
    }); 
} 

/** 
* -- Dispatched when the Menu-Key is presses 
* ===================================================================== 
**/ 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_MENU) { 
     Intent i = new Intent(List_AC.this, DashBoard.class); 
     startActivity(i); 
    } 
    return super.onKeyDown(keyCode, event); 
} 

/** 
* -- Local Variables 
* ===================================================================== 
**/ 
protected TextView activityTitle; 
boolean mExternalStorageAvailable = false; 
boolean mExternalStorageWriteable = false; 
String extStorageDirectory = Environment.getExternalStorageDirectory() 
     .toString(); 
File dbfile = new File(extStorageDirectory 
     + "/XXX/xxx/dB/xxx.db"); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); 

/** ======================= END ==================================== **/ 
} 

我的適配器(AdaperAC):

public class AdapterAC extends SimpleCursorAdapter { 


static Cursor dataCursor; 
private LayoutInflater mInflater; 

public Adapter_AC(Context context, int layout, Cursor dataCursor, 
     String[] from, int[] to) { 
    super(context, layout, dataCursor, from, to); 
    this.dataCursor = dataCursor; 
    mInflater = LayoutInflater.from(context); 
} 

public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_item, null); 

     holder = new ViewHolder(); 
     holder.text1 = (TextView) convertView.findViewById(R.id.label); 
     holder.text2 = (TextView) convertView.findViewById(R.id.listTitle); 
     holder.text3 = (TextView) convertView.findViewById(R.id.caption); 
     holder.text4 = (TextView) convertView.findViewById(R.id.dummy); 

     holder.text4.setVisibility(View.GONE); 

     convertView.setTag(holder); 

    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    dataCursor.moveToPosition(position); 

    int label_index = dataCursor.getColumnIndex("label"); 
    String label = dataCursor.getString(label_index); 

    int title_index = dataCursor.getColumnIndex("title"); 
    String title = dataCursor.getString(title_index); 

    int description_index = dataCursor.getColumnIndex("description"); 
    String description = dataCursor.getString(description_index); 

    int goto_index = dataCursor.getColumnIndex("gotoURL"); 
    String gotoURL = dataCursor.getString(goto_index); 

    holder.text1.setText(label); 
    holder.text1.setTag(label); 
    holder.text2.setText(title); 
    holder.text3.setText(description); 
    //holder.text4.setText(gotoURL); 
    holder.text4.setTag(gotoURL); 

    return convertView; 
} 

static class ViewHolder { 
    TextView text1; 
    TextView text2; 
    TextView text3; 
    TextView text4; 
} 

} 

修改爲:

public class QueryDisplay extends ListActivity { 

protected TextView activityTitle; 

boolean mExternalStorageAvailable = false; 
boolean mExternalStorageWriteable = false; 

String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
File dbfile = new File(extStorageDirectory + "/myComp/myApp/dB/myApp.db"); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); 

private static final String QUERY_KEY = "QUERY_KEY"; 

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

    Bundle extras = getIntent().getExtras(); 

    String q = null; 
    if (extras != null) { 

     q = extras.getString(QUERY_KEY); 
     Log.i("tag", "getting extras" + extras); 
    } 

    reloadQuery(q); 
} 

public void reloadQuery(String q) { 

    Log.i("tag", "reloadQuery"); 

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 

     Cursor c = db.rawQuery(q, null); 
     setListAdapter(new QueryAdapter(this, c)); 
     db.close(); 

    }else { 
     Alerts.sdCardMissing(this); 
    } 
} 

private class QueryAdapter extends CursorAdapter { 

    public QueryAdapter(Context context, Cursor c) { 
     super(context, c); 
     LayoutInflater.from(context); 
    } 

    @Override 
    public void bindView(View v, Context context, Cursor c) { 

      int tvLabel = c.getColumnIndexOrThrow("label"); 
      String label = c.getString(tvLabel); 
      TextView labelTxt = (TextView) v.findViewById(R.id.label); 

      if (labelTxt != null) { 
       labelTxt.setText("(" + label + ")"); 
      } 

      int tvTitle = c.getColumnIndexOrThrow("title"); 
      String title = c.getString(tvTitle); 
      TextView titleTxt = (TextView) v.findViewById(R.id.listTitle); 

      if (titleTxt != null) { 
       titleTxt.setText(title); 
      } 

      int tvDescription = c.getColumnIndexOrThrow("description"); 
      String description = c.getString(tvDescription); 
      TextView descriptionTxt = (TextView) v.findViewById(R.id.caption); 

      if (descriptionTxt != null) { 
       descriptionTxt.setText(description); 
      } 

      int tvGoto= c.getColumnIndexOrThrow("gotoURL"); 
      String gotoURL = c.getString(tvGoto); 
      TextView gotoTxt = (TextView) v.findViewById(R.id.dummy); 

      if (gotoTxt != null) { 
       gotoTxt.setText(gotoURL); 
      } 

      gotoTxt.setVisibility(View.GONE); 
      v.setTag(tvGoto); 
    } 

    @Override 
    public View newView(Context context, Cursor c, ViewGroup parent) { 

     final View v = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); 
     return v; 
    } 
} 
} 

...以及如何調用&注入查詢:

OnClickListener myClickListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (v == myBtn) { 

      String queryKey = "SELECT * FROM a_tablet ORDER BY `column` ASC"; 
      Intent i = new Intent(CurrentClass.this,QueryDisplay.class); 
      i.putExtra("QUERY_KEY", queryKey); 
      startActivity(i); 
      } 
+0

我知道;編輯過多-LOL – CelticParser 2011-05-06 22:59:05

+1

您的'QUERY_KEY'爲空,因此當您嘗試從意圖中獲取額外內容時,它也會返回null。 private static final String QUERY_KEY = null; 應該類似於 private static final String QUERY_KEY =「QUERY_KEY」; – techiServices 2011-05-08 17:51:27

+0

@techiServices thnx,是的,我意識到那個星期六晚上。我沒有改變任何其他的東西,但添加了「QUERY_KEY」;現在我現在沒有得到任何錯誤,但是列表在列表中填充了正確數量的行,但在TextView中沒有任何數據,並且它們也可以選擇。 ??? – CelticParser 2011-05-09 03:07:32

回答

2

看起來你似乎在做非常多的額外工作,只是爲了給你的光標提供一堆不同的查詢選項。

爲什麼不用一個ListActivity,只有一個CursorAdapter,並且只需要將查詢放入Intent就可以啓動Activity?

我會研究一個小的代碼示例,並在編輯過程中發佈它,但您真的在想這件事。你所要做的就是在ListView中顯示查詢結果。唯一改變的是查詢。重新使用其他一切。

代碼示例:

public class QueryDisplay extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     Bundle extras = getIntent().getExtras(); 

     if (extras != null) 
      reloadQuery(extras.getString(QUERY_KEY)); 

    } 

    private void reloadQuery(query) { 
     // Build your Cursor here. 
     setAdapter(new QueryAdapter(this, cursor)); 
    } 

    private class QueryAdapter extends CursorAdapter { 

     @Override 
     public void bindView(View view, Context context, Cursor cursor) { 
      // Set up your view here 
     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      // Create your new view here 
      final View view = LayoutInflator.from(context).inflate(R.layout.your_query_list_item_layout, parent, false); 
      return view; 
     } 
    } 
} 

這是字面上所有你可能需要的代碼(加上填充插件爲您的自定義視圖和構建光標)。無論何時您需要更改查詢,只需撥打reloadQuery(String query)即可設置。

+0

那是正確的 - 重複使用其他所有內容,是的,我過度思考它。這就是爲什麼我需要退後一步並重新獲得某人的關注。 Thnx @ LeffelMania – CelticParser 2011-05-06 17:37:53

+1

@camelCaser我通常不喜歡發佈整個班級的人,因爲整個「給一個人一條魚/教一個人釣魚」的諺語,但希望你至少可以從這個設計課程中學習。 – LeffelMania 2011-05-06 17:55:14

+0

感謝您@LeffelMania今天我將在工作站內進行關閉工作,所以我可能在星期一纔開始工作。 – CelticParser 2011-05-06 17:56:13

1

擴展SimpleCursorAdapter的代碼中是否有任何相似之處?關於像獲取外部存儲狀態這樣的代碼,您可以實現一個名爲Utils的類,並使其中的方法爲靜態。但是,您可能需要將ContextActivity作爲參數傳遞。

例如,這是我的外部存儲狀態檢查器。

public static final boolean isExternalStorageAvailable() { 
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
     return true; 
    } else { 
     return false; 
    } 
} 

而我的網絡狀態檢查器。

public static final boolean isPreferedNetworkAvailable(final Context context) { 
    final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(connectivityManager.getNetworkPreference()); 
    return networkInfo.isAvailable(); 
} 
+1

@camelCaser:首先,如果您只是使用一個'Activity',那麼'isExternalStorageAvailable'方法不需要是靜態的。第二請發佈異常的logcat。 – techiServices 2011-05-07 02:29:48

+0

它會在幾個活動中被調用 - 我只是暫時把它扔到那裏。 thnx的幫助。 – CelticParser 2011-05-09 03:09:16