我想将firebase数据库中的数据显示到listview,但是在创建Listadapter时会出现错误

时间:2016-12-11 08:19:42

标签: java android firebase listadapter firebaseui

我正在尝试创建一个聊天列表视图,但是在chatdatpter中收到像Chatadapter android.Context.context java.lang.string这样的错误,不能用于匿名的com.google.firebase数据库Java.util.array列表。

我收到错误信息

ListAdapter theAdapter = new Chatadapter(this,message);

下面是完整的代码。

public class Chatactivity extends AppCompatActivity {


    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference();

    Button buttonSend;
    EditText chatText;
    ListView L;
    String username;
    String sendid;
    int Request_id;
    UserLocalStore userLocalStore;
    SharedPreferences sharedPreferences;


    ArrayList<String> message = new ArrayList<>();
    Messageproperties messageproperties = new Messageproperties();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_activity);
        buttonSend = (Button) findViewById(R.id.buttonSend);
        L = (ListView)findViewById(R.id.listView1);
        //Add Data
        //String[] days={"Egg Benedict", "Mushroom Risotto", "Full Breakfast", "Hamburger", "Ham and Egg Sandwich", "Creme Brelee", "White Chocolate Donut", "Starbucks Coffee", "Vegetable Curry", "Instant Noodle with Egg", "Noodle with BBQ Pork", "Japanese Noodle with Pork", "Green Tea", "Thai Shrimp Cake", "Angry Birds Cake", "Ham and Cheese Panini"};

        userLocalStore = new UserLocalStore(this);
        sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);

              this.RetriveData();

        });

        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                System.out.println("Send Button Pressed");
                chatText = (EditText) findViewById(R.id.chatText);
                String message = (String) chatText.getText().toString();
                username = userLocalStore.getname();
                sendid = userLocalStore.getToken();
                String req_id = sharedPreferences.getString("requestid", "");                        
                System.out.println("Username====>" + username);
                messageproperties.setDate(formattedDate);
                messageproperties.setMessage(message);
                messageproperties.setSender_id(sendid);
                messageproperties.setSenderDisplayName(username);
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference();
                myRef.child(req_id).push().setValue(messageproperties);
                ((EditText) findViewById(R.id.chatText)).getText().clear();

            }
        });


    }

       //Retrive Data
    private void RetriveData()
    {

         String req_id = sharedPreferences.getString("requestid", "");
       // System.out.println("Now Getting Nsuser Default====>" + req_id);
        myRef.child(req_id).addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {

                Messageproperties newPost = dataSnapshot.getValue(Messageproperties.class);
                message.add(newPost.getMessage());

               // ArrayAdapter adapter = new ArrayAdapter(Chatactivity.this, android.R.layout.simple_list_item_1, message);
              //  L.setAdapter(adapter);

                ListAdapter theAdapter = new Chatadapter(this,message);
                ListView theListview = (ListView)findViewById(R.id.listView1);
                theListview.setAdapter(theAdapter);


            }
            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

    }
}


//This is Chat Addatter page

package com.app.secretcupid;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by vysimac on 11/12/16.
 */
public class Chatadapter extends ArrayAdapter<String> {

    public Chatadapter(Context context, String[] Values) {
        super(context,R.layout.row_adapter_layout ,Values);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater theInflater = LayoutInflater.from(getContext());
        View theView = theInflater.inflate(R.layout.row_adapter_layout, parent, false);
        String tvShow = getItem(position);
        TextView theTextView = (TextView) theView.findViewById(R.id.textview1);
        theTextView.setText(tvShow);
        theTextView.setBackgroundResource(false ? R.drawable.bubble1 : R.drawable.bubble2);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.LEFT;
        theTextView.setLayoutParams(params);
        return theView;
    }
}

1 个答案:

答案 0 :(得分:0)

我将举例说明我的应用有效:

Firebase.setAndroidContext(context);
Firebase ref = new Firebase("link here to your firbase");
        Firebase productsRef = ref.child("Products");//only if you have child
        FirebaseListAdapter<Product> fireAdapter = new FirebaseListAdapter<Product>(
                getActivity(),
                Product.class, R.layout.row_main,
                productsRef) {
            @Override
            protected void populateView(View view,final Product product, final int i) {

                textName = (TextView) view.findViewById(R.id.textName);
                textOverview = (TextView) view.findViewById(R.id.textOverView);
                textPrice = (TextView) view.findViewById(R.id.textPrice);
                productImage = (ImageView) view.findViewById(R.id.imageView);
                buttonAdd= view.findViewById(R.id.buttonAdd);
                products.add(product);
                textName.setText(product.getName().toString());
                textOverview.setText(product.getOverview().toString());
                textPrice.setText(String.valueOf(product.getPrice() + "₪"));
                Picasso.with(getActivity())
                        .load(product.getImageURL())
                        .into(productImage);



                buttonAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        FragmentManager fragmentManager = getFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.content_frame, new ProdcutFragment()).addToBackStack("tag").commit();
                        MainActivity.homeButton.setVisibility(View.VISIBLE);

                        ProdcutFragment.sendProdcut(product);
                    }
                });




            }
        };
        listView.setAdapter(fireAdapter);