How to import RecyclerView for Android L-preview

前端 未结 22 1548
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 14:55

Trying to use the new RecyclerView from the support library. I downloaded the 20 update for the support library using the SDK manager.

I\'ve added the jar file to th

相关标签:
22条回答
  • 2020-12-02 15:45

    This works for me

    Define internet permission

     <uses-permission android:name="android.permission.INTERNET" >
    

    Add dependency

    compile 'com.squareup.retrofit2:retrofit:2.1.0'
        compile 'com.google.code.gson:gson:2.6.2'
        compile 'com.squareup.retrofit2:converter-gson:2.1.0'
        compile 'com.google.code.gson:gson:2.6.2'
        compile 'com.squareup.retrofit2:converter-gson:2.1.0'
        compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
        compile 'com.squareup.okhttp3:okhttp:3.4.1'
        compile 'com.squareup.retrofit2:retrofit:2.1.0'
        compile ('com.squareup.retrofit2:converter-simplexml:2.1.0'){
            exclude group: 'stax', module: 'stax-api'
            exclude group: 'stax', module: 'stax'
            exclude group: 'xpp3', module: 'xpp3'
        }
    

    In Main Activity

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.util.List;
    
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    import retrofit2.Retrofit;
    import retrofit2.converter.simplexml.SimpleXmlConverterFactory;
    
    public class MainActivity extends AppCompatActivity {
        private BreakfastMenu breakfastMenu;
        List<BreakfastMenu> list;
        TextView responseText;
        APIInterface apiInterface;
        String name;
        String price;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            responseText=(TextView)findViewById(R.id.reponseText);
            apiInterface = APIClient.getClient().create(APIInterface.class);
    
    
            /**
             GET List Resources
             **/
            Call<BreakfastMenu> call = apiInterface.getBreakfastMenu();
            call.enqueue(new Callback<BreakfastMenu>() {
                @Override
                public void onResponse(Call<BreakfastMenu> call, Response<BreakfastMenu> response) {
                   Log.d("TAG", response.code() + "");
    
                    String displayResponse = "";
                    BreakfastMenu resource = response.body();
                    System.out.println(displayResponse+"display response   ");
    
                    for (Food food : resource.getFoodList())
                    {
                        name=food.getName();
                        price=food.getPrice();
                        System.out.println(name+price+"=========================================");
                        displayResponse += food.getName() + " " + food.getPrice()+"\n"+"\n";
                        Toast.makeText(MainActivity.this,name+price,Toast.LENGTH_LONG).show();
                    }
                    responseText.setText(displayResponse);
    
                }
    
                @Override
                public void onFailure(Call<BreakfastMenu> call, Throwable t) {
                    call.cancel();
                }
            });
    
        }
    }
    

    Make APIClient.java class

    import okhttp3.OkHttpClient;
    import okhttp3.logging.HttpLoggingInterceptor;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;
    import retrofit2.converter.simplexml.SimpleXmlConverterFactory;
    
    class APIClient {
    
        private static Retrofit retrofit = null;
    
        static Retrofit getClient() {
    
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
    
    
            retrofit = new Retrofit.Builder()
                    .baseUrl("https://www.w3schools.com/")
                    .addConverterFactory(SimpleXmlConverterFactory.create())
                    .build();
    
    
    
            return retrofit;
        }
    
    }
    
        enter code here
    
    Make APIInterface.java
    
    import retrofit2.Call;
    import retrofit2.http.Body;
    import retrofit2.http.Field;
    import retrofit2.http.FormUrlEncoded;
    import retrofit2.http.GET;
    import retrofit2.http.Headers;
    import retrofit2.http.POST;
    import retrofit2.http.Query;
    
    interface APIInterface {
    
        @GET("xml/simple.xml")
        @Headers({"Accept: application/xml",
                "User-Agent: Retrofit-Sample-App"})
        Call<BreakfastMenu> getBreakfastMenu();
    }
    
    In BreakfastMenu.java
    
    import org.simpleframework.xml.ElementList;
    import org.simpleframework.xml.Root;
    
    import java.util.List;
    
    @Root(name = "breakfast_menu")
    public class BreakfastMenu
    {
    
        @ElementList(inline = true)
        private List<Food> foodList;
    
        public BreakfastMenu()
        {
        }
    
        public List<Food> getFoodList()
        {
            return foodList;
        }
    
        public void setFoodList(List<Food> foodList)
        {
            this.foodList = foodList;
        }
    }
    

    Make Food.java

    import org.simpleframework.xml.Element;
    import org.simpleframework.xml.Root;
    
    @Root(name = "food")
    public class Food
    {
    
        @Element(name = "name")
        private String name;
    
        @Element(name = "price")
        private String price;
    
        @Element(name = "description")
        private String description;
    
        @Element(name = "calories")
        private String calories;
    
        public Food()
        {
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public String getPrice()
        {
            return price;
        }
    
        public void setPrice(String price)
        {
            this.price = price;
        }
    
        public String getDescription()
        {
            return description;
        }
    
        public void setDescription(String description)
        {
            this.description = description;
        }
    
        public String getCalories()
        {
            return calories;
        }
    
        public void setCalories(String calories)
        {
            this.calories = calories;
        }
    }
    

    In activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
            <TextView
                android:id="@+id/reponseText"
                android:layout_width="match_parent"
                android:layout_height="600dp"
              />
    
    
    </Linear Layout>
    
    0 讨论(0)
  • 2020-12-02 15:46

    If You have Compiled SDK Version 22.2.0 then add below dependency for recycler view and cardview additional for support of cardView

    // for including all the libarary in the directory lib
    compile fileTree(include: ['*.jar'], dir: 'libs')
    // for support appcompat
    compile 'com.android.support:appcompat-v7:22.2.0'
    //for including google support design (it makes possible of implementing material design theme from 2.3 and higher)
    `compile 'com.android.support:design:22.2.0'

    for adding the recycler view use following dependency
    compile 'com.android.support:recyclerview-v7:22.2.0'


    After that click on Build->rebuild project and you are done.

    0 讨论(0)
  • 2020-12-02 15:48

    If anyone still has this issue - you don't have to change compileSdkVersion, this just defeats the whole purpose of support libraries.

    Instead, use these in your gradle.build file:

    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:palette-v7:+'`
    
    0 讨论(0)
  • 2020-12-02 15:49

    This works for me:

    compile 'com.android.support:recyclerview-v7:21.0.0-rc1'
    
    0 讨论(0)
  • 2020-12-02 15:50

    I used a small hack to use the RecyclerView on older devices. I just went into my local m2 repository and picked up the RecyclerView source files and put them into my project.

    You can find the sourcecode here:

    <Android-SDK>\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0-rc1\recyclerview-v7-21.0.0-rc1-sources.jar

    0 讨论(0)
  • 2020-12-02 15:52
    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    }
    

    Just make your dependencies like above in build.gradle file, worked for me.

    0 讨论(0)
提交回复
热议问题