I am developing android application that uses Firebase Database as back End. List View items retrieved from my Json key. For example:-
{
"Player3" : {
"LY" : "ewfw",
"Ty" : "rwerw"
}
}
Then, "LY" and "TY" will be loaded on As list View Item(which works fine). and after What i want is when "LY" or "TY" (List View Item) clicked "ewfw" or "rwerw" will be shown on detail acitivity. The problem is only Key is displayed on detail activity not Value. How can i load value for detail activity? Here is my code...
ListItemActivity.Java
public class ListItemActivity5 extends Fragment {
private static final String TAG = "Tab4Fragment";
//String[] values = new String[]{"", "", "", "", "", "", "", "", ""};
ListView listView;
TextView textView;
Array array;
RelativeLayout relativeLayout;
ViewGroup.LayoutParams layoutparams;
private DatabaseReference mDatabase;
RelativeLayout container;
AnimationDrawable anim;
Context context;
//private TextView text;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_conferance, container, false);
//text = (TextView) view.findViewById(R.id.textView1);
listView = (ListView) view.findViewById(R.id.list);
container = (RelativeLayout) view.findViewById(R.id.container5);
final TextView textView1 = (TextView) view.findViewById(R.id.textView1);
anim = (AnimationDrawable) container.getBackground();
anim.setEnterFadeDuration(2000);
anim.setExitFadeDuration(3000);
final ListView listView = (ListView) view.findViewById(R.id.list);
final ArrayList<String> friends = new ArrayList<String>();
mDatabase = FirebaseDatabase.getInstance().getReference("Player");
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
friends.clear();
for (final DataSnapshot player : dataSnapshot.getChildren()) {
Log.i("Player", player.getKey());
friends.add(player.getKey());
player.getValue();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
player.getValue();
//player2 = (String) String.valueOf(player.getValue());
//String topic = String.valueOf(player.getValue());
//topic = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(getContext(), Details.class);
intent.putExtra("key", String.valueOf(player.getValue()));
startActivity(intent);
}
});
}
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1,
friends) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
layoutparams = view.getLayoutParams();
TextView textView = (TextView) view.findViewById(android.R.id.text1);
/*YOUR CHOICE OF COLOR*/
textView.setTextColor(Color.WHITE);
textView.setTextSize(22);
//Define your height here.
//layoutparams.height = 240;
view.setLayoutParams(layoutparams);
return view;
}
};
listView.setAdapter(arrayAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
}
Details.Java
public class Details extends Activity {
RelativeLayout container;
AnimationDrawable anim;
ScrollView mScrollView;
TextView textView1;
private DatabaseReference mDatabase;
private ListItemActivity1 mylist1;
private DataSnapshot dataSnapshot;
FloatingActionButton floatingActionButton;
private ListItemActivity5 listItemActivity5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ab1);
textView1 = (TextView) findViewById(R.id.textView1);
//mref = new Firebase("https://tafach-love-2.firebaseio.com/");
Intent intent = getIntent();
if(intent.hasExtra("key")){
textView1.setText(intent.getStringExtra("key"));
}
textView1.setMovementMethod(new ScrollingMovementMethod());
}
}
Thanks for your help.
I have got answer for my question. I put these lines to my code on List ItemActivity and set onclickListner in ChildEventListner instead of AddEventLister...
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
final String playerName = dataSnapshot.getValue(String.class);
hy.add(playerName);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//player.getValue();
//player2 = (String) String.valueOf(player.getValue());
//String topic = String.valueOf(player.getValue());
//topic = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(getContext(), Details.class);
intent.putExtra("key", String.valueOf(hy.get(position)));
startActivity(intent);
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.