The button view shows confirming that the fragment shows, however the recycler view doesn't.
The logged array size of "jobs" is 1, confirming that getItemCount() isn't the problem and that the recycler adapter constructor is called. I'm reusing my JobRecyclerAdapter class but nothing's static, so there shouldn't be any interference between the two instances. The layout is a a copy of a working one I have, minus different ids and layout name, so that shouldn't be a problem either. There are no errors.
Layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<include layout="#layout/top_bar"
android:id="#+id/topBarAJ"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:layout_constraintTop_toTopOf="parent"/>
<include layout="#layout/bottom_bar"
android:id="#+id/bottomBarAJ"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/activeJobRecyclerView"
app:layout_constraintTop_toBottomOf="#id/topBarAJ"
app:layout_constraintBottom_toTopOf="#id/bottomBarAJ"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/activeJobRecyclerView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/topBarAJ" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerAdapter
List<Job> jobs;
List<Drawable> images;
RecyclerViewClickListener clickListener;
public JobRecyclerAdapter(List<Job> downloadedJobs, List<Drawable> images, RecyclerViewClickListener clickListener) {
this.jobs = downloadedJobs;
this.images = images;
Integer arraySize = images.size();
Log.d("downloadActiveJob", "JobRecyclerAdapter: images array size: " + arraySize.toString());
arraySize = jobs.size();
Log.d("downloadActiveJob", "JobRecyclerAdapter: jobs array size: " + arraySize.toString());
this.clickListener = clickListener;
}
class JobCardViewHolder extends RecyclerView.ViewHolder {
public ImageView itemImage;
public TextView itemName;
public TextView itemWeight;
public TextView itemSize;
public TextView transmitterName;
public JobCardViewHolder(View v) {
super(v);//call default constructor
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("RecyclerView", "onClick:" + getAdapterPosition());
clickListener.recyclerViewListClicked(v, getAdapterPosition());
}
});
itemImage = (ImageView) v.findViewById(R.id.itemImage);
itemName = (TextView) v.findViewById(R.id.itemName);
itemWeight = v.findViewById(R.id.itemWeight);
itemSize = v.findViewById(R.id.itemSize);
transmitterName = v.findViewById(R.id.transmitterName);
}
}
public void removeItem(int jobIndex) {
jobs.remove(jobIndex);
notifyDataSetChanged();
}
#Override
public JobCardViewHolder onCreateViewHolder(ViewGroup vg, int n) {
View v = LayoutInflater.from(vg.getContext()).inflate(R.layout.job_card_layout, vg, false);
JobCardViewHolder vH = new JobCardViewHolder(v);
return vH;
}
#Override
public void onBindViewHolder(JobCardViewHolder vH, int position) {
vH.itemName.setText(jobs.get(position).itemName);
vH.itemImage.setImageDrawable(images.get(position));
vH.itemWeight.setText(jobs.get(position).itemWeight);
vH.itemSize.setText(jobs.get(position).itemSize);
vH.transmitterName.setText(jobs.get(position).transmitterName);
}
#Override
public int getItemCount() {
return jobs.size();
}
}
Fragment (I only showed the relevant parts of the fragment.)
RecyclerView jobRecyclerView;
JobRecyclerAdapter jobRecyclerAdapter;
LinearLayoutManager llm;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sub_fragment_active_jobs_1, container, false);
this.jobRecyclerView = view.findViewById(R.id.activeJobRecyclerView);
return view;
}
public void configureJobsRecyclerAdapter(Job activeJob, List<Drawable> imageDrawables) {
Log.d("downloadActiveJob", "configureJobRecyclerAdapter called");
List<Job> downloadedJobs = new ArrayList<>();
downloadedJobs.add(activeJob);
Integer arraySize = imageDrawables.size();
Log.d("downloadActiveJob", "configureJobRecyclerAdapter: " + arraySize.toString());
jobRecyclerAdapter = new JobRecyclerAdapter(downloadedJobs, imageDrawables, this);
llm = new LinearLayoutManager(context);
jobRecyclerView.setLayoutManager(llm);
jobRecyclerView.setAdapter(jobRecyclerAdapter);
}
Update: If I move the recycler view/adapter setup code to onResume, the recycler view is visible. When I update the recycler view with a new adapter, the recycler view becomes invisible.
If I can't solve it using this new discovery I'll post the whole fragment.
Hey, you create the function configureJobsRecyclerAdapter() but didn't call it inside fragment onCreateView(). Like...
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sub_fragment_active_jobs_1, container, false);
this.jobRecyclerView = view.findViewById(R.id.activeJobRecyclerView);
configureJobsRecyclerAdapter()//Add this line.
return view;
}
if you are fetching data which are required for configureJobsRecyclerAdapter() from an api maybe you are setting empty list at the time when you are calling configureJobsRecyclerAdapter(). and that's why when you call it in onResume, the list becomes visible(because then data is fetched).
and don't forget to call adapter.notifyDataSetChanged() after each change in your list.
Related
I have a SearchView and a ListView. My goal here is when I search for (for example) Toyota, I got result in the ListView with Toyota, and than when I click on it, Toyota save to another list.
Everything is working well, except the ListView. When I search for something, I get different result. For example if I search for Toyota, I got BMW or Mazda etc. but not Toyota. BUT, if I click on this result Toyota will be saved and not BMW or Mazda despite I click on them.
I think something with the ListView is not good, because in the background evertyhing is working well, but it's shown otherwise.
Here is my xml file:
<SearchView
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false">
<requestFocus />
</SearchView>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
I am using a custom row in my ListView, I don't think its affect any harm, but now I am even questioning my own existence :D So here it is the list_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"
android:padding="15sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name"
android:layout_centerVertical="true"
android:layout_marginLeft="28sp"
android:textColor="#73c993"/>
</RelativeLayout>
And the ListViewAdapter.java:
public class ListViewAdapter extends ArrayAdapter<String> {
ArrayList<String> list;
Context context;
public ListViewAdapter(Context context, ArrayList<String> items) {
super(context, R.layout.list_row, items);
this.context = context;
list = items;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row, null);
TextView name = convertView.findViewById(R.id.name);
name.setText(list.get(position));
}
return convertView;
}
}
Finally, my HomeFragment.java:
public class HomeFragment extends Fragment implements SearchView.OnQueryTextListener {
SearchView editsearch;
static ListView listView;
static ListViewAdapter adapter;
static ArrayList<String> cars, selectedCars;
static Context context;
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
return inflater.inflate(R.layout.fragment_home, container, false);
}
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
listView = (ListView) view.findViewById(R.id.list);
cars = new ArrayList<>(Arrays.asList("Toyota", "BMW", "Mazda"));
selectedCars= new ArrayList<>();
context = getContext();
adapter = new ListViewAdapter(context, cars);
listView.setAdapter(adapter);
editsearch = (SearchView) view.findViewById(R.id.search);
editsearch.setOnQueryTextListener(this);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = adapter.getItem(position);
if (!(selectedCars.contains(item))) {
addItem(item);
makeToast(item + " added.");
} else {
makeToast(item + " already added.");
}
}
});
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
I'm a beigner working on firebase java android. My problem is that i cannot load the recyclerView it is showing that adapter not attached Skipping layout even though I've attached the adapter still its showing the same
Below is my code please help me. Thanks in advance
private RecyclerView bookList,listOrg,listcustom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_work_employee);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
MyCustomWorkRef = FirebaseDatabase.getInstance().getReference().child("WorksCustomAdded");
bookList = findViewById(R.id.myCustomerList);
listcustom = findViewById(R.id.myCustomerListCustom);
bookList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyWorkEmployeeActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
bookList.setLayoutManager(linearLayoutManager);
listcustom.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(MyWorkEmployeeActivity.this);
linearLayoutManager2.setReverseLayout(true);
linearLayoutManager2.setStackFromEnd(true);
listcustom.setLayoutManager(linearLayoutManager2);
//LoadDat();
DisplayCustom("ltG284DWVbbxfHlD3vAiiwkEv963","22883b15-d432-42b7-8ba5-1b3d4586ff09");
// DisplayCustom();
}
This is my Display Custom Function
private void DisplayCustom(String cmpnyUID,String empId) {
Toast.makeText(getApplicationContext(), "CompnUID : " + cmpnyUID + "\n empID : "+empId, Toast.LENGTH_SHORT).show();
Query SortPostsInDecendingOrder = MyCustomWorkRef.child(cmpnyUID).orderByChild("empID").equalTo(empId);;
FirebaseRecyclerOptions<MyCustomer> options = new FirebaseRecyclerOptions.Builder<MyCustomer>()
.setQuery(SortPostsInDecendingOrder, MyCustomer.class)
.build();
FirebaseRecyclerAdapter<MyCustomer, MyWorkEmployeeActivity.PBooksViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MyCustomer, MyWorkEmployeeActivity.PBooksViewHolder>(options)
{
#NonNull
#Override
public MyWorkEmployeeActivity.PBooksViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i)
{
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.viewwork_list_eng, viewGroup, false);
MyWorkEmployeeActivity.PBooksViewHolder viewHolder =new MyWorkEmployeeActivity.PBooksViewHolder(view);
return viewHolder;
}
#Override
protected void onBindViewHolder(#NonNull MyWorkEmployeeActivity.PBooksViewHolder holder, int position, #NonNull MyCustomer model)
{
final String pos = getRef(position).getKey();
String cmpnyID = model.getCmpnyID();
String wrkID = model.getWrkSiteID();
String engID = model.getEngKey();
String usrID = model.getUsrkey();
holder.bookname.setText(model.getUsrname());
holder.count.setText("Work No: \n"+model.getCnt());
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reqIntent = new Intent(MyWorkEmployeeActivity.this, ViewCustomWrkActivity.class);
reqIntent.putExtra("wrkID", model.getWrkSiteID());
reqIntent.putExtra("usrkey","0");
startActivity(reqIntent);
}
});
}
};
listcustom.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
public class PBooksViewHolder extends RecyclerView.ViewHolder {
TextView bookname, bookauthor,fees ,count;
ImageView profImage;
LinearLayout mView;
String t_area, b_area, plan, allow;
public PBooksViewHolder(#NonNull View itemView) {
super(itemView);
profImage = itemView.findViewById(R.id.ivRequestProfile);
bookname = itemView.findViewById(R.id.tvRequestUserName);
bookauthor = itemView.findViewById(R.id.tvWorksCompletedEngineerList);
mView = itemView.findViewById(R.id.request_layout);
fees = itemView.findViewById(R.id.tvFeesEngineerList);
count = itemView.findViewById(R.id.wrkCount);
}
}
I found the answer for this problem. In My xml File I've been using match_parent on the height. Which caused the main thread to skip the layout, because it couldn't find the layout scales.
So I changed the value to some standard height in dp. Eg: 600dp. Which defined the layout correctly so now the recyclerView is loading perfectly without skipping the layout.
my XML Code
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myWorkListemp"
android:layout_width="match_parent"
android:layout_height="700dp"
android:layout_marginTop="15dp" />
<androidx.recyclerview.widget.RecyclerView
android:visibility="gone"
android:id="#+id/myCustomerListCustom"
android:layout_width="match_parent"
android:layout_height="700dp"
android:layout_marginTop="15dp" />
</LinearLayout>
`
So I have my main Bottom Navigation Activity, and there is three section one of which is dashboard. And I want to make a grid of categories in there. I've been trying to follow some guides on youtube about creation of GridView, but it seems to not work in this Bottom Navigation Activity. The main issue is that in my custom adapter in method getView it can't resolve R.layout.category_item but the file exists.
I would be grateful for some workaround or explanation of why it happens.
DashboardFragment class:
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
String[] category_names = {"one","two","three","four","five","six","seven","eight","nine","ten"};
int[] data ={1,2,3,4,5,6,7,8,9,10};
GridView gridView;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
}
});
gridView = root.findViewById(R.id.category_grid);
CustomAdapter customAdapter = new CustomAdapter();
gridView.setAdapter(customAdapter);
return root;
}
private class CustomAdapter extends BaseAdapter{
#Override
public int getCount() {
return category_names.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view1 = getLayoutInflater().inflate(R.layout.category_item,null);
return null;
}
}
}
category_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.dashboard.DashboardFragment">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Hi i don't know if it's the solution of your problem but you don't use any context when you inflate your view
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate the layout for each list row
if (convertView == null) {
convertView = LayoutInflater.from(context).
inflate(R.layout.layout_list_view_row_items, parent, false);
}
...
}
Ok, I have an Listview adapter that contains an ImageView, TextView and another ImageView in that order. So I want to be able to delete the item in list when user presses on second ImageView. When the user press on the item in list it will start TextToSpeech and it will say what is inside TextView. But if the user wants to remove the entry he/she will press on second ImageView which is a delete icon, at that point the item will be remove from the list. Starting the click listener for the imageview is not a problem, the problem is how do I get the number (int) of the corresponding item in listview adapter so I can remove it from array. Here's xml list_item.xml for single entry in list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp">
<ImageView
android:id="#+id/item_icon_imageview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:src="#drawable/ic_record_voice_24dp2"/>
<TextView
android:id="#+id/phrase_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:text="My phone number is 305 666 8454"/>
<ImageView
android:layout_gravity="center"
android:id="#+id/delte_item_imageview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:src="#drawable/ic_delete_black_24dp"/>
</LinearLayout>
</RelativeLayout>
The fragment for inflating the listview
public class CustomFragment extends Fragment {
private ArrayAdapter<String> adapter;
private ListView listView;
private FloatingActionButton addPhraseButton;
private TextView phraseTitleTextView;
private TextToSpeech textToSpeech;
private ArrayList<String> phrases;
private static final String PHRASE_LABEL = " Phrases";
private static final String CATEGORY_KEY = "categories";
private ImageView deleteItemImageView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_custom, container, false);
final String categories;
//if we selecte a category sent from home fragment else we got here through menu
Bundle arguments = getArguments();
if (arguments != null && arguments.containsKey(CATEGORY_KEY)) {
categories = arguments.getString(CATEGORY_KEY).toLowerCase();
}
else {
Resources res = view.getResources();
categories = res.getStringArray(R.array.categories)[0].toLowerCase();
}
final Phrases allPhrases = Phrases.getPhrases();
allPhrases.fetchAllPhrases(view.getContext());
phrases = allPhrases.getAllPhrases().get(categories);
phraseTitleTextView = view.findViewById(R.id.label_phrases_txtview);
deleteItemImageView = view.findViewById(R.id.delte_item_imageview);
phraseTitleTextView.setText(categories.substring(0,1).toUpperCase() +
categories.substring(1)+ PHRASE_LABEL);
addPhraseButton = view.findViewById(R.id.add_phrases_btn);
// setting local for text to speech
textToSpeech = new TextToSpeech(getActivity().getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
textToSpeech.setLanguage(Locale.US);
}
});
//setting adapter and listview
adapter = new ArrayAdapter<String>(getContext(), R.layout.entry_item, R.id.phrase_textview, phrases);
listView = (ListView) view.findViewById(R.id.phrases_list);
listView.setAdapter(adapter);
listView.setItemsCanFocus(true);
//activating text to speech when user selects item in listview
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paren, View view, int position, long id) {
String text = phrases.get(position);
Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show();
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,null, null);
}
});
deleteItemImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
//button to display alert dialog box to add new phrase
addPhraseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addPhraseDialogBox();
}
});
return view;
}
}
You can achieve this through Custom Adapter. Check below:
public class CustomArrayAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
ArrayList<String> phrases;
public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
super(context, textViewResourceId, items);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
phrases = items;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.entry_item, parent, false);
}
....
ImageView deleteItemImageview = (ImageView) convertView.findViewById(R.id. delte_item_imageview);
deleteItemImageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
phrases.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}
I am trying to make a listView that contains only images without texts but it gives a white screen and I am not getting any error or anything.
Here is my codes:
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="3">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2">
<ListView
android:id="#+id/setOneListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</RelativeLayout>
.........
</LinearLayout>
</LinearLayout>
Activity:
public class Set1_Activity extends Activity {
ListView list;
int[] images = {
R.drawable.iv1, R.drawable.iv2, R.drawable.iv3, R.drawable.iv4,
R.drawable.iv5, R.drawable.iv6, R.drawable.iv11, R.drawable.iv12,
R.drawable.iv13, R.drawable.iv14, R.drawable.iv15, R.drawable.iv16,
R.drawable.iv111, R.drawable.iv112, R.drawable.iv113, R.drawable.iv114,
R.drawable.iv115, R.drawable.iv116, R.drawable.iv7, R.drawable.iv8,
R.drawable.iv9, R.drawable.iv19, R.drawable.iv49, R.drawable.iv50,
R.drawable.iv52, R.drawable.iv54
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
list = (ListView) findViewById(R.id.setOneListView);
HeartlessAdapter adapter = new HeartlessAdapter(this, images);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch(position){
case 0:
display.setImageResource(R.drawable.iv1);
tophone = R.drawable.iv1;
break;
.........
class HeartlessAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;
HeartlessAdapter(Context c, int imgs[])
{
super(c, R.layout.imageview);
this.context=c;
this.images=imgs;
}
class MyViewHolder
{
ImageView myImage;
MyViewHolder(View v)
{
myImage = (ImageView) v.findViewById(R.id.imageView);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
MyViewHolder holder = null;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.imageview, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
}
else
{
holder = (MyViewHolder) row.getTag();
}
holder.myImage.setImageResource(images[position]);
return row;
}
}
All works fine, no error and the emulator just show empty white screen nothing inside. What could be missing?
Thanks!
Try moving the android:layout_weight="2" up a level to the horizontal LinearLayout.
1) Let HeartlessAdapter extend ArrayAdapter<Integer> instead of ArrayAdapter<String>.
2) on your constructor, call super(c, R.layout.imageview, imgs); It should work like that.
3) If you wanna optimize it a lot, remove the references to Context and the array of images from the adapter. You can get any item you need calling getItem(position) and the Context by calling getContext().
EDIT:
In order for it to work, call this:
super(c, R.layout.imageview, HeartlessAdapter.toIntegerArray(imgs));
and add this method to HeartlessAdapter:
private static Integer[] toIntegerArray(int[] array){
Integer[] finalArray = new Integer[array.length];
for (int i=0; i<array.length; i++){
finalArray[i] = array[i];
}
return finalArray;
}
try this...
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged()
}
});