Search code examples
androidviewflipperlayout-inflater

Android - Adding inflated views to viewflipper


first off I apologise if it's a dumb question as I'm a noob when it comes to Android and StackOverflow too...

I'm trying to read a twitter json feed and then push the various tweets into a ViewFlipper so it cycles horizontally between them...I just need the date (formatted) and the text of the tweet... I've already got the feed working and I can cycle between the tweets, my question is how to inject them into the viewflipper? Right now I'm inflating a layout xml and using addView to get them in there, which works, but I have no idea how to set the two textviews in the inflated view...

Is there another way perhaps that's easier?

Thanks in advance!

EDIT: Some source code

    ViewFlipper flipper = (ViewFlipper) findViewById(R.id.twitterFlipper);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and the for loop:

for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            flipper.addView(inflater.inflate(R.layout.twitter_item, flipper, false));
            Log.i("first", jsonObject.getString("text"));
        }

Solution

  • Try this:

    for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            View view = inflater.inflate(R.layout.twitter_item, flipper, false);
    
            TextView textView1 = view.findViewById(R.id.text1);
            TextView textView2 = view.findViewById(R.id.text2);
            textView1.setText("Some text 1");            
            textView2.setText("Some text 2");
    
            flipper.addView(view);
            Log.i("first", jsonObject.getString("text"));
        }
    

    Of course, replace the TextView resource id according to your layout.