我試圖將一些自定義列表適配器放到它們自己的類中,以使我的應用程序擁有更少的冗餘代碼並使其更易於管理。Android ListAdapter如何更正:無法對靜態方法進行靜態引用
我通過ListFragment中的條件語句調用不同的適配器類。我原本在ListFragment類中有適配器,並且這一切都按照計劃進行。現在要清理所有內容並將所有代碼從ListFragment中移出,我將適配器移出並放入他們自己的類中。因爲這是完成的,這些方法必須是靜態的,所以我可以叫上他們,但這些新的類現在containa很多:
不能使靜態參考來自非靜態方法 setListAdapter(ListAdapter) type ListFragment
具體來說就是setListAdapter,setListAdapter,getFragmentManager和getFragmentManager方法。我不需要大量的ListView Fragment類,並且由於很多代碼會被重用,它使得更多因爲只有一個ListFragment並使用條件來獲得正確的適配器,但我不知道如何修復這些新類所以我可以使用它們。
對不起,很長的解釋。我會盡量只發布相關的代碼,以便將想法告訴我想要完成的事情,併爲您提供指導。
這樣做可以這樣做,我該如何糾正它?
如果還有更好的方法,請發佈一些代碼,以便在適配器類中更改需要更改的代碼。
在我的片段的onActivityCreated:
. . .
// Get the string to query from last Fragment and pass it to this
// Fragment
Bundle args = this.getArguments();
boolean rawRes = args.getBoolean(KEY_IS_RAW_RES);
String url = args.getString(KEY_URL);
int fileName = args.getInt(KEY_RES_FILE);
this.getJsonFile(url, rawRes, fileName);
}
public void getJsonFile(String url, boolean rawRes, int fileName) {
if (rawRes == true) {
getFromRawRes(fileName);
} else {
getFromURL(url);
}
}
public void getFromRawRes(int fileName) {
InputStream file = getResources().openRawResource(fileName);
JSONParser jParser = new JSONParser();
JSONArray json = jParser.getJSONFromRes(file);
ListAdapter_SevenItem.callback(json, context);//<--THIS IS A CALL TO THE ADAPTER!!
}
其中一個適配器:
public class ListAdapter_SevenItem extends ListViewFragment {
. . .
public static void callback(JSONArray json, Context c) {
if (json != null) {
// Hashmap for ListView
. . .
// create the list item mapping
String[] from = new String[]{TAG_LABEL, TAG_TITLE, TAG_DISCR, TAG_RES_FILE, TAG_IS_RAW_RES, TAG_CONT_ID};
int[] to = new int[]{R.id.listLabel, R.id.listTitle, R.id.listDiscription, R.id.listResFile, R.id.listIsRawRes, R.id.listContID};
// Updating parsed JSON data into ListView
SimpleAdapter adapter = new SimpleAdapter(c, mList, R.layout.list_item, from, to);
setListAdapter(adapter);
// selecting single ListView item
final ListView lv = setListAdapter();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
MainActivity.mLayout.toggleSidebar();
setHasOptionsMenu(true);
FragmentManager fm = getFragmentManager();
final FragmentTransaction lcFT = fm.beginTransaction();
lcFT.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out);
final Bundle args = new Bundle();
String resFile = ((TextView) view.findViewById(R.id.listResFile)).getText().toString();
int passResFile = getFragmentManager().getIdentifier(resFile, "raw", "com.andaero.app");
args.putInt("KEY_RES_FILE", passResFile);
boolean isRawRes = true;
args.putBoolean("KEY_IS_RAW_RES", isRawRes);
// Delayed to improve animations
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
ListViewFragment lvf = new ListViewFragment();
lcFT.replace(R.id.listContainer, lvf).commit();
lvf.setArguments(args);
}
}, 300);
}
});
}
}
}
日Thnx的評論。有幾個數據源(大多數是JSON)從(本地,遠程等)填充,需要解析器和大量的捆綁參數傳遞。如果我可以使用本教程中實現的基本方法,那麼我會從字面上獲得數百個ListView片段,其中一半的代碼將在其中重複使用。它基本上是什麼:加載片段 - >確定數據來自哪個src - >確定需要什麼解析器 - >解析它 - >發送要映射到視圖 - >顯示列表視圖。我是新來的機器人,所以幫助表示讚賞! – CelticParser
另一點:'ListAdapter_SevenItem擴展ListViewFragment'看起來非常可疑。爲什麼'ListAdapter'擴展'Fragment'?這是兩個完全不同的概念。 –