gpt4 book ai didi

java - 将 fragment Activity 上下文传递给自定义 ListView 适配器

转载 作者:行者123 更新时间:2023-11-29 08:28:27 25 4
gpt4 key购买 nike

我有一个自定义 ListView 适配器设置,我需要在我的 fragment 中调用它,但我似乎无法将我的 Activity 传递给它。我有一个非常相似的设置,但是从一个 Activity 中调用,而且效果很好。所以,我认为问题在于将 Activity 上下文传递给适配器,但我不确定如何实现它。

下面是我的 fragment 的代码,我在其中调用我的自定义适配器:

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;


public class SiblingUnitFragment extends Fragment {

Context siblingContext = this.getActivity();

Activity context = this.getActivity();

ProgressDialog progressDialog;

ListView siblingLVAdapter;

String ReadOnly;
String LexaUser;
String Password;
String SearchValue;

String finalResultSiblings;
String HttpURLSiblings = "https://[myDataSpot]/getSiblings.php";
HashMap<String, String> hashMapSiblings = new HashMap<>();
HttpParse httpParse = new HttpParse();

String[] Uuid;
String[] Usize;
String[] Ustatus;

String SV;
String[] svSeparated;

ListView siblingListView;


public SiblingUnitFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_siblings, container, false);

siblingListView = (ListView) view.findViewById(R.id.SiblingsList);

return view;
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);



if (getArguments() != null) {
SV = getArguments().getString("SearchValue");
LexaUser = getArguments().getString("LexaUser");
}

if (SV.contains("-")) {

svSeparated = SV.split("-");
SearchValue = svSeparated[0];
getSiblings(SearchValue, LexaUser);

} else {
SearchValue = SV;
getSiblings(SearchValue, LexaUser);
}
}

public void getSiblings(String searchInput, String lexaUser) {

class SiblingsClass extends AsyncTask<String,Void,String> {

@Override
protected void onPreExecute() {
super.onPreExecute();

progressDialog = ProgressDialog.show(siblingContext, "Loading Data", null, true, true);
}

@Override
protected void onPostExecute(String httpResponseMsg) {

super.onPostExecute(httpResponseMsg);

if (httpResponseMsg != null) {

try {

JSONArray json = new JSONArray(httpResponseMsg);

Uuid = new String[json.length()];
Usize = new String[json.length()];
Ustatus = new String[json.length()];

for (int i = 0; i < json.length(); i++) {

JSONObject object = json.getJSONObject(i);

Uuid[i] = object.getString("id");
Usize[i] = object.getString("size");
Ustatus[i] = object.getString("status");
}

siblingLVAdapter = new SiblingsListViewAdapter(context, Uuid, Usize, Ustatus);
siblingListView.setAdapter(siblingLVAdapter);


} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
Toast.makeText(siblingContext, "Error: " + e.toString(), Toast.LENGTH_LONG).show();
} // catch (JSONException e)

progressDialog.dismiss();

} else {

progressDialog.dismiss();
Toast.makeText(siblingContext, "HttpResponseMsg is null.", Toast.LENGTH_LONG).show();
}
}

@Override
protected String doInBackground(String... params) {

hashMapSiblings.put("searchinput", params[0]);
hashMapSiblings.put("lexauser", params[1]);

finalResultSiblings = httpParse.postRequest(hashMapSiblings, HttpURLSiblings);

return finalResultSiblings;
}
}
SiblingsClass siblingsClass = new SiblingsClass();
siblingsClass.execute(searchInput, lexaUser);
}
}

这是我的适配器代码:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SiblingsListViewAdapter extends BaseAdapter {

Activity context;
String Uuid[];
String Usize[];
String Ustatus[];

public SiblingsListViewAdapter(Activity context, String[] Uuid, String[] Usize, String[] Ustatus) {
super();
this.context = context;
this.Uuid = Uuid;
this.Usize = Usize;
this.Ustatus = Ustatus;
}

public int getCount() {
// TODO Auto-generated method stub
return Uuid.length;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

private class ViewHolder {
TextView txtViewUid;
TextView txtViewSize;
TextView txtViewStatus;
}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();

if (convertView == null)
{
convertView = inflater.inflate(R.layout.siblings_list, null);
holder = new ViewHolder();
holder.txtViewUid = (TextView) convertView.findViewById(R.id.SiblingsUid);
holder.txtViewSize = (TextView) convertView.findViewById(R.id.SiblingsSize);
holder.txtViewStatus = (TextView) convertView.findViewById(R.id.SiblingsStatus);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}

holder.txtViewUid.setText(Uuid[position]);
holder.txtViewSize.setText(Usize[position]);
holder.txtViewStatus.setText(Ustatus[position]);

return convertView;
}
}

这是 logcat 错误:

error: incompatible types: SiblingsListViewAdapter cannot be converted to ListView

感谢所有帮助。谢谢!

最佳答案

根据 Fragment Lifecycle ,您需要仅在 fragment 生命周期的 onAttach 方法被调用后调用 getContext 否则没有关联的 Activity 因此没有上下文

所以像这样使用它

// declare context 
Context siblingContext ;

然后初始化

.. onCreateView(...){
siblingContext = getContext();
context = getActvity();
}

关于java - 将 fragment Activity 上下文传递给自定义 ListView 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50336866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com