How can I reuse methods for ListViews?

I have several methods in ListAC ListActivity that I want to reuse, and would like to add separate classes (if possible?). I will have dozens of ListView activities (ListActivities, that is: ListAD, ListTCDS, listSFAR, etc. Etc.), Which will call these methods, which are exactly the same in this action, but the only changes to the "Cursor databaseCursor "(i.e., the table name for each ListActivity). I plan to put these classes and associate them with my own packages with the application (i.e.: com.myCompany.myApp.AC). In addition, during these actions, the query "ORDER BY" must have several cursors (ie: "..." list "ASC | DESC", "..." name "ASC | DESC", etc. ) B4 cursors are called, I have an ExternalStorageState validation method. I was provided with the StorageStateChecker class (see below) from LeffelMania (THNX!) To reuse ExternalStorageState, and I would like to use it also - even if it is not implemented in the code below. I decided to step back * to rethink this (I screwed up - LOL) and make it easier for you guys and girls to read through the code.

As you can see, this will lead to a lot of unnecessary redundancy and byte space on the device. I need to compress every bit of space on devices, because this application will be the only one goal for users of these devices (if the project moves forward - LOL).

So, I need a way to shorten it by calling these methods through separate classes, if at all possible, with the way I wrote these methods and my adapters. Any help w / suggestions, methods, methods and code would be very appreciated and helpful to me while learning Java / Android. Thnx !!

* This message is linked and continues from HERE and HERE .

UPDATE: the completed class is listed in the REVISED block. Thnx helped everyone, and now I better understand the classes and the use of methods.

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 ==================================== **/ } 

My adapter (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; } } 

REVISED:

 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; } } } 

... and how to call and enter the query:

 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); } 
+1
android methods class listview adapter
source share
2 answers

It seems that you are doing an extraordinary amount of extra work to give a bunch of different query options for your cursor.

Why not have one ListActivity with one CursorAdapter and just put the query in the intent when you want to start the Activity?

I will be working on a small sample code and post it here as an edit, but you really think too much about it. All you are trying to do is display the query results in a ListView. The only thing that changes is the request. Reuse everything else.

Code example:

 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; } } } 

This is literally all the code you need (plus padding for your custom views and plotting the cursor). Whenever you need to modify a query, you just need to call reloadQuery(String query) and you will install.

+2
source share

Are there any similarities in the code of the extended SimpleCursorAdapter ? As for the code, such as obtaining the external state of the repository, you can implement a class called, for example, Utils , and make static methods in it. You will most likely need a Context or Activity passed as a parameter.

For example, this is my external storage status check.

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

And my network status check.

 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
source share

All Articles