RecyclerView does not display any data - android

I've been struggling with this issue for a while. I have an activity (SerieActivity) where I allow the user to select various items and give them values. The items are stored in an ArrayList<Variable>, where Variable is a class that I've defined. The items are displayed in a RecyclerView. This works just fine. Upon clicking a button in that activity, the user is sent to another activity (Result) that displays the exact same items from the ArrayList in SerieActivity, with the respective values that the user has input.
In my Result activity I have created an instance of the SerieActivity class and then made an ArrayList to store the values of the original ArrayList from SerieActivity. However it seems like the RecyclerView from my Result class doesn't get populated with data. What could be going wrong?
Here's my code:
Variable class
public class Variable {
boolean known;
String unit, name;
double value;
}
RVAdapterSerie - the adapter for the RecyclerView used in the SerieActivity class
public class RVAdapterSerie extends RecyclerView.Adapter<RVAdapterSerie.ViewHolder> {
private Context context;
private ArrayList<Variable> variableList;
public RVAdapterSerie(Context context, ArrayList<Variable> variableList) {
this.context = context;
this.variableList = variableList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(RVAdapterSerie.ViewHolder holder, int position) {
Variable v = variableList.get(position);
holder.setItems(v);
}
#Override
public int getItemCount() {
return variableList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CheckBox checkBox;
private EditText editText;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.value_cb);
editText = itemView.findViewById(R.id.value_et);
textView = itemView.findViewById(R.id.value_tv);
}
public void setItems (Variable variable) {
checkBox.setText(variable.getName());
editText.setText(String.format("%.3f", variable.getValue()));
textView.setText(variable.getUnit());
}
}
}
SerieActivity class
public class SerieActivity extends AppCompatActivity {
public ArrayList<Variable> mList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_serie);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = findViewById(R.id.serie_rv);
RVAdapterSerie rvAdapterSerie = new RVAdapterSerie(this, mList);
recyclerView.setAdapter(rvAdapterSerie);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
populateList();
rvAdapterSerie.notifyDataSetChanged();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.serie_fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SerieActivity.this, Result.class);
intent.putExtra("tag", "serie");
startActivity(intent);
}
});
}
public void populateList() {
mList.add(new Variable(false, "dummyunit", "dummyname", 0));
mList.add(new Variable(false, "diofishafuio", "ghfuef", 0));
mList.add(new Variable(false, "diofishdfesffafuio", "ghfuef", 0));
//add dummy items
}
}
RVResultAdapter - adapter for the RecyclerView in Result class
public class RVResultAdapter extends RecyclerView.Adapter<RVResultAdapter.ViewHolder> {
private Context context;
private ArrayList<Variable> variableList;
public RVResultAdapter(Context context, ArrayList<Variable> variableList) {
this.context = context;
this.variableList = variableList;
}
#Override
public RVResultAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.result_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(RVResultAdapter.ViewHolder holder, int position) {
Variable v = variableList.get(position);
holder.setItems(v);
}
#Override
public int getItemCount() {
return variableList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView1;
TextView textView2;
TextView textView3;
public ViewHolder(View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.result_name_tv);
textView2 = itemView.findViewById(R.id.result_value_tv);
textView3 = itemView.findViewById(R.id.result_unit_tv);
}
public void setItems (Variable variable) {
textView1.setText(variable.getName());
textView2.setText(String.format("%.3f", variable.getValue()));
textView3.setText(variable.getUnit());
}
}
}
Result class
public class Result extends AppCompatActivity {
public ArrayList<Variable> rList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent = getIntent();
String tag = intent.getExtras().getString("tag");
if (tag.equals("serie")) {
SerieActivity serieActivity = new SerieActivity();
rList = serieActivity.mList;
}
RecyclerView recyclerView = findViewById(R.id.result_rv);
RVResultAdapter rvResultAdapter = new RVResultAdapter(this, rList);
recyclerView.setAdapter(rvResultAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
rvResultAdapter.notifyDataSetChanged();
}
}
My problem is with the RecyclerView in Result. It shows the blue "end of content" things when I scroll up and down, so I guess the RecyclerView is there, but doesn't get any data. Any help is greatly appreciated. I am quite new to Android and Java programming, so I guess it must be a dumb thing that I just don't see.

What are you doing dude. How can you get list from a new object of SerieActivity.class?
if (tag.equals("serie")) {
SerieActivity serieActivity = new SerieActivity();
rList = serieActivity.mList;
}
This is totally wrong way. If you want to pass list between activities. You can use parcelable methods putExtra and getExtra.
Assuming that your List is a list of strings make data an ArrayList<String> and use intent.putStringArrayListExtra("data", data)
Here is a skeleton of the code you need:
First you need to create a Parcelable object class, see the example
public class Student implements Parcelable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int arg1) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
}
public Student(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
}
And the list
ArrayList<Student> arraylist = new ArrayList<Student>();
Code from Calling activity
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);
this.startActivity(intent);
Code on called activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle = getIntent().getExtras();
ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}

So you calling SerieActivity constructor in Results activity and execute populateList() inside onCreate(). You shouldn't create activities in this way because onCreate() will be never called. Instead of this you should pass data using putExtra and use Parcelable interface.

just set the recyclerview code like this first initialize adapter then set layout manager and then set to adapter to recyclerview.
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new MoviesAdapter(movieList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareMovieData();

Related

Adding item dynamically to recyclerview

I am building a notepad app. I am trying to add another item to the recyclerview as soon as the new note is saved by the user.
public class NotePad extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_pad);
final FloatingActionButton button =
(FloatingActionButton)findViewById(R.id.save_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SaveData();
}
});
}
// To Send Note Title to MainActivity
public void SendData() {
EditText editText = findViewById(R.id.textView3);
final String str = editText.getText().toString();
Log.d("Check","str:"+str);
Intent intent = new Intent(NotePad.this,MainActivity.class);
intent.putExtra(EXTRA_MESSAGE, str);
startActivity(intent);
}
// To Save Note Content
public void SaveData() {
FileOutputStream outputStream;
try {
EditText editText = findViewById(R.id.textView3);
final String str = editText.getText().toString();
String content = findViewById(R.id.textView).toString();
Log.d("Test","inside try block");
outputStream = openFileOutput(str, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
Log.d("Test","save done");
SendData();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving a note and sending the title(str) that is to be displayed in recyclerview row to MainActivity as shown above.
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
ArrayList<String> notes = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String title = intent.getStringExtra(NotePad.EXTRA_MESSAGE);
Log.d("Check","title added"+title);
notes.add(title);
BuildRecyclerView();
}
public void BuildRecyclerView() {
recyclerView = (RecyclerView)findViewById(R.id.recycle_list);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
mAdapter = new MyAdapter(notes);
recyclerView.setAdapter(mAdapter);
}
But whenever I add a new note it overwrites the first entry in the recyclerview instead of occupying the next row of the recyclerview. This is my adapter code:
public class MyAdapter extends
RecyclerView.Adapter<MyAdapter.MyViewHolder> {
ArrayList<String> Notes_list = new ArrayList<String>();
public MyAdapter(ArrayList<String> notes) {
Notes_list = notes;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int
viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_items,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int
position) {
holder.mTextView.setText(Notes_list.get(position));
}
#Override
public int getItemCount() {
return Notes_list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.view_holder);
}
}
}
Because you pass last item to MainActivity and generate a new RecyclerView with passed item. You have to keep notes array on first class and pass whole array to MainActivity with intent.

How to send data from adapter to activity and display it in a recyclerview?

I try to pass data from an adapter to an activity and display it in a recyclerview. I am getting the NullPointerException when I run the app. What am I doing wrong?
This is the activity where I want to display data:
public class ListOfChatsActivity extends AppCompatActivity {
Toolbar lToolbar;
RecyclerView lRV;
BottomNavigationView lBnv;
ListOfChatsAdapter adapter;
LinearLayoutManager layoutManager;
ArrayList<String> chatList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_chats);
lToolbar = findViewById(R.id.lToolbar);
lRV = findViewById(R.id.lRV);
lBnv = findViewById(R.id.lBnv);
Intent intent = this.getIntent();
if(intent!=null){
String string = intent.getStringExtra("username");
chatList.add(string);}
adapter = new ListOfChatsAdapter(chatList, getApplicationContext());
layoutManager = new LinearLayoutManager(getApplicationContext());
lRV.setAdapter(adapter);
lRV.setLayoutManager(layoutManager);
}
This is the adapter from where I want to send data:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
ArrayList<User> friendsSearchedList = new ArrayList<>();
Context mContext;
public SearchAdapter(ArrayList<User> friendsSearchedList, Context mContext){
this.friendsSearchedList = friendsSearchedList;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_user_item,
parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final User username = friendsSearchedList.get(position);
holder.text.setText(username.getUsername());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String iUsername = holder.text.getText().toString();
Intent intent = new Intent();
intent.putExtra("username", iUsername);
}
});
}
#Override
public int getItemCount() {
return friendsSearchedList == null ? 0:friendsSearchedList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text;
Button button;
RelativeLayout relativeLayout;
public ViewHolder(View itemView){
super(itemView);
text = itemView.findViewById(R.id.sText);
button = itemView.findViewById(R.id.addFriend);
relativeLayout = itemView.findViewById(R.id.relativeLayout);
}
}
}
I don't open the Activity from this Adapter. I just want to send extras. I tried more options, but I don't get it right.
Change 1
adapter = new ListOfChatsAdapter(chatList, getApplicationContext());
Why is the ListOfChatsAdapter being used here? You have included SearchAdapter as the adapter to use in the RecyclerView.
Change 2
The ViewHolder object that is returned by onCreateViewHolder(), should be of type SearchAdapter.ViewHolder and not just ViewHolder. The same applies to the first arguement of onBindViewHolder().
Try these and check

How can i get the all value of Recyclerview on Button click?

public class MainActivity extends Activity implements
GetDataContract.View,RecyclerItemClickListener {
private Presenter mPresenter;
RecyclerView recyclerView;
LinearLayoutManager linearLayoutManager;
CountryAdapter countryAdapter;
EditText etEnterName;
Button btAddItem;
List<CountryRes> allCountriesEditValue;
List<CountryRes> allCountriesData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getView();
}
private void getView() {
/*presentator */
mPresenter = new Presenter(this);
/*initiliaze of id*/
recyclerView = (RecyclerView) findViewById(R.id.recycler);
etEnterName = (EditText) findViewById(R.id.etEnterName);
btAddItem = (Button) findViewById(R.id.btAddItem);
linearLayoutManager = new LinearLayoutManager(this);
/*initiliaze the arraylist*/
allCountriesData=new ArrayList<>();
recyclerView.setLayoutManager(linearLayoutManager);
btAddItem.setOnClickListener(new AddButtonClick());
}
#Override
public void onGetDataFailure(String message) {
Log.d("Status", message);
}
#Override
public void onGetDataSuccess(String message, List<CountryRes>
allCountriesData) {
/*add the value mannulay*/
CountryRes countryRes = new CountryRes();
countryRes.setName(etEnterName.getText().toString());
allCountriesData.add(countryRes);
countryAdapter = new CountryAdapter(getApplicationContext(),
allCountriesData, (RecyclerItemClickListener) this);
recyclerView.setAdapter(countryAdapter);
/*set the data in the room*/
AppDataBase database = AppDataBase.getAppDatabase(this);
DataGenerator.with(database).generateCats(allCountriesData);
Logger.displayCatsInLog(database.catDao().loadAll());
countryAdapter.notifyDataSetChanged();
}
/*on item click*/
#Override
public void onDashBoardItemClick(String pos) {
Bundle bundle = new Bundle();
bundle.putString("TABVALUE", pos);
Intent intent = new Intent(getApplicationContext(),
DynamicTabsActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
private class AddButtonClick implements View.OnClickListener {
#Override
public void onClick(View view) {
mPresenter.getDataFromURL(getApplicationContext(), "");
}
}
}
class adapter
public class CountryAdapter extends
RecyclerView.Adapter<CountryAdapter.MyViewHolder> {
private Context context;
private List<CountryRes> list = new ArrayList<>();
private List<CountryRes> list_edit = new ArrayList<>();
private RecyclerItemClickListener onRecyclerItemClickListener;
public CountryAdapter(Context context, List<CountryRes> list,
RecyclerItemClickListener onRecyclerItemClickListener) {
this.context = context;
this.list = list;
this.onRecyclerItemClickListener = onRecyclerItemClickListener;
}
#Override
public CountryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View layoutView;
layoutView =
LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent,
false);
return new FooterViewHolder(layoutView,
onRecyclerItemClickListener);
}
#Override
public void onBindViewHolder(CountryAdapter.MyViewHolder holder, int
position) {
holder.tvCountryName.setText(list.get(position).getName());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvCountryName;
LinearLayout llListItem;
public MyViewHolder(View itemView) {
super(itemView);
tvCountryName = (TextView)
itemView.findViewById(R.id.tv_country_name);
llListItem = (LinearLayout)
itemView.findViewById(R.id.llListItem);
}
}
private class FooterViewHolder extends MyViewHolder {
public FooterViewHolder(View layoutView, final
RecyclerItemClickListener onRecyclerItemClickListener) {
super(layoutView);
llListItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (onRecyclerItemClickListener != null) {
onRecyclerItemClickListener.onDashBoardItemClick(tvCountryName.getText().toS
tring());
}
}
});
}
}
}
Interface
public interface RecyclerItemClickListener {
void onDashBoardItemClick(String pos);
}
Case 1: Inside ViewHolder if you need to understand exactly position of clicked item you could call getAdapterPosition().
Case 2: You just need to receive all items of adapter. It is simple, make getter method which will return the items.
Case 3: Send info to activity, first make some specific event listener of your view inside ViewHolder, then event triggers (user clicks or other) and you call your interface method which you passed inside adapter, that was implemented in activity.
Tell me if you need an example, i will do it.
Try this one,
Button button= (Button) findViewById(R.id.your_button_id);
button.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//create new ArrayList.
//ArrayList<CountryRes> showList=new ArrayList<>;
//now, put all stored data of a list in this arraylist.
showList.addAll(allCountriesData);
}
});

Dynamically inflating the RecyclerView listItem with EditText string

I'm trying to learn how to use RecyclerViews and the best way to learn is by doing :) so I'm trying to transition from using a ListView in a simple to do list to a Recyclerview. I'm trying to get the String value from the EditText and add that value to my RecyclerView list when the button is clicked. The following are my implementations.
This is my custom adapter. I'm using a ViewHolder to use a basic, single TextView custom list-item.
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{
private List<todo> todoList;
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView toDoTextView;
public ViewHolder(View itemView) {
super(itemView);
toDoTextView = (TextView)itemView.findViewById(R.id.to_do);
}
}
public ItemAdapter(List<todo> todoList){
this.todoList = todoList;
}
#Override
public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ItemAdapter.ViewHolder holder, int position) {
todo toDo = todoList.get(position);
holder.toDoTextView.setText(toDo.getToDo());
}
#Override
public int getItemCount() {
return todoList.size();
}
}
And this is my MainActivity. This is where I'm going to be adding my EditText values to my RecyclerView List. The implementation is done in the setOnClickListener but it gives me an error.
public class MainActivity extends AppCompatActivity {
private List<todo> items = new ArrayList<>();
private ItemAdapter itemAdapter;
private RecyclerView listItemsRecyclerView;
EditText itemsInput;
Button addingItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemsInput = (EditText)findViewById(R.id.to_do_editText);
addingItems = (Button)findViewById(R.id.to_do_btn);
listItemsRecyclerView = (RecyclerView) findViewById(R.id.to_do_list);
final String todoItem = String.valueOf(itemsInput.getText());
itemAdapter = new ItemAdapter(items);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
listItemsRecyclerView.setLayoutManager(layoutManager);
listItemsRecyclerView.setItemAnimator(new DefaultItemAnimator());
listItemsRecyclerView.setAdapter(itemAdapter);
addingItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(todoItem != null){
items.add(todoItem);
itemsInput.setText("");
}else {
Toast.makeText(MainActivity.this, "Please enter something to do", Toast.LENGTH_SHORT).show();
}
itemAdapter.notifyDataSetChanged();
}
});
This is my todo class:
public class todo {
private String toDo;
public todo(){}
public todo(String somethingToDo){
somethingToDo = toDo;
}
public String getToDo() {
return toDo;
}
public void setToDo(String toDo) {
this.toDo = toDo;
}
}
Modify your code inside if condition inside button OnClickListener
if(todoItem != null){
todo mTodo = new todo(todoItem); //Create object of todo using the input done by user.
items.add(mTodo);
itemsInput.setText("");
}
Also your todo class constructor code is wrong
public todo(String somethingToDo){
//somethingToDo = toDo;
toDo = somethingToDo; //need to assign the value received as signature to the class variable.
}

update data from intentservice on an activity

I have an activity with the following code and I am trying to update the data from IntentService by creating the instance of the class and calling the method to update but it's crashing with a null pointer exception at mAdapter. Is this the right way to pass data from IntentService to an activity?
COde:
public class MainActivity extends Activity {
RecyclerView rv;
SimpleStringRecyclerViewAdapter mAdapter;
public static final TypedValue mTypedValue = new TypedValue();
public static int mBackground;
public static List<String> mValues;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cheesecake_homepage_fragment_cheese_list);
rv = (RecyclerView) findViewById(R.id.recyclerview);
setupRecyclerView(rv);
send_msg("");
}
public void setupRecyclerView(RecyclerView recyclerView) {
mAdapter = new SimpleStringRecyclerViewAdapter(getBaseContext(),
getRandomSublist(Cheeses.sCheeseStrings, 3));
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(mAdapter);
}
private List<String> getRandomSublist(String[] array, int amount) {
ArrayList<String> list = new ArrayList<>(amount);
Random random = new Random();
while (list.size() < amount) {
list.add(array[random.nextInt(array.length)]);
}
return list;
}
public static class SimpleStringRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder {
public String mBoundString;
public final View mView;
public final ImageView mImageView;
public final TextView mTextView;
public ViewHolder(View view) {
super(view);
mView = view;
mImageView = (ImageView) view.findViewById(R.id.avatar);
mTextView = (TextView) view.findViewById(android.R.id.text1);
}
#Override
public String toString() {
return super.toString() + " '" + mTextView.getText();
}
}
public String getValueAt(int position) {
return mValues.get(position);
}
public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
mValues = items;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cheesecake_homepage_list_item, parent, false);
view.setBackgroundResource(mBackground);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mBoundString = mValues.get(position);
holder.mTextView.setText(mValues.get(position));
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
/*Intent intent = new Intent(context, CheeseDetailActivity.class);
intent.putExtra(CheeseDetailActivity.EXTRA_NAME, holder.mBoundString);
context.startActivity(intent);*/
}
});
}
#Override
public int getItemCount() {
return mValues.size();
}
}
public void send_msg( String set_text){
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
List<String> uu = getRandomSublist(Cheeses.sCheeseStrings, 1);
mValues.addAll(uu);
mAdapter.notifyDataSetChanged();
}
});
}
}
In intent service:
MainActivity test = new MainActivity();
test.send_msg("");
You can not use MainActivity instance like that in your Intent service code.
There are multiple approach to update UI from service like, LocalBroadcastReceivers, Messengers, BoundedService etc.
You can find an example to update UI from service using LocalBroadcastManager here
update data from intentservice on an activity
By creating Object of class which is extending Activity or any other main application component is not right way for communication between components.
For with in process communication(as in your case want to send data from IntentService to Activity) use LocalBroadcastManager

Categories

Resources