我有一个片段,从另一个片段中检索名称标题,并且必须在此片段中显示详细信息。但我无法在Textview中显示数据。
这是我的 HistoryFragment
public class HistoryFragment extends Fragment {
public static final String TAG = "HISTORY_COUNTRY";
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
public static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public int countryIDs;
ArrayList<AsiaCountry> asiaList = new ArrayList<>();
AsiaCountry asiaCountry;
int position;
Bundle bundle;
String content;
private List<AsiaCountry> item = Collections.emptyList();
private TextView historyContent;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private HistoryOnFragmentInteractionListener mListener;
public HistoryFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
bundle = getActivity().getIntent().getExtras();
if (bundle != null) {
content = bundle.getString("name");
Log.d("content", content.toString());
}
fetchData();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_history, container, false);
historyContent = (TextView) view.findViewById(R.id.content);
historyContent.setText(asiaCountry.getHistory());
Log.d("His", historyContent.getText().toString());
return view;
}
private void fetchData() {
WorldCountry worldCountry = new WorldCountry(getActivity());
worldCountry.checkCopyDatabase();
worldCountry.openDataBase();
Log.d(ARG_PARAM1, "Database opened");
try {
Cursor cursor = worldCountry.QueryData("SELECT history FROM country WHERE name = ?" + content);
Log.d("CURSOR", cursor.toString());
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
AsiaCountry asia = new AsiaCountry();
asia.setHistory(cursor.getString(cursor.getColumnIndex("history")));
Log.d(ARG_PARAM2, cursor.getString(0));
asiaList.add(asia);
} while (cursor.moveToNext());
worldCountry.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
Log.d("TAG", e.getMessage());
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.historyOnFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof HistoryOnFragmentInteractionListener) {
mListener = (HistoryOnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface HistoryOnFragmentInteractionListener {
// TODO: Update argument type and name
void historyOnFragmentInteraction(Uri uri);
}
}
在上面的代码中,我从数据库中检索数据,我希望在Textview中显示,但我不能。
伙计们我认为我的冒犯是错误的,因为我没有检索历史记录列,因此,asiaList不会添加asia.setHistory(cursor.getString(0));也许我错了。我必须告诉光标检索历史列,其中name = content。
答案 0 :(得分:0)
asiaCountry没有内容。
historyContent.setText(asiaCountry.getHistory());
尝试通过提供所需的位置来获取列表中的内容。对于前。
historyContent.setText(asiaList.get(0).getHistory);
答案 1 :(得分:0)
试试这个:
if(asiaList != null && asiaList.size()>0){
for(int i=0;i<asiaList.size();i++){
historyContent.append(asiaList.get(i).getHistory()+"\n");
}
}
而不是
historyContent.setText(asiaCountry.getHistory());
在您的查询中尝试此操作
Cursor cursor = worldCountry.QueryData("SELECT history FROM country WHERE name = '"+content+"'");