正如标题所说,我的ListFragment
没有显示从firebase检索的项目,这里是我的代码:
CONTAINER:
public class MainPageLogin extends AppCompatActivity {
private BottomBar mBottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page_login);
// mBottomBar = BottomBar.attach(this, savedInstanceState);
mBottomBar = BottomBar.attachShy((CoordinatorLayout) findViewById(R.id.myCoordinator),
findViewById(R.id.myScrollingContent), savedInstanceState);
mBottomBar.setFragmentItems(getSupportFragmentManager(), R.id.fragmentContainer,
new BottomBarFragment(ChatListFragment.newInstance(R.layout.chat_list_fragment,0), R.drawable.ic_action_user, "Chat"),
new BottomBarFragment(PollListFragment.newInstance(R.layout.poll_list_fragment,1), R.drawable.ic_assessment, "Sondaggi"),
new BottomBarFragment(ContactListFragment.newInstance(R.layout.contact_list_fragment,2), R.drawable.ic_contacts, "Contatti")
);
mBottomBar.setActiveTabColor("#C83F09");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Necessary to restore the BottomBar's state, otherwise we would
// lose the current tab on orientation change.
mBottomBar.onSaveInstanceState(outState);
}}
CONTAINER XML
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
android:id="@+id/myScrollingContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
LISTFRAGMENT JAVA
public class ChatListFragment extends ListFragment {
ListView usr_chats;
Button btn_crea;
StringBuilder total;
TextView nochat;
RequestQueue queue;
TextView chat_item;
FirebaseListAdapter<String> Fireadapter;
View v;
public ChatListFragment(){}
public static ChatListFragment newInstance(int res, int i){
ChatListFragment f = new ChatListFragment();
Bundle args = new Bundle();
args.putInt("res", res);
args.putInt("choice",i);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.chat_list_fragment, container, false);
nochat = (TextView) v.findViewById(R.id.no_chat);
btn_crea = (Button) v.findViewById(R.id.crea_chat);
usr_chats = (ListView) v.findViewById(android.R.id.list);
//-------------------------------
return v;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
total = new StringBuilder();
total.append("");
btn_crea.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(),contacts.class);
intent.putExtra("KEY",total.toString());
startActivity(intent);
}
});
Firebase.setAndroidContext(getContext());
try {
FileInputStream inputStream = getActivity().openFileInput("usr_basekey");
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
inputStream.close();
//Log.d("VOTA",total.toString());
} catch (FileNotFoundException e) {
Log.d("VOTA", "FILE NON TROVATO:" + e.getMessage());
} catch (IOException e) {
Log.d("VOTA", "IO EXCEPTION:" + e.getMessage());
}
Firebase myFirebaseRef = new Firebase("https://fiery-fire-7347.firebaseio.com/user/"+total.toString());
myFirebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
String user_params = snapshot.getValue().toString();
try {
JSONObject jsonO = new JSONObject(user_params);
String user_name = jsonO.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onCancelled(FirebaseError error) {
}
});
Firebase chats = myFirebaseRef.child("chatadded");
queue = Volley.newRequestQueue(getContext());
Log.d("VOTA","preListAdapter");
Fireadapter = new FirebaseListAdapter<String>(
getActivity(),
String.class,
R.layout.chat_user_list,
chats
) {
@Override
protected void populateView(View view, final String s, int i) {
chat_item = (TextView) view.findViewById(R.id.chat_name_for_users);
String url = "https://fiery-fire-7347.firebaseio.com/chats/"+s+"/nomeChat.json";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("VOTA","ResponseAdapter-"+response);
chat_item.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("VOTA", "Volley Error: " + error);
}
});
queue.add(stringRequest);
}
};
usr_chats.setAdapter(Fireadapter);
Log.d("VOTA", "postListAdapter");
usr_chats.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getContext(), ChatActivityHome.class);
intent.putExtra("nChat","");
intent.putExtra("myKey",total.toString());
intent.putExtra("kChat", Fireadapter.getItem(position));
startActivity(intent);
}
});
//-----------------------------
}}
LISTVIEW XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gitlab.votateam.votateam.MainPage">
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/crea_chat"
android:layout_alignParentTop="true" />
<TextView
android:textColor="#737373"
android:id="@+id/no_chat"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/android:list"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crea Chat"
android:id="@+id/crea_chat"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
ITEMS XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/imageView"
android:src="@drawable/def_groupchat_profile"
/>
<TextView
android:textColor="#737373"
android:text=""
android:id="@+id/chat_name_for_users"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="12dp" />
</RelativeLayout>
我确信代码有效,错误应该与XML有关,因为我的日志显示了来自firebase数据库的正确“响应”。
答案 0 :(得分:0)
您正在触发异步请求以获取字符串StringRequest
。当字符串返回时,Android不再期待对视图的更改,并且它不会更新视图。
解决方案通常是致电notifyDataSetChanged()
:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("VOTA","ResponseAdapter-"+response);
chat_item.setText(response);
}
}, ...