我想显示一个ListView项目,代码来自我的活动,我想在我的所有片段中显示
我已经像这样创建了一个Json
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
//mTextMessage.setText(namapegawai);
selectedFragment = PegawaiFragment.newInstance();
break;
case R.id.navigation_dashboard:
//mTextMessage.setText(R.string.title_dashboard);
selectedFragment = MemberFragment.newInstance();
break;
//case R.id.navigation_notifications:
// mTextMessage.setText(R.string.title_notifications);
//break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pegawai);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
namapegawai = prefs.getString(MainActivity.UserName,MainActivity.UserName);
mTextMessage = findViewById(R.id.message);
StudentListView = findViewById(R.id.listview1);
progressBar = findViewById(R.id.progressBar);
StudentListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//Intent intentSemuaMemberLihat = new Intent(getActivity(),SemuaMemberLihat.class);
// Sending ListView clicked value using intent.
//intentSemuaMemberLihat.putExtra("ListViewValue", IdList.get(position).toString());
//startActivity(intentSemuaMemberLihat);
}
});
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
//Manually displaying the first fragment - one time only
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, PegawaiFragment.newInstance());
transaction.commit();
}
// JSON parse class started from here.
public class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
public Context context;
String JSonResult;
List<Member> studentList;
public GetHttpResponse(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0)
{
// Passing HTTP URL to HttpServicesClass Class.
HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl);
try
{
httpServicesClass.ExecutePostRequest();
if(httpServicesClass.getResponseCode() == 200)
{
JSonResult = httpServicesClass.getResponse();
if(JSonResult != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(JSonResult);
JSONObject jsonObject;
Member member;
studentList = new ArrayList<Member>();
for(int i=0; i<jsonArray.length(); i++)
{
member = new Member();
jsonObject = jsonArray.getJSONObject(i);
// Adding Student Id TO IdList Array.
IdList.add(jsonObject.getString("id").toString());
//Adding Student Name.
member.nama = jsonObject.getString("nama").toString();
member.nrp = jsonObject.getString("nrp").toString();
studentList.add(member);
Fragment fragment = PegawaiFragment.newInstance();
Bundle bundle = new Bundle();
bundle.putString("name", member.nama);
fragment.setArguments(bundle);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
progressBar.setVisibility(View.GONE);
StudentListView.setVisibility(View.VISIBLE);
if(studentList != null) {
ListAdapterClass adapter = new ListAdapterClass(studentList, context);
StudentListView.setAdapter(adapter);
}else
{
Toast.makeText(context, "Tidak ada data ditampilkan", Toast.LENGTH_SHORT).show();
}
}
}
在我的片段中用
调用它@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_member, container, false);
new PegawaiActivity.GetHttpResponse(getActivity()).execute();
return v;
}
并收到此错误
错误:(44,9)错误:需要包含PegawaiActivity.GetHttpResponse的封闭实例
围绕此代码
new PegawaiActivity.GetHttpResponse(getActivity()).execute();
任何人都知道如何解决这个问题? 已经在其他相关的搜索但仍然混淆了解它。 谢谢你的帮助
============更新
我简单的AsyncTask
public class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
public AsyncResponse delegate = null;
public interface AsyncResponse{
void onComplete(String output);
}
@Override
protected void onPostExecute(String result){
delegate.onComplete(result);
}
}
这里是片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_member, container, false);
getActivity().setTitle("Daftar Member");
setHasOptionsMenu(true);
StudentListView = v.findViewById(R.id.listview1);
progressBar = v.findViewById(R.id.progressBar);
PegawaiActivity a = new PegawaiActivity();
PegawaiActivity.GetHttpResponse mm = a.new GetHttpResponse(new PegawaiActivity.GetHttpResponse.AsyncResponse(){
@Override
public void onComplete(String output){
progressBar.setVisibility(View.GONE);
}
});
mm.execute();
return v;
}
但是给我错误
错误:(51,49)错误:类中的构造函数GetHttpResponses PegawaiActivity.GetHttpResponses不能应用于给定类型; 要求:找不到参数:原因:实际 和形式参数列表的长度不同
答案 0 :(得分:0)
你的第一个错误就是试图让你的内心课public static class GetHttpResponse
你的第二个错误告诉你,你在没有为一个构造函数定义构造函数的情况下将参数传递给Asynctask。
在我的所有片段中
对于初学者来说,你的Asynctask只是试图设置一个片段。
其次,您正在创建一个全新的,而不是在任何现有的ListView中设置ListView。
Fragment可以访问属于Activity的任何数据。
例如,尝试在Fragment中使用它
List<Member> studentList = ((PegawaiActivity) getActivity()).getStudents();
将学生列表字段从Asynctask中移出并移动到父Activity后,您可以创建一个getter方法。
您不一定需要将ListView数据传递给任何片段。但是,如果你这样做,请结帐How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
在活动中返回结果后,您将加载片段,就像您已经尝试过的那样
无论您选择哪种方式,都将在Fragments中构建适配器,并在Fragment布局的Listview上设置适配器,而不是现有的StudentListView
,这是Activity布局的一部分,因此您无法显示在每个片段中