2016-12-11 2 views
0

チャットリストビューを作成しようとしていますが、chatadatterのChatAdapter android.Context.context java.lang.stringのようなエラーcom.google.firebaseデータベースJava.util.arrayリストへのapplien。firebaseデータベースのデータをリストビューに表示していますが、Listadapterを作成するとエラーが発生します。

私は怒鳴るがフルコードである

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; 
    } 
} 
+0

yuoではなくhttps://firebaseui.com/docs/android/com/firebase/ui/FirebaseListAdapter FirebaseListAdapterを使用する必要があります.html – Opiatefuchs

+0

ありがとうございます。私は今チェックします。 – VyTcdc

+0

私はtreidですが、firebaseアダプターのエラーを解決できないと言います。 – VyTcdc

答えて

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); 
+0

ありがとうございます。 – VyTcdc

+0

これはFirebase SDKのバージョン2.xを使用しています。これでも動作しますが、Firebaseを初めて使用する開発者は新しいバージョンのSDK(9.xまたは10.x)を使用することをお勧めします。 –

+0

私はすでにそれを作成しているので私たち自身のリストビューアダプタで行うことはできません。あなたは私のコーディングチャットアダプタページを見ることができます。 – VyTcdc

関連する問題