我正在开发使用Firebase数据库作为后端的Android应用程序。列表查看从我的Json密钥检索的项目。例如: -
{
"Player3" : {
"LY" : "ewfw",
"Ty" : "rwerw"
}
}
然后," LY"和" TY"将被加载在As列表中查看项目(工作正常)。在我想要的是什么时候" LY"或" TY" (列表查看项目)点击" ewfw"或者" rwerw"将在细节活动上显示。问题是只有Key显示在详细活动而非Value。如何为详细活动加载价值? 这是我的代码......
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());
}
}
感谢您的帮助。
答案 0 :(得分:0)
我的问题得到了答案。我将这些行放在List ItemActivity上的代码中,并在ChildEventListner中设置onclickListner而不是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);
}
});
}