Images on ListView? - java

I have this:
public class First_fragment extends Fragment {
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_profesorado,container,false);
String[] menuItems = {
"Do some","kkk", ",,,,,", "Do some","kkk", ",,,,,",
"Do some", "kkk", ",,,,,", "Do some", "kkk", ",,,,,",
"Do some", "kkk", ",,,,,"
};
ListView listView = (ListView) v.findViewById(R.id.mymenu);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, menuItems
);
listView.setAdapter(listViewAdapter);
return v;
}
}
With the text is correct, I want to use IMAGES instead of text. 10 images in listview. How can do this? Do you have any examples? I am really tired of this because I have been programming all day about this and I failed.

Its possibly to do that using ListActivity and ItemsAdapter. See below code:
public class ItemsList extends ListActivity {
private ItemsAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items_list);
this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems());
setListAdapter(this.adapter);
}
private class ItemsAdapter extends ArrayAdapter<Item> {
private Item[] items;
public ItemsAdapter(Context context, int textViewResourceId, Item[] items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.items_list_item, null);
}
Item it = items[position];
if (it != null) {
ImageView iv = (ImageView) v.findViewById(R.id.list_item_image);
if (iv != null) {
iv.setImageDrawable(it.getImage());
}
}
return v;
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
this.adapter.getItem(position).click(this.getApplicationContext());
}
}
EDIT: It seems like you are new to android development. You have to use ImageView:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/picture_you_want_to_use" />

Related

Two ListFragments in one activity - handle onListItemClick

I'm new to Android and I'm still learning. Could You please help me?
I'm trying to set clickable List in ListFragment with custom ArrayAdapter. It works with normal ArrayAdapter but not with custom. Here is my CustomArrayAdapter class:
public class CustomListAdapter<T> extends ArrayAdapter<T> {
private List<Integer> imageIds;
private List<String> titleList;
private List<String> descriptionList;
private List<String> createdList;
private final Context activityContext;
private static final String DATE_FORMAT_8 = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_8);
public CustomListAdapter(#NonNull Context context, #NonNull List<T> objects) {
super(context, R.layout.row_layout, objects);
this.activityContext = context;
imageIds = new ArrayList<>();
titleList = new ArrayList<>();
descriptionList = new ArrayList<>();
createdList = new ArrayList<>();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View rowView = convertView;
if (convertView == null)
rowView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
ImageView ivNote = rowView.findViewById(R.id.ivNote);
TextView tvTitle = rowView.findViewById(R.id.tvTitle);
TextView tvDescription = rowView.findViewById(R.id.tvDescription);
TextView tvCreated = rowView.findViewById(R.id.tvCreated);
ivNote.setImageResource(imageIds.get(position));
tvTitle.setText(titleList.get(position));
tvDescription.setText(descriptionList.get(position));
tvCreated.setText(createdList.get(position));
return rowView;
}
list setters etc
And here is my ListFragment class:
private List<TaskDTO> taskDTOS;
TasksItemSelected tasksItemSelected;
public interface TasksItemSelected {
void onTaskItemSelected(int index);
}
public TasksFragment() {
// Required empty public constructor
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
tasksItemSelected = (TasksItemSelected) context;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments() != null) {
taskDTOS = (List<TaskDTO>) getArguments().getSerializable("tasksList");
}
View view = inflater.inflate(R.layout.fragment_tasks, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (taskDTOS != null && taskDTOS.size() > 0) {
setListAdapter(getCustomListAdapter(getContext(),taskDTOS));
}
}
private CustomListAdapter getCustomListAdapter(Context activity, List<TaskDTO> taskDTOS) {
CustomListAdapter customListAdapter = new CustomListAdapter(activity, taskDTOS);
customListAdapter.setImageIds(taskDTOS);
customListAdapter.setTitleList(taskDTOS);
customListAdapter.setDescriptionList(taskDTOS);
customListAdapter.setCreatedList(taskDTOS);
return customListAdapter;
}
#Override
public void onListItemClick(#NonNull ListView l, #NonNull View v, int position, long id) {
super.onListItemClick(l, v, position, id);
tasksItemSelected.onTaskItemSelected(position);
}
}
When I set new ArrayAdapter, list items become clickable and new activity runs, but nothing happens when tapping list items in the above case.

how to made grid layout using cardView?

This is Adapter for grid view. I included image and letters in adapter class.
public class GridAdapter extends BaseAdapter {
private int icons[];
private String letters[];
private Context context;
private LayoutInflaterinflater;
public GridAdapter(Context context , int icons[] , String letters[]){
this.context=context;
this.icons=icons;
this.letters=letters;
}
#Override
public int getCount() {
return letters.length;
}
#Override
public Object getItem(int position) {
return letters[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridView = convertView;
if(convertView == null){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = inflater.inflate(R.layout.custom_layout,null);
}
ImageView icon = (ImageView) gridView.findViewById(R.id.icons);
TextView letter = (TextView) gridView.findViewById(R.id.letters);
icon.setImageResource(icons[position]);
letter.setText(letters[position]);
return gridView;
}
}
This is main class. I made 8 card and want to add an activity for each card.how to add activity for each card.
public class NavigationMainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
GridView gridView;
String letterList[] = {"Types of industries", "Safety Sign's",
"Tools & equipments", "Slogens", "Low", "Definitions", "First aid kit", "Near by fire stations"};
int lettersIcon[] = {R.drawable.safetyicon, R.drawable.signs, R.drawable.tools,
R.drawable.slogen, R.drawable.low, R.drawable.definitions,
R.drawable.firstkit, R.drawable.firestations};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
gridView = (GridView) findViewById(R.id.gridView);
GridAdapter adapter = new GridAdapter(NavigationMainActivity.this, lettersIcon, letterList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), TypesOfIndusturies.class);
}
});

Pass object from GridView of Fragment to ListView of another Fragment

I have a GridView in FragmentA containing all objects. When the user tap on each object, these objects are added into ListView of FragmentB.
Note that FragmentA and FragmentB are both in the same Activity, side by side.
I have tried using Bundle to send object from FragmentA to FragmentB but I was stucked as I have no idea how FragmentB is going to know that a new object has been added.
Bundle object is sent from ProductGridAdapter (under FragmentA) and receiver will be FragmentB where FragmentB will update the ListView for the latest objects added by FragmentA
Here is my code:
ProductGridAdapter.java
public class ProductGridAdapter extends ArrayAdapter<Product> {
private List<Product> productList;
private Context context;
public ProductGridAdapter(Context context, int resource,
List<Product> objects) {
super(context, resource, objects);
this.context = context;
this.productList = objects;
}
#Override
public int getCount() {
return ((null != productList) ?
productList.size() : 0);
}
#Override
public Product getItem(int position) {
return ((null != productList) ?
productList.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(null == view) {
view = layoutInflater.inflate(R.layout.grid_product, null);
}
final Product product = productList.get(position);
if(product != null) {
final CardView productGridLayout = (CardView) view.findViewById(R.id.product_gridlayout);
final TextView productName = (TextView) view.findViewById(R.id.product_name);
final ImageView productIcon = (ImageView) view.findViewById(R.id.product_icon);
productName.setText(product.getName());
productIcon.setImageDrawable(product.getDrawable());
productGridLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
bundle.putSerializable("product", product);
fragment.setArguments(bundle);
}
});
}
return view;
}
}
FragmentA.java
public class FragmentA extends Fragment {
private GridView gridView;
private ProductGridAdapter productAdapter;
public FragmentA() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_product_display, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridview);
productAdapter = new ProductGridAdapter(getActivity(), R.layout.fragment_product_display, getProductList());
gridView.setAdapter(productAdapter);
gridView.destroyDrawingCache();
gridView.setVisibility(GridView.INVISIBLE);
gridView.setVisibility(GridView.VISIBLE);
return rootView;
}
}
FragmentB.java
public class FragmentB extends Fragment {
private ListView listView;
private PaymentListAdapter paymentAdapter;
private LinearLayout button;
private ArrayList<String> arr;
public FragmentB() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_payment, container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, getProductList());
// button = (LinearLayout) rootView.findViewById(R.id.payment);
listView.setAdapter(paymentAdapter);
Bundle bundle = this.getArguments();
Product product = (Product) bundle.getSerializable("product");
return rootView;
}
you can have two common method in interface class in main activity, To update view in both fragment,When some changes happens in FragmentA you have to notify that changes to main activity then main activity should communicate to FragementB about that changes.try to do as like this,Hope this will help you.Happy Coding!

Show Custom ArrayAdapter in a ListFragment

I want to show my custom ArrayAdapter in a ListFragment with a SlidingTabLayout. Previously I tested the adapter in a normal activity and worked fine, also the SlidingTabLayout worked correctly, but yet the ListFragment show nothing. I get the data from a meteor server and logcat show me the data has been downloaded...
Does anybody have some tips?
This is my Fragment Java Code:
public class MorningFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_morning,container,false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
MedicationTimeAdapter timeAdapter;
timeAdapter = new MedicationTimeAdapter(getActivity(), R.layout.item_time);
setListAdapter(timeAdapter);
}
}
And my Fragment XML Code:
<?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">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
My adapter
public class MedicationTimeAdapter extends ArrayAdapter<MedicationTime> implements DataManager.MedicationCallback {
private static final String LOG_TAG = MedicationTimeAdapter.class.getName();
private final int resourceId;
private final Activity activity;
// private List<MedicationEntry> medications = new ArrayList<MedicationEntry>();
public MedicationTimeAdapter(Activity context, int resource) {
super(context, resource);
resourceId = resource;
activity = context;
}
#Override
public void handleMedicationPlan(List<MedicationEntry> medication) {
// ignore
}
#Override
public void handleMedicationTimes(final List<MedicationTime> medicationTimes) {
// TODO Activity möglicherweise nicht mehr aktiv
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
setItems(medicationTimes);
}
});
}
public void setItems(List<MedicationTime> itemList) {
clear();
for (MedicationTime item : itemList) {
add(item);
}
notifyDataSetChanged();
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int index, View convertView, ViewGroup parent) {
//data from your adapter
MedicationTime itemData = getItem(index);
if (convertView == null) {
// final LayoutInflater inflater = MHApplication.getLayoutInflater();
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// TODO mit Referenz Parent-Group erzeugen?! (war für ColorFinder notwendig, um die LayoutParams beim Laden zu erhalten)
// convertView = inflater.inflate(resourceId, parent, false);
convertView = inflater.inflate(resourceId, null);
// prepareInflatedView(convertView);
}
setViewContent(index, convertView, itemData);
return convertView;
}
protected void setViewContent(int index, View itemView, MedicationTime itemData) {
final TextView nameView = (TextView) itemView.findViewById(R.id.time_name);
final TextView medicationView = (TextView) itemView.findViewById(R.id.medication);
int nameId = activity.getResources().getIdentifier("time_name." + itemData.getTimeName(), "string", activity.getPackageName());
nameView.setText(activity.getResources().getString(nameId));
nameView.setText(nameId);
StringBuilder medText = new StringBuilder();
List<MedicationTime.Entry> medications = itemData.getMedications();
for (MedicationTime.Entry medication : medications) {
medText.append(medication.getCount());
medText.append(" * ");
medText.append(medication.getMedicationEntry().getSubstance());
medText.append(" (");
medText.append(medication.getMedicationEntry().getTradingName());
medText.append(")\n");
}
medicationView.setText(medText.toString());
}
Edit:
I solved the Problem , i have a Method in my mainActivity to fill the adapter with Data. The problem was i create a new Adapter in my Fragment. So i add a konstruktor in my Fragment class and submitter the Adapter from my mainActivity.
public MorningFragment(MedicationTimeAdapter timeAdapter) {
medicationTimeAdapter = timeAdapter;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_morning,container,false);
setListAdapter(medicationTimeAdapter);
return v;
}
Call handleMedicationTimes(final List<MedicationTime> medicationTimes) with some data, Your code seems is not inflating any data.

android ListActivity with subtitle

I am new to developing for android, I'm trying to make a ListActivity with title and subtitle.
So far only managed to make the title:
this.data = new ArrayList<String>();
// add some objects into the array list
this.data.add("YOU WILL HEAR");
this.data.add("USEFUL PHRASES");
this.data.add("VOCABULARY");
this.data.add("DIALOGUES");
this.data.add("INFORMATION");
this.port = new ArrayList<String>();
this.port.add("BEM VINDO");
this.port.add("FRASES ÚTEIS");
this.port.add("VOCABULÁRIO");
this.port.add("DIÁLOGOS");
this.port.add("VOCABULÁRIO");
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.title,this.data));
ListView lv = getListView();
this code is in the onCreate method.
My question is, how i can populate the R.id.subtitle with the second array?
try the following code:
public ListView lv;
lv = (ListView) findViewById(R.id.ListView01);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setBackgroundResource(R.drawable.background);
lv.setCacheColorHint(00000000);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
class MySimpleArrayAdapter extends ArrayAdapter<String> {
private Context context;
public MySimpleArrayAdapter(Context context) {
super(context, R.layout.list);
this.context = context;
}
public int getCount() {
return names.size();
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = vi.inflate(R.layout.list, null);
}
TextView name = (TextView) rowView.findViewById(R.id.Name);
TextView number = (TextView) rowView.findViewById(R.id.no);
return rowView;
}
}

Categories

Resources