2011-05-06 38 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

相关问题