My recycleview isn't working. Can you help me? - java

I really need some help. I'm new in android studio. I'm having a problem about my recyclerview, it wont show some items but there's no errors.
Adapter:
public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ClassViewHolder> {
private ArrayList<ExampleClass> qClassList;
public static class ClassViewHolder extends RecyclerView.ViewHolder {
public TextView code,name,year;
public ClassViewHolder(View view){
super(view);
code = view.findViewById(R.id.code);
name = view.findViewById(R.id.name);
year = view.findViewById(R.id.year);
}
}
public ClassAdapter(ArrayList<ExampleClass> classList){
qClassList = classList;
}
#NonNull
#Override
public ClassViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_classes, viewGroup, false);
ClassViewHolder cvh = new ClassViewHolder(v);
return cvh;
}
#Override
public void onBindViewHolder(ClassViewHolder classViewHolder, int i)
{
ExampleClass exampleClass = qClassList.get(i);
classViewHolder.code.setText(exampleClass.getqCode());
classViewHolder.name.setText(exampleClass.getqName());
classViewHolder.year.setText(exampleClass.getqYear());
}
#Override
public int getItemCount() {
return qClassList.size();
}
}
}
ClassActivity:
public class ClassActivity extends AppCompatActivity {
private ArrayList<ExampleClass> exampleClassArrayList;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classes);
ArrayList<ExampleClass> exampleClasses = new ArrayList<>();
exampleClasses.add(new ExampleClass("CANS103","PROGRAMMING 1","BSIT-3"));
createClassList();
buildRecycleView();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Click action
Intent intent = new Intent(ClassActivity.this, CreateClassActivity.class);
startActivity(intent);
}
});
}
public void createClassList(){
exampleClassArrayList = new ArrayList<>();
}
public void buildRecycleView(){
recyclerView = findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(this);
adapter = new ClassAdapter(exampleClassArrayList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
activity_class.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:padding="4dp"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/recyclerView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="27dp"
android:layout_marginBottom="32dp"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
I've already search some problems similar to mine but it didn't work. I was wondering if someone can tell me whats the mistake I've done here. I would really appreciate it. Sorry for the inconvience.

Chaneg this Lines of code:
ArrayList<ExampleClass> exampleClasses = new ArrayList<>(); // remove this line No need
createClassList(); //call this method
exampleClassArrayList.add(new ExampleClass("CANS103","PROGRAMMING 1","BSIT-3"));
After this call
buildRecycleView()
You are adding in different list (exampleClasses ) and using different list for adapter (exampleClassArrayList) .

In createClassList() method create a new instance from your list, while you added a new item in onCreate list, remove call createClassList() worked correctly

Related

RecyclerView click and replace image from another activity

I have some doubts about how to solve the doubt about how to click on a RecyclerView and change image from another activity, such as MainActivity.
In the image you can see what I'm trying to do. I have a total of 6 images, 3 Small images and another 3 Large images.
In the footer of MainActivity I have the ReciclerView that loads the 3 small images, I want that when they click for example to the image_Small_2/item_Small_2, that replaces the image that is in the center by the image_Large_2/item_Large_2.
I don't know if I'm explaining myself well, I leave you a screenshot and the code to see if you can give me a hand. Thanks in advance. P.S. I use the glide library to load the images.
My Adapter RecyclerView
public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
private ArrayList<Items> itemsLi;
private Context context;
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView idSrcImagen;
public ViewHolder(#NonNull View itemView) {
super(itemView);
idSrcImagen = itemView.findViewById(R.id.idImagen);
}
}
public AdaptadorX(ArrayList<Items> itemsListado, Context context_L){
itemsLi = itemsListado;
context = context_L;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
ViewHolder content = new ViewHolder(view);
return content;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Items contarItems = itemsLi.get(position);
Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Mensaje_AdaptadorX.java", "Mi Posicion FOTO es: "+String.valueOf(position));
}
});
}
#Override
public int getItemCount() {
Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
return itemsLi.size();
}
}
Class Items
public class Items {
private String xNombre_imagen;
public Items (String nombre_imagen_M){
xNombre_imagen = nombre_imagen_M;
}
public String getxNombre_imagen() {
return xNombre_imagen;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
public ImageView idImgHead;
private ArrayList<Items> items;
private RecyclerView idRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idRecyclerView = findViewById(R.id.idRecyclerView);
idImgHead = findViewById(R.id.idImgHead);
Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
listadoXhead();
}
private void listadoXhead() {
ArrayList<Items> items = new ArrayList<>();
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
idRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(MainActivity.this);
((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
adapter = new AdaptadorX(items, MainActivity.this);
idRecyclerView.setLayoutManager(layoutManager);
idRecyclerView.setAdapter(adapter);
}
}
activity_main
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/idImgHead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/idRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/idImagen"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_launcher_foreground"
android:onClick="accionBoton"/>
</LinearLayout>
Example Image Here
You need to implement an interface listener in your activity and pass it to your adapter. Add this to your adapter and pass it through the constructor
interface OnImageClickListener{
void onSelected(String url);
}
Your whole adapter should look like this.
public class AdaptadorX extends RecyclerView.Adapter<AdaptadorX.ViewHolder> {
private ArrayList<Items> itemsLi;
private Context context;
pruvate OnImageClickListener listener;
interface OnImageClickListener{
void onSelected(String url);
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView idSrcImagen;
public ViewHolder(#NonNull View itemView) {
super(itemView);
idSrcImagen = itemView.findViewById(R.id.idImagen);
}
}
public AdaptadorX(ArrayList<Items> itemsListado, Context context_L, OnImageClickListener listener){
itemsLi = itemsListado;
context = context_L;
this.listener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item, parent, false);
ViewHolder content = new ViewHolder(view);
return content;
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
final Items contarItems = itemsLi.get(position);
Glide.with(context).load(contarItems.getxNombre_imagen()).into(holder.idSrcImagen);
holder.idSrcImagen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onSelected(contarItems.getxNombre_imagen())
}
});
}
#Override
public int getItemCount() {
Log.d("Mensaje_Size_Tamaño", String.valueOf(itemsLi.size()));
return itemsLi.size();
}
}
And in your Activity you implement this interface and pass it to the adapter.
public class MainActivity extends AppCompatActivity implements OnImageClickListener {
public ImageView idImgHead;
private ArrayList<Items> items;
private RecyclerView idRecyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idRecyclerView = findViewById(R.id.idRecyclerView);
idImgHead = findViewById(R.id.idImgHead);
Glide.with(this).load("https://www.midominio.com/Imagen_GRANDE_head_01.jpg").into(idImgHead); //IMAGE BIG
listadoXhead();
}
#Override
public onSelected(String url) {
Glide.with(this).load(url).into(idImgHead);
}
private void listadoXhead() {
ArrayList<Items> items = new ArrayList<>();
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_01.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_02.jpg")); //IMAGE Small
items.add(new Items("https://www.midominio.com/Imagen_Pequeña_head_03.jpg")); //IMAGE Small
idRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(MainActivity.this);
((LinearLayoutManager) layoutManager).setOrientation(RecyclerView.HORIZONTAL);
adapter = new AdaptadorX(items, MainActivity.this, this);
idRecyclerView.setLayoutManager(layoutManager);
idRecyclerView.setAdapter(adapter);
}
}

Dynamic ListView With EditTexts Android

I have a ListView in my Android application that uses a CustomAdapterClass. The ListView is populated with rows that contain 2 Edit Texts per row. This is fine, the problem I'm having is the ListView is dynamic, i.e. I have added a button that will append on a new row by calling notifyDataSetChanged(); however this clears the whole list.
How can I keep the text that has been entered into the EditTexts when a new row is appended? Code below:
Activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res
/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="afterDescendants"
android:id="#+id/listViewAddPeople">
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabAddPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#mipmap/ic_launcher"
app:backgroundTint="#FFFFFF"
android:elevation="6dp"
/>
</RelativeLayout>
Row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res
/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/personNameEditText"
android:hint="Enter Persons Full Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/personPhoneNoEditText"
android:layout_below="#+id/personNameEditText"
android:hint="Enter Phone No. (Optional)"/>
</RelativeLayout>
Java Class:
public class AddPeopleNewProcedureActivity extends AppCompatActivity {
private ListView addPeopleListView;
private CustomAdapterClass customAdapter;
private FloatingActionButton fab;
private int lineCount;
private EditText personsNameET;
private EditText personsPhoneET;
private List<String> personsName;
private List<String> personsPhone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addpeoplenew_activity);
setupActivityReferences();
inflateListView();
setupClickListeners();
}
private void setupClickListeners() {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
lineCount = lineCount +1;
personsName.clear();
personsPhone.clear();
customAdapter.notifyDataSetChanged();
}
});
}
private void inflateListView() {
customAdapter = new CustomAdapterClass();
addPeopleListView.setAdapter(customAdapter);
}
private void setupActivityReferences() {
addPeopleListView = (ListView) findViewById(R.id.listViewAddPeople);
fab = findViewById(R.id.fabAddPerson);
personsNameET = (EditText) findViewById(R.id.personNameEditText);
personsPhoneET = (EditText) findViewById(R.id.personPhoneNoEditText);
lineCount = 1;
}
public class CustomAdapterClass extends BaseAdapter {
#Override
public int getCount() {
return lineCount;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view =
getLayoutInflater().inflate(R.layout.row_addpersonnew,null);
final int rowClicked = i;
return view;
}
}
}
Any help is appreciated!
I have added a button that will append on a new row by calling
notifyDataSetChanged();
I can't see any append actions when someone clicks that button. I see that you clear both of your lists, personsName and personsPhone and after that you notify your adapter that those lists are empty.
Write what really want to be done inside the onClick().

How to make card style menu in android

I want to add card style in my app like this
i use in my app mysql database so i need to make like this cards and put my data from database in it now i use ListView with this code
public void listAllItme() {
ListAdapter lA = new listAdapter(listitems);
listView.setAdapter(lA);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent open = new Intent(R_arabic.this, rewaya_show.class);
open.putExtra("name", listitems.get(position).name);
open.putExtra("url", listitems.get(position).url);
open.putExtra("img", listitems.get(position).img);
open.putExtra("num", listitems.get(position).num);
startActivity(open);
}
}
});
}
class listAdapter extends BaseAdapter {
ArrayList<listitem_gib> lista = new ArrayList<listitem_gib>();
public listAdapter(ArrayList<listitem_gib> lista) {
this.lista = lista;
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
View view = layoutInflater.inflate(R.layout.row_item_gib, null);
TextView name = (TextView) view.findViewById(R.id.textView_gib);
ImageView img = (ImageView) view.findViewById(R.id.imageView_gib);
TextView num = (TextView) view.findViewById(R.id.textView_gib2);
TextView size = (TextView) view.findViewById(R.id.textView_gib3);
name.setText(lista.get(position).name);
num.setText(lista.get(position).num);
size.setText(lista.get(position).size);
Picasso.with(R_arabic.this).load("http://grassyhat.com/android/image/" + lista.get(position).img).into(img);
return view;
}
}
first i want to know how i can make like this card style
second how i can use this code with card menu not listview
sorry im new in android and sorry for my bad english
What do you mean by card menu? because the example in the image is a recyclerview with a cardview item, you can achieve this by doing something like this
This will be your activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
//Just your list of objects, in your case the list that comes from the db
List<Items> itemsList = new ArrayList<>();
CardAdapter adapter = new CardAdapter(this, itemsList);
//RecyclerView needs a layout manager in order to display data so here we create one
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//Here we set the layout manager and the adapter to the listview
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Inside the layout file you just have to place the recyclerview like this
<RelativeLayout
android:id="#+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="jsondh.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Then your adapter will be something like this
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {
private List<Items> itemsList;
private Activity activity;
public CardAdapter(Activity activity, List<Items> items){
this.activity = activity;
this.itemsList = items;
}
#Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = activity.getLayoutInflater().inflate(R.layout.cardview_layout, parent, false);
return new CardViewHolder(itemView);
}
#Override
public void onBindViewHolder(CardViewHolder holder, int position) {
//Here you bind your views with the data from each object from the list
}
#Override
public int getItemCount() {
return itemsList.size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
public ImageView bookImage;
public TextView bookLabel01, bookLabel02;
public CardViewHolder(View itemView) {
super(itemView);
bookImage = (ImageView)itemView.findViewById(R.id.image);
bookLabel01 = (TextView)itemView.findViewById(R.id.label01);
bookLabel02 = (TextView)itemView.findViewById(R.id.label02);
}
}
And the last one will be the layout from each item on the list, like this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="15dp"
app:cardBackgroundColor="#3369Ed">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="150dp"
android:layout_height="130dp"/>
<TextView
android:id="#+id/label01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Label"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
<TextView
android:id="#+id/label02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LongerLabel"
android:layout_gravity="right"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</android.support.v7.widget.CardView>
You also have to add this to your gradle file:
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
Hope it helps!
It's pretty simple. You will have to use a RecyclerView with GridLayoutManager and add a cardView to it.
Then, use an Adapter and ViewHolder to feed the data.
I suggest you to check this out:
https://developer.android.com/training/material/lists-cards.html

Insert Value of Checkbox (ListView) into Database

So this has been bugging me for like a week and I haven't found the answer yet.
Basically I have this MainActivity which contains the main code and a fragment that work for the listview.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private Button bNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RoomServer(), "Room Server");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
RoomServer.java
public class RoomServer extends ListFragment implements OnItemClickListener{
public RoomServer() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rs, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setChoiceMode(getListView().CHOICE_MODE_MULTIPLE);
getListView().setTextFilterEnabled(true);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.rs_list, android.R.layout.simple_list_item_checked);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}
}
Fragment_rs.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.RoomServer">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
<Button
android:id="#+id/bNext_rs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp"
android:layout_marginBottom="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
The items of listview r declared inside strings.XML
As u can see I'm using CHOICE_MODE_MULTIPLE to create checkbox in the listview. And now I'm stuck at how to get the value of this checkbox. I'm planning to use Volley to send value to my database by a button click. Most of the answer I saw was using the isChecked and stuff but I dont really understand how to implement it in my array adapter.
You can either implement a custom array adapter yourself, or use this method in the ListView class to get a SparseBooleanArray of the indexes of the selected items:
//Submit button example
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Get base list
String[] items = getActivity().getResources().getStringArray(R.array.rs_list);
//Get selected items & set intent args
SparseBooleanArray selectedItems = getListView().getCheckedItemPositions();
ArrayList<String> argList = new ArrayList<>();
for (int i = 0; i < selectedItems.size(); i++) {
argList.add(items[selectedItems.keyAt(i)]);
}
//Start activity
Intent intent = new Intent();
intent.putStringArrayListExtra("SelectedItems", argList);
getActivity().startActivity(intent);
}
});

How to Store EditText Into ListView Item With Buttons

I am just starting with android, so I don't have much to show of what I have so far...
I am trying to make a to-do list. I have seen tutorials on how to do this with a menu for input and have the tasks displayed in a ListView, however I would like the user to input the task into an EditText instead. I was using this (which doesn't use a ListView) to just have a button click make a new TextView underneath the EditText, however I couldn't add buttons next to the TextView which would remove it (when the user finishes a task).
Using a ListView seems much easier for this, but how can I have the EditText input go to a ListView with all the tasks and buttons to remove them (permanently, not just set visibility to 0)? I would also like the tasks to be stored, so the user can close the app and return with the tasks still there.
Any ideas? I haven't made much progress.
I recommend you to use RecyclerView instead of Listview. compile this in your Gradle
compile 'com.levelupstudio:expandable-recyclerview:1.0.1'
Here is the mainActivity.java
public class MainActivity extends ActionBarActivity implements
RecyclerViewAdapter.OnItemClickListener{
private RecyclerView myRecyclerView;
private LinearLayoutManager linearLayoutManager;
private RecyclerViewAdapter myRecyclerViewAdapter;
EditText nameField;
Button btnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
/*
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
*/
myRecyclerViewAdapter = new RecyclerViewAdapter(this);
myRecyclerViewAdapter.setOnItemClickListener(this);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerView.setLayoutManager(linearLayoutManager);
nameField = (EditText)findViewById(R.id.namefield);
btnAdd = (Button)findViewById(R.id.addbutton);
btnAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String newName = nameField.getText().toString();
Context context= getApplicationContext();
myRecyclerViewAdapter.add(0,newName);
Toast.makeText(context,"You added" +newName.toUpperCase()+ "in your view",Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
Toast.makeText(this,
"Remove " + position + " : " + item.getItemName(),
Toast.LENGTH_SHORT).show();
}}
And AdapterClass for Recycler
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {
private List<String> itemsName;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public RecyclerViewAdapter(Context context){
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
}
#Override
public RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.list_item, parent, false);
return new ItemHolder(itemView, this);
}
#Override
public void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {
holder.setItemName(itemsName.get(position));
}
#Override
public int getItemCount() {
return itemsName.size();
}
public void setOnItemClickListener(OnItemClickListener listener){
onItemClickListener = listener;
}
public OnItemClickListener getOnItemClickListener(){
return onItemClickListener;
}
public interface OnItemClickListener{
public void onItemClick(ItemHolder item, int position);
}
public void add(int location, String iName){
itemsName.add(location, iName);
notifyItemInserted(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RecyclerViewAdapter parent;
TextView textItemName;
public ItemHolder(View itemView, RecyclerViewAdapter parent) {
super(itemView);
itemView.setOnClickListener(this);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.item_name);
}
public void setItemName(CharSequence name){
textItemName.setText(name);
}
public CharSequence getItemName(){
return textItemName.getText();
}
#Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if(listener != null){
listener.onItemClick(this, getPosition());
}
}
}}
and your xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/namefield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/addbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/myrecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
list_xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:id="#+id/item_name"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

Categories