我正在创建一个应用程序,它会在点击每个网格视图项目时上传产品的价值和图像。
MainActivity.java
package com.example.intuition.ivposcloud;
import android.media.Image;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import org.w3c.dom.Text;
import static android.R.attr.name;
public class MainActivity extends AppCompatActivity {
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
GridView grid;
String[] web = {
"Pizza",
"Cheese",
"Cake",
"Icecream",
"Chocolate shake",
"coffee",
"Cake2",
"Yummy",
"Chicken",
"Tikka",
"pizza2",
"pizza3",
"Butter",
"Chocolate Cake",
"Nothing"
};
String[] web1 = {
"120",
"111",
"123",
"12",
"121",
"155",
"575",
"72",
"145",
"251",
"412",
"124",
"124",
"412",
"142"
};
int[] imageId = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
R.drawable.image9,
R.drawable.image10,
R.drawable.image11,
R.drawable.image12,
R.drawable.image13,
R.drawable.image14,
R.drawable.image15
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "Clicked position at " + position, Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "You Clicked at " + web[+position] + "price is " + web1[+position], Toast.LENGTH_SHORT).show();
// final ImageView image = (ImageView) findViewById(R.id.grid_image);
final TextView text = (TextView) findViewById(R.id.grid_text);
// String ImageView = image.getImageMatrix().toString().trim();
String TextView = text.getText().toString().trim();
Item item = new Item();
// myRef.child("Image").setValue(item);
myRef.child("Text").setValue(text);
//Value event listener for realtime data update
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
Item item = postSnapshot.getValue(Item.class);
//Adding it to a string
String string = "Item name: " + item.getText() + "\n Item Image: " + item.getImage() + "\n\n";
//Displaying it on textview
// textViewPersons.setText(string);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("Cannot Read Data" + DatabaseError.UNKNOWN_ERROR);
}
});
}
});
}
}
这是 - Item.java类
package com.example.intuition.ivposcloud;
import static android.R.attr.name;
/**
* Created by intuition on 14-12-2016.
*/
public class Item {
//name and address string
private String image;
private String text;
public Item() {
/*Blank default constructor essential for Firebase*/
}
//Getters and setters
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
这是我运行应用程序后收到的LogCat致命错误。
logcat的:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.intuition.ivposcloud, PID: 16884
com.google.firebase.database.DatabaseException: Found conflicting getters for name: isImportantForAccessibility
at com.google.android.gms.internal.zzbqi$zza.<init>(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzi(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzax(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzaw(Unknown Source)
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
at com.example.intuition.ivposcloud.MainActivity$1.onItemClick(MainActivity.java:112)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
at android.widget.AbsListView$3.run(AbsListView.java:3865)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:7)
这不起作用:
final TextView text = (TextView) findViewById(R.id.grid_text);
myRef.child("Text").setValue(text);
Firebase数据库只能存储JSON类型,因此您无法将TextView
传递给它。
您可能希望将文本视图中的值存储在数据库中,您可以这样做:
final TextView text = (TextView) findViewById(R.id.grid_text);
myRef.child("Text").setValue(text.getText().toString());