出错
这是我从扩展ListActivity
复制的java我将ListActivity更改为Fragment和错误的代码,我添加getActivity()但ListView lv = getListView();是错的
你可以帮帮我吗?public class menuTIFragment extends Fragment {
private ProgressDialog pDialog;
protected ListView lv;
// buat json object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> classList;
// url untuk get semua buku tamu
private static String url_Class = Koneksi.DISPLAY_MATKULTI;
// JSON Node
private static final String TAG_SUCCESS = "success";
private static final String TAG_JUMLAH ="jumlah";
private static final String TAG_ID = "id_matkul";
private static final String TAG_NAMA = "nama_matkul";
// pendaftaran JSONArray
JSONArray jumlah = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.
menu,container,false);
classList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadClass().execute();
// Get listview
ListView lv = getListView();
ListView lv = (ListView) v.findViewById(R.id.listview);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String id_matkul = ((TextView) view.findViewById(R.id.idmatkul2)).getText().toString();
Intent in = new Intent(getActivity().getApplicationContext(), TabFragment.class);
in.putExtra(TAG_ID, id_matkul);
startActivityForResult(in, 100);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// jika kode hasil sama dengan 100
if (resultCode == 100) {
// maka diterima
// ketika user ngedit atau menghapus data
// reload screen
Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);
}
}
class LoadClass extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(menuTIFragment.this.getActivity());
pDialog.setMessage("Loading Data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* dapetkan semua produk dari get url di background
* */
protected String doInBackground(String... args) {
// Buat Parameter
List<NameValuePair> params = new ArrayList<NameValuePair>();
// ambil json dari url
JSONObject json = jParser.makeHttpRequest(url_Class, "GET", params);
// cek logcat untuk response dari json
Log.d("Semua Hasil: ", json.toString());
try {
// cek jika tag success
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// data ditemukan
// ambil array dari bukutamu
jumlah = json.getJSONArray(TAG_JUMLAH);
// tampilkan perulangan semua produk
for (int i = 0; i < jumlah.length(); i++) {
JSONObject c = jumlah.getJSONObject(i);
// simpan pada variabel
String id_matkul = c.getString(TAG_ID);
String nama_matkul = c.getString(TAG_NAMA);
// buat new hashmap
HashMap<String, String> map = new HashMap<String, String>();
// key => value
map.put(TAG_NAMA, nama_matkul);
map.put(TAG_ID, id_matkul);
// masukan HashList ke ArrayList
classList.add(map);
}
} else {
// jika tidak ada data
// maka jalankan tambahkan buku tamu
Intent i = new Intent(getActivity().getApplicationContext(),
HomeFragment.class);
// tutup semua proses sebelumnya
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* jika pekerjaan di belakang layar selesai maka hentikan progress dialog
* **/
protected void onPostExecute(String file_url) {
// hentikan progress ketika semua data didapat
pDialog.dismiss();
// perbarui screen
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ListAdapter adapter = new SimpleAdapter(
menuTIFragment.this.getActivity(), classList,
R.layout.menu2, new String[]{TAG_NAMA, TAG_ID},
new int[]{R.id.matkul2, R.id.idmatkul2});
// perbarui list pendaftaran
lv.setAdapter(adapter);
}
});
}
}}