我正在制作一个笔记应用程序,当我点击Recyclerview中的任何项目时,我想开始一个新的活动并传递一些字符串,我甚至从stackoverflow引用了许多链接,我尝试了它们,仍然无法正常工作。你可以帮助我,因为我刚刚在android中开始。
这是我的NotesAdapter
import android.view.View;
/**
* Created by Sohail on 9/6/2017.
*/
public interface ItemClickListener {
void onClick(View view, int position, int i);
}
这是我用来实现onClick
的Itemclick监听器接口 package org.terna.notes_aap;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ListActivity extends AppCompatActivity implements ItemClickListener{
int c;
int get;
Note note;
PersonDB db1=new PersonDB(this,"persondb2.db",null,1);
private List<Note>noteList = new ArrayList<>();
private RecyclerView recyclerView, listrow;
private NotesAdapter notesAdapter;
ImageButton addNote,appIcon;
Button delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
notesAdapter = new NotesAdapter(noteList,this);
RecyclerView.LayoutManager nLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(nLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(notesAdapter);
notesAdapter.setClickListener(this);
final Intent intent1 = new Intent(this,Add_Update_Activity.class);
addNote = (ImageButton)(findViewById(R.id.addNote));
appIcon = (ImageButton)(findViewById(R.id.appIcon));
addNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent1.putExtra("check",1);
startActivity(intent1);
}
});
appIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
/* db1.insertIntoPerson("Sudhanshu's song","Rock","2017");
db1.insertIntoPerson("Akshay's song","Pop","2007");*/
prepareNoteData();
}
@Override
protected void onRestart() {
super.onRestart();
ArrayList<PersonModel> persons = db1.getAllPersons();
for(int i=persons.size()-1;i>=0;i--)
{
PersonModel person = persons.get(i);
String title = person.title;
int id = person.id;
String description = person.description;
String month = person.month;
String date = person.date;
String year = person.year;
note = new Note(id,title,description,month,date,year);
noteList.add(note);
}
notesAdapter.notifyDataSetChanged();
}
@Override
protected void onStop() {
super.onStop();
ArrayList<PersonModel> persons = db1.getAllPersons();
c = persons.size();
if(c !=0 ){
for(int j=persons.size()-1;j>=0;j--){
noteList.removeAll(persons);
}
}
noteList.clear();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void passdata(){
NotesAdapter n = null;
int get = n.getdata();
Toast.makeText(this, "get = "+get, Toast.LENGTH_SHORT).show();
}
private void prepareNoteData() {
Toast.makeText(this, "i am in preparenote", Toast.LENGTH_SHORT).show();
ArrayList<PersonModel> persons = db1.getAllPersons();
for(int i=persons.size()-1;i>=0;i--)
{
PersonModel person = persons.get(i);
String title = person.title;
int id = person.id;
String description = person.description;
String month = person.month;
String date = person.date;
String year = person.year;
Log.e("DB","Initialised");
note = new Note(id,title,description,month,date,year);
noteList.add(note);
}
notesAdapter.notifyDataSetChanged();
}
@Override
public void onClick(View view, int position, int i) {
int p = i;
Toast.makeText(this, i+" is clicked", Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(this,Add_Update_Activity.class);
startActivity(intent2);
}
}
这是我的ListActivity
package org.terna.notes_aap;
import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.icu.util.TimeZone;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
@RequiresApi(api = Build.VERSION_CODES.N)
public class Add_Update_Activity extends AppCompatActivity {
EditText title1, description1;
TextView label,month1,date1,year1;
String title,description,month,date,year;
int check;
PersonDB db1=new PersonDB(this,"persondb2.db",null,1);
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__update_);
title1 = (EditText)(findViewById(R.id.title1));
description1 = (EditText)(findViewById(R.id.description1));
label = (TextView) (findViewById(R.id.label));
month1 = (TextView) (findViewById(R.id.month));
date1 = (TextView) (findViewById(R.id.date));
year1 = (TextView) (findViewById(R.id.year));
check = getIntent().getIntExtra("check",0);
/*if(check == 1)
{
// Code for Creating new Note
*//*Formatter mon = new Formatter();
Calendar cal = Calendar.getInstance();
mon = new Formatter();
mon.format("%tb",cal);
Calendar calendar = Calendar.getInstance();
int y = calendar.get(Calendar.YEAR);
year = String.valueOf(y);
date = new SimpleDateFormat("EE", Locale.ENGLISH).format(cal.getTime());
month = mon.toString();
month = new SimpleDateFormat("MMM").format(new Date());
date = new SimpleDateFormat("dd").format(new Date());
year = new SimpleDateFormat("yyyy").format(new Date());
}
else
{
// Code for Updating a Note
}
*/
month = new SimpleDateFormat("MMM").format(new Date());
date = new SimpleDateFormat("dd").format(new Date());
year = new SimpleDateFormat("yyyy").format(new Date());
month1.setText(month);
date1.setText(date);
year1.setText(year);
Toast.makeText(this, "month"+month+" date"+date+" year"+year, Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this, "add activity restarted", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "add activity resumed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
super.onPause();
title = title1.getText().toString();
description = description1.getText().toString();
if(title.equals(""))
{
if(description.equals(""))
{
}
else
{
Toast.makeText(this, "desc inserted", Toast.LENGTH_SHORT).show();
db1.insertIntoPerson(title, description,month, date, year);
onBackPressed();
}
}
else
{
Toast.makeText(this, "title inserted", Toast.LENGTH_SHORT).show();
db1.insertIntoPerson(title, description,month, date, year);
onBackPressed();
}
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
这是我的Add_Update_Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.terna.notes_aap">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SplashActivity" />
<activity
android:name=".ListActivity"
android:label="@string/title_activity_list"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Add_Update_Activity"
android:label="@string/title_activity_add__update_"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Edit_Update_Activity"
android:label="@string/title_activity_edit__update_"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
这是我的清单文件
09-06 06:05:14.596 6726-6726/? E/FirebaseInstanceId: Failed to resolve target intent service, skipping classname enforcement
09-06 06:05:14.598 6726-6726/? E/FirebaseInstanceId: Error while delivering the message: ServiceIntent not found.
09-06 06:16:46.851 6726-6726/? E/FirebaseInstanceId: Failed to resolve target intent service, skipping classname enforcement
09-06 06:16:46.854 6726-6726/? E/FirebaseInstanceId: Error while delivering the message: ServiceIntent not found.
这一切都显示在我的日志中
{{1}}
答案 0 :(得分:0)
您已使用构造函数将上下文传递给适配器:
series_params
所以在onClickListener中你可以使用那个,不需要再次调用上下文,如下所示:
def new
@series = Series.new
#build objects of nested
@season = @series.seasons.build
@episod = @season.episods.build
end
def series_params
params.require(:series).permit(:title,
seasons_attributes: [:id, :title, episodes_attributes: [:id, :title]])
end
虽然你使用它的方式不应该导致问题,但是没有必要两次做同样的事情。
<强> SOLUTION:强>
在Add_Update_Activity中而不是
public NotesAdapter(List<Note> noteList, Context context) {
this.noteList = noteList;
this.context = context;
mInflater = LayoutInflater.from(context);
viewBinderHelper.setOpenOnlyOne(true);
}
使用
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(context1,Add_Update_Activity.class);
Toast.makeText(context1, "i am clicked", Toast.LENGTH_SHORT).show();
// When I comment it, my app works fine, I think something is missing here.
context.startActivity(i);
}
});
因为当你不发送任何额外内容时,适配器中的getExtras方法将返回null。