我目前正在处理我的应用程序的管理部分。我有一个数据库,我想在一个片段上显示 ListView 中的数据。但是,代码中没有错误,当我打开该特定片段(UserFragment)时,它只会注销/返回上一个活动。谁能告诉我如何实现这一目标?以下是我迄今为止尝试过的,
用户片段.java
这是我的 UserFragment (当我在应用程序中打开这个时,它会回到最后一个活动)
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class UserFragment extends Fragment {
dataconnect myDB;
ArrayList < String > items_user;
ArrayAdapter adapter;
ListView userList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_user, container, false);
items_user = new ArrayList < > ();
userList = (ListView) view.findViewById(R.id.listView_users);
viewData();
return view;
}
private void viewData() {
Cursor cursor = myDB.viewData();
if (cursor.getCount() == 0) {
Toast.makeText(getActivity(), "No data to show", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
items_user.add(cursor.getString(1));
}
adapter = new ArrayAdapter < > (getActivity(), android.R.layout.simple_list_item_1, items_user);
userList.setAdapter(adapter);
}
}
}
相关分类