I try to migrate from ListView to RecyclerView. And i have got error on setOnItemClickListener. An error say: Cannot resolve method 'setOnItemClickListener(anonymous android.widget.AdapterView.OnItemClickListener)' I have no idea with this error. Can anyone help me to migrating to recycler view?
Here is screenshot error in MainActivity.java
This is MainActivity.java code:
package com.example.rssreader; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import android.app.ProgressDialog; import com.google.android.gms.ads.AdLoader; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.formats.UnifiedNativeAd; import com.google.android.gms.ads.reward.RewardedVideoAd; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; public class MainActivity extends AppCompatActivity { RecyclerView myRss; ArrayList titles; ArrayList links; private AdView mAdView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the Google Mobile Ads SDK MobileAds.initialize(this, getString(R.string.admob_app_id)); // Admob mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); // タップイベント myRss = (RecyclerView) findViewById(R.id.myRss); titles = new ArrayList(); links = new ArrayList(); myRss.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Uri uri = Uri.parse(links.get(position)); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } }); new ProcessInBackground().execute(); } // ネットワーク接続 public InputStream getInputStream(URL url) { try{ return url.openConnection().getInputStream(); } catch (IOException e) { return null; } } // XML // https://blog.codecamp.jp/feed.xml public class ProcessInBackground extends AsyncTask { ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); Exception exception = null; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.setMessage("Loading Data"); progressDialog.show(); } @Override protected String doInBackground(Integer... integers) { try{ URL url = new URL("https://blog.codecamp.jp/feed.xml"); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(false); XmlPullParser xpp = factory.newPullParser(); xpp.setInput(getInputStream(url), "UTF_8"); boolean insideItem = false; int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { if (xpp.getName().equalsIgnoreCase("item")) { insideItem = true; } else if (xpp.getName().equalsIgnoreCase("title")) { if (insideItem) { titles.add(xpp.nextText()); } } else if (xpp.getName().equalsIgnoreCase("link")) { if (insideItem) { links.add(xpp.nextText()); } } } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) { insideItem = false; } eventType = xpp.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } // 読み込んだ XMLデータを画面出力 @Override protected void onPostExecute(String s) { super.onPostExecute(s); ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, titles); myRss.setAdapter(adapter); progressDialog.dismiss(); } } }
And this is app gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId 'com.example.rssreader'
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'com.pascalwelsch.arrayadapter:arrayadapter:1.2.0'
implementation 'com.google.firebase:firebase-ads:18.1.1'
implementation 'com.google.android.gms:play-services-ads:18.1.1'
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
}
apply plugin: 'com.google.gms.google-services'