I recently started to use ListView on Android Application and I'm not making it to display at the screen.
I saw many tutorials on how to do a custom Adapter and followed it line by line, but at the end I wasn't able to make the ListView display on my screen, I even let a fixed Text in one TextView to display at the ListView but it still didn't displayed it.
This is the constructor of my entity code:
public Classificacao(int colocacao, int time, int pontos) {
this.colocacao = colocacao;
this.time = time;
this.pontos = pontos;
}
This my Custom Adapter that extends "ArrayAdapter"
private Context context;
private ArrayList<Classificacao> classificacaoList;
ClassificacaoAdapter(Context context, ArrayList<Classificacao> classificacaoList) {
super(context, R.layout.layout_classificacao, classificacaoList);
this.context = context;
this.classificacaoList = classificacaoList;
}
#SuppressLint("SetTextI18n")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.layout_classificacao, parent, false);
}
TextView colocacao = convertView.findViewById(R.id.texto1);
colocacao.setText(Integer.toString(classificacaoList.get(position).getColocacao()));
ImageView imagem = convertView.findViewById(R.id.imagem1);
imagem.setImageResource(classificacaoList.get(position).getImagem());
TextView nome = convertView.findViewById(R.id.texto2);
nome.setText(classificacaoList.get(position).getTime());
TextView pontos = convertView.findViewById(R.id.texto3);
pontos.setText(Integer.toString(classificacaoList.get(position).getPontos()));
return convertView;
}
This the layout "activity_atividade02" that have the ListView, and some others Text views that I won't display here.
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
This is the layout "layout_classificacao" that will be filled by the adapter
<LinearLayout 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="wrap_content"
android:paddingTop="20dp">
<TextView
android:id="#+id/texto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="30sp" />
<ImageView
android:id="#+id/imagem1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="imagem do time"
app:srcCompat="#drawable/flamengo" />
<TextView
android:id="#+id/texto2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:textColor="#color/colorPrimary"
android:textSize="30sp" />
<TextView
android:id="#+id/texto3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:textColor="#android:color/black"
android:textSize="30sp" />
</LinearLayout>
And this is the main class "Atividade02"
that start everything
public class Atividade02 extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_atividade02);
listView = findViewById(R.id.listView);
ArrayList<Classificacao> classificacaoArrayList = new ArrayList<>();
classificacaoArrayList.add(new Classificacao(1, 2,46));
classificacaoArrayList.add(new Classificacao(2, 1,42));
classificacaoArrayList.add(new Classificacao(3,0,38));
classificacaoArrayList.add(new Classificacao(4,3,35));
classificacaoArrayList.add(new Classificacao(5,5,33));
classificacaoArrayList.add(new Classificacao(6,4,33));
ArrayAdapter classificacaoAdapter = new ClassificacaoAdapter(this, classificacaoArrayList);
listView.setAdapter(classificacaoAdapter);
}
}
Resuming, the ListView isn't displaying the "ClassificacaoAdapter" and I want it to do it.
Make sure adapter has the method getItemcount() and it returns the classificacaoList.size(). It should not return 0;
Well... it was a pretty obvious problem, in the main layout activity_atividade02 there was a LinearLayout above the ListView that it's layout_height was set to match_parent and it was occupying the entire layout and not letting the ListView being displayed.
I test your code as you write and found an error here and it works normally but I comment imageView line because you don't define it in Classificacao class
an error here
TextView nome = convertView.findViewById(R.id.texto2);
nome.setText(classificacaoList.get(position).getTime());
setText() accept String only ..but you pass integer rather than String you can do that
nome.setText(""+classificacaoList.get(position).getTimee());
or use
Integer.toString( classificacaoList.get(position).getTimee() )
as you use in other lines
please add getCount() method in your ClassificacaoAdapter class, it is an override method. It will return the size of the list,if you will not override this it will return 0 as your list size.
public int getCount() {
return classificacaoList .size();
}
Related
I tried a lot of solutions but no solution has worked for me.
I am getting data from the server and showing it inside a recycleview. The data shows fine but shows this error in the logcat
Access denied finding property "persist.vendor.log.tel_dbg"
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
sometimes when I refresh again and again the recyclerview becomes empty.
I am using viewpager consisting of 2 fragments inside a parent fragment.
this recycleview is using in a nested scrollview
some time data not showing
Code snippet:
homebuyer_adapter_recycler=new homebuyer_adapter_recycle(getActivity(), items);
LinearLayoutManager home = new LinearLayoutManager(getActivity());
home.setOrientation(LinearLayoutManager.VERTICAL);
allitemsgrid.setLayoutManager(home);
allitemsgrid.setAdapter(r);
Here is more about
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,
final int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.items_view,parent,
false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
ViewHolder viewHolder = new ViewHolder(v);
HERE IS MY item view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:cardCornerRadius="6dp"
android:layout_marginBottom="3dp"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:id="#+id/image_items"
android:scaleType="fitXY"
/>
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:background="#drawable/blackshade"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/items_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:fontFamily="sans-serif-smallcaps"
android:paddingLeft="20dp"
android:text="Message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Timing"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:fontFamily="#font/mylight"
android:layout_alignParentRight="true"
android:textSize="10dp"
android:id="#+id/shoptimming"/>
</RelativeLayout>
<TextView
android:id="#+id/name_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:alpha="0.6"
android:fontFamily="#font/arimo_bold"
android:paddingLeft="20dp"
android:text="Name"
android:textColor="#color/black"
android:textSize="17dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<TextView
android:id="#+id/minumum_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:drawablePadding="10dp"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:text="Minimum " />
<TextView
android:id="#+id/price_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:gravity="left"
android:paddingLeft="20dp"
android:text="charges"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is my adapter class
public class homebuyer_adapter_recycle extends RecyclerView.Adapter<homebuyer_adapter_recycle.ViewHolder> {
private ArrayList<seller_information> listData;
private LayoutInflater layoutInflater;
int lastpostition=-1;
Context context;
public homebuyer_adapter_recycle(Context aContext, ArrayList<seller_information> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder
(#NonNull ViewGroup parent, final
int viewType) {
View v = LayoutInflater.from(parent.getContext()).
inflate(R.layout.items_view,parent, false);
ViewHolder viewHolder = new ViewHolder(v);
Log.i("inadapter","calling time");
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
phone_number_shop = listData.get(viewType).getPhonenumber();
homebuyer.Delivery_charges_shop =
listData.get(viewType).getDiliveryfee();
homebuyer.Shop_name = listData.get(viewType).getShopname();
homebuyer.minimum_order = listData.get(viewType).getMinorder();
//profession=items.get(i).getName();
// Toast.makeText(getActivity(),phn,Toast.LENGTH_SHORT).show();
all_and_cetegory_items items = new all_and_cetegory_items();
Bundle b = new Bundle();
b.putString("phone",homebuyer.phone_number_shop);
items.setArguments(b);
FragmentTransaction fragmentTransaction =
((AppCompatActivity)context).getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, items);
fragmentTransaction.addToBackStack(null).commit();
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(listData.get(position).getShopname());
//
Picasso.with(context).load(listData.get(position)
.getShopimage()).into(holder.shopimage);
Glide.with(context).load(listData.get(position).getShopimage())
.into(holder.shopimage);
holder.dilivery.setText("Rs "+listData.get(position).getDiliveryfee()+"
Delivery fee");
holder.minorder.setText("Rs "+listData.get(position).getMinorder()+" minimum");
holder.items.setText(listData.get(position).getShopmessage());
holder.time.setText("Service Available
"+listData.get(position).getStartingtime()+" to
"+listData.get(position).getEndingtime());
Log.i("inadapter","calling time"+listData.get(position).getShopname());
}
#Override
public int getItemCount() {
return listData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView dilivery;
TextView items;
TextView minorder;
TextView time;
ImageView shopimage;
public ViewHolder(#NonNull View v) {
super(v);
name = (TextView) v.findViewById(R.id.name_item);
dilivery = (TextView) v.findViewById(R.id.price_item);
minorder = (TextView) v.findViewById(R.id.minumum_order);
items=(TextView)v.findViewById(R.id.items_all);
time=(TextView)v.findViewById(R.id.shoptimming);
shopimage=(ImageView) v.findViewById(R.id.image_items);
}
}
}
In error it is saying that the Adapter for RecylerView is not attached. So, try to add adapter to the layout using:
recyclerView.setAdapter(categoryAdapter);
I am assuming homebuyer_adapter_recycler is your adapter.
According to that, you haven't really set your adapter as the logcat is specifying.
Add the below code instead of allitemsgrid.setAdapter(r);
allitemsgrid.setAdapter(homebuyer_adapter_recycler);
If my assumption or suggestion is wrong, it is okay. It is probably because your question is not very clear and does not provide the necessary details. Please provide more details such as the adapter class code and what is allitemsgrid, homebuyer_adapter_recycler and r.
I am in the making of a shopping list using a ListView with custom items with the posibility of increasing the amount of that item and also decreasing the amount.
At the moment, I get the right amount of items when I load them in to the shopping list, as can seen in the image below:
the image of the ListView and problem
But the problem occurs when I am trying to increase/decrese the amount of each item by clicking the buttons, only the last item in the ListView gets updated by the clicks.
The reason for this is because i am re-declaring the TextView for each item, and the others (probably?) doesn't get stored anywhere. So i read about "setTag()" and "getTag()" and tried to implement but this didn't work either (maybe beacuse i did it wrong).
I am using a custom class called "Item" which stores each item and the amount of that item.
Otherwise, this is the custom adapter and the .xml
public class ShoppingListAdapter extends ArrayAdapter<Item> {
int amount;
TextView amountText;
private Context mContext;
int mResource;
public ShoppingListAdapter(#NonNull Context context, int resource, #NonNull ArrayList<Item> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
//get the item information
String name = getItem(position).getName();
String brand = getItem(position).getBrand();
int id = getItem(position).getId();
String img = getItem(position).getImg();
amount = getItem(position).getAmount();
//all info
Item item = new Item(name,id,brand,img);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource,parent,false);
TextView tVName = (TextView) convertView.findViewById(R.id.textView2);
ImageView imgV = (ImageView) convertView.findViewById(R.id.imageView);
TextView tVId = (TextView) convertView.findViewById(R.id.textView3);
amountText = (TextView) convertView.findViewById( R.id.amountItem );
tVName.setText(name);
amountText.setText( String.valueOf( amount ) );
amountText.setTag(position);
Picasso.with(mContext).load(img).into(imgV); // tVBrand.setText(brand);
//tVId.setText(id);
//Buttons
Button addButton =(Button) convertView.findViewById(R.id.addValue);
Button subButton =(Button) convertView.findViewById(R.id.subValue);
addButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
getItem(position).incrementAmount();
//This gives the right position and the amount for all items
Log.d("ifIncreased", " " + getItem(position).getAmount());
Log.d("ifIncreased", position + "" );
TextView newText = (TextView) amountText.getTag(position);
newText.setText( String.valueOf( getItem(position).getAmount()));
}
} );
subButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
if(getItem(position).getAmount() == 1)
{
//TODO remove from list;
}
else{
getItem(position).decrementAmount();
Log.d("ifIncreased", position + "" );
amountText.setText( String.valueOf( getItem(position).getAmount()) );
}
}
} );
return convertView;
}
and the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:weightSum="100">
<ImageView
android:id="#+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="33.3"
app:srcCompat="#mipmap/ic_launcher" />
<LinearLayout
android:layout_width="212dp"
android:layout_height="60dp"
android:layout_weight="33.3"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/textView2"
android:gravity=""
android:paddingLeft="10dp"
android:text="TextView3" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity=""
android:paddingLeft="10dp"
android:text="TextView2" />
</LinearLayout>
<LinearLayout
android:layout_width="112dp"
android:layout_height="60dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/amountItem"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="1"
android:textStyle="bold" />
<Button
android:id="#+id/addValue"
android:layout_width="40dp"
android:layout_height="33dp"
android:layout_gravity="center"
android:text="+"
android:textAlignment="center"
android:textSize="12sp" />
<Button
android:id="#+id/subValue"
android:layout_width="40dp"
android:layout_height="33dp"
android:layout_gravity="center"
android:text="–"
android:textAlignment="center"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
So- since I am having the right values in the onClick functions, my question is, how can I change each TextView (the amount of that item) from the items in the full list?
Try this to update single value into adapter.. make public method and that method called into fragment or activity when you updated any value and refresh adapter.
/**
* this method used to update list item when broad cast received.
* #param percentage
* #param position
*/
public void updateProgress(double percentage, final int position) {
feedBackVoList.get(position).setPercentage(percentage);
notifyItemChanged(position);
}
after that updated value set into textview when you called notifyItemChanged(position) reset again that row or refresh that row.
I have a listview populating using cursor in which custom row exist. There are set of textviews in the row. I also have a image view in the row. I would like to set an image based on a value of the textview. My code is
public class DisplayCards extends Activity {
private Divyadesamdb dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pasuram);
dbHelper = new Divyadesamdb(this);
dbHelper.open();
// Clean all data
dbHelper.deleteAllPasurams();
// Add some data
dbHelper.insertPasurams();
Intent intent = getIntent();
String category = intent.getStringExtra(MainActivity.EXTRA_CATEGORY);
String start = intent.getStringExtra(MainActivity.EXTRA_START);
String ending = intent.getStringExtra(MainActivity.EXTRA_ENDING);
// Generate ListView from SQLite Database
displayListView(category, start, ending);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void displayListView(String mcategory, String mstart, String mending) {
String category = mcategory;
String starting = mstart;
String ending = mending;
int rowsFound = 0;
Cursor cursor = dbHelper.fetchAllPasurams(category, starting, ending);
/*
* How many rows returned from the query
*/
rowsFound = cursor.getCount();
// the desired columns to be bound
String[] columns = new String[] {
Divyadesamdb.PASURAMS_COLUMN_PAASURAMNUMBER,
Divyadesamdb.PASURAMS_COLUMN_AAYIRAM,
Divyadesamdb.PASURAMS_COLUMN_AZHWAAR,
Divyadesamdb.PASURAMS_COLUMN_CATEGORY,
Divyadesamdb.PASURAMS_COLUMN_MANGALASASANAMON,
Divyadesamdb.PASURAMS_COLUMN_PAASURAM_EN_STR,
Divyadesamdb.PASURAMS_COLUMN_SUBCATEGORY,
Divyadesamdb.PASURAMS_COLUMN_TITLE,
Divyadesamdb.PASURAMS_COLUMN_TITLENUMBER };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.tvPaasuramNumber, R.id.tvAayiram,
R.id.tvAzhwaar, R.id.tvCategory, R.id.tvMangalasasanamOn,
R.id.tvPaasuram, R.id.tvSubCategory, R.id.tvTitle,
R.id.tvtvTitleNumber };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
MyCursorAdapter dataAdapter = new MyCursorAdapter(this,
R.layout.paasuram_single_item, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
}
// extend the SimpleCursorAdapter to create a custom class where we
// can override the getView to change the row colors
private class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// get reference to the row
View view = super.getView(position, convertView, parent);
// check for odd or even to set alternate colors to the row
// background
// want to get the value of this text view,
// but not getting handle.
LayoutInflater inflater = ((Activity) Context).getLayoutInflater();
TextView azhwaarView = (TextView) view.getTag(R.id.textViewAzhwaar);
String azhVal = azhwaarView.getText().toString();
if (position % 2 == 0) {
view.setBackgroundColor(Color.rgb(238, 233, 233));
} else {
view.setBackgroundColor(Color.rgb(255, 255, 255));
}
return view;
}
}
}
not sure how to get the handle of textview. Getting Null pointer exception. Any help is greatly appreciated.
paasuram_single_item.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="wrap_content"
android:background="#drawable/mybackground"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_margin="10dp"
android:contentDescription="#string/Azhwaar"
android:padding="5dp"
android:src="#drawable/nammalwar" />
<TextView
android:id="#+id/tvtvTitleNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ivAzhwaar"
android:layout_toRightOf="#+id/ivAzhwaar"
android:text="TitleNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvHyphan1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvtvTitleNumber"
android:layout_alignBottom="#+id/tvtvTitleNumber"
android:layout_toRightOf="#+id/tvtvTitleNumber"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/tvHyphan1"
android:layout_alignBottom="#+id/tvHyphan1"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/tvHyphan1"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvAayiram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvtvTitleNumber"
android:layout_below="#+id/tvtvTitleNumber"
android:text="Aayiram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvHyphan2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvtvTitleNumber"
android:layout_toRightOf="#+id/tvAayiram"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvHyphan2"
android:layout_alignBottom="#+id/tvHyphan2"
android:layout_toRightOf="#+id/tvHyphan2"
android:text="Category"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvHyphan3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvCategory"
android:layout_alignBottom="#+id/tvCategory"
android:layout_toRightOf="#+id/tvCategory"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvSubCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvHyphan3"
android:layout_alignBottom="#+id/tvHyphan3"
android:layout_toRightOf="#+id/tvHyphan3"
android:text="SubCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvPaasuram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvAayiram"
android:layout_below="#+id/tvAayiram"
android:padding="5dp"
android:text="paasuram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvPaasuramNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvPaasuram"
android:layout_alignBottom="#+id/tvPaasuram"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="PaasuramNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvMangalasasanamOn"
android:layout_alignBottom="#+id/tvMangalasasanamOn"
android:layout_alignLeft="#+id/tvPaasuram"
android:text="Azhwaar"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/tvMangalasasanamOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvCategory"
android:layout_below="#+id/tvPaasuram"
android:layout_centerHorizontal="true"
android:text="MangalasasanamOn"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Only two errors are their:
1).You are not inflating any view here:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
View view = inflater.inflate(R.layout.custom_inflator_layout,parent,false);
TextView azhwaarView = (TextView) view.getTag(R.id.textViewAzhwaar);
String azhVal = azhwaarView.getText().toString();
return view;
}
2).You are not seting any text to textview ,thats y when you trying to get the text
it gives null pointer.
In order to resolve the problem I have done the following:
created a table column with int datatype
got the R.drawable.xxx and stored the int to the database
retrieved the data and set to the image in the row level.
Thanks for all the help! Appreciate every help rendered. Big thanks once again.
I have a list fragment displaying my custom list items which consist of a relative layout containing images and text etc.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/main_background"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="#+id/source_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#color/black"
android:layout_alignParentLeft="true"
android:contentDescription="#string/info" />
<TextView
android:id="#+id/item_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:textStyle="bold"
android:layout_toRightOf="#+id/source_image"
android:textColor="#color/black"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:textAlignment="center"
android:text="#string/item_desc" />
<ImageView
android:id="#+id/arrow_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
android:contentDescription="#string/info"
android:src="#drawable/arrow_right" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_centerInParent="true"
android:background="#color/main_background"
android:layout_below="#+id/item_description">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="fill_horizontal"
android:layout_marginRight="20dp"
android:padding="5dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:src="#drawable/chameleon" />
<ImageView
android:id="#+id/item_image2"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:gravity="center"
android:src="#drawable/chameleon" />
<ImageView
android:id="#+id/item_image3"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/info"
android:gravity="center"
android:src="#drawable/chameleon" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
I am trying to set up an OnItemClickListener so when the user clicks anywhere on the list item, a new Intent is started.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Populate list view with array objects
adapter = new HomeListItemAdapter(items, getActivity());
listView = getListView();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(listView.getContext(), "CLICKED", Toast.LENGTH_SHORT);
toast.show();
}
});
}
I am attempting this as shown above but can't seem to get the OnItemClickListener to work (just testing with Toast instead of Intent for now). Where am I going wrong?
Thanks
EDIT: Added custom list adapter code
public class HomeListItemAdapter extends BaseAdapter {
private List<HomeListItem> items;
private Context context;
private int numItems = 0;
public HomeListItemAdapter(final List<HomeListItem> items, Context context) {
this.items = items;
this.context = context;
this.numItems = items.size();
}
public int getCount() {
return numItems;
}
public HomeListItem getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Get the current list item
final HomeListItem item = items.get(position);
// Get the layout for the list item
final RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
// Set the the item picture(s)
ImageView itemImage = (ImageView) itemLayout.findViewById(R.id.item_image);
itemImage.setImageDrawable(item.getItemPic());
//Set the profile picture / source picture
ImageView sourceImage = (ImageView) itemLayout.findViewById(R.id.source_image);
sourceImage.setImageDrawable(item.getSourcePic());
// Set the text label as defined in our list item
TextView itemDesc = (TextView) itemLayout.findViewById(R.id.item_description);
AssetManager am = context.getAssets();
Typeface font = Typeface.createFromAsset(am, "Raleway-Thin.ttf");
itemDesc.setTypeface(font);
itemDesc.setText(item.getText());
return itemLayout;
}
}
Try extending ListFragment (instead of just a normal Fragment). Then you can override onListItemClicked:
public void onListItemClick(ListView l, View v, int pos, long id)
This gives you the list, view, position, and id, so you should be able to do whatever you need.
Could you try setting the HozizontalScrollView to not get focus with the XML attribute
android:focusable="false"
If you have a TextView inside the row then that will take focus when the row is clicked. This will mean that onListItemClick is not called. You either need to have an OnClickListener on the TextView or remove focus from the TextView.
See ListFragment OnListItemClick not being called
Try this sample project for costum ListFragment. It is a very good example with a costum ListFragment showing 2 types of views for phone and tablets.
http://s000.tinyupload.com/index.php?file_id=09444774931653041243
Courtsey
http://jcrazyandroider.blogspot.in/2015/01/download-links.html
I have listview that i want to fill the whole screen,There are four items in listview. It leaves empty space below after four items are filled.You can see in the screenshot. It leaves the blank space. I want whole screen to be covered.
I would like to have like this:
Here is the source Code
MainActivity.java.
public class MainActivity extends Activity {
ListView resultPane;
List<Taskinfo> list;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultPane = (ListView) findViewById(R.id.mylist);
list = new ArrayList<Taskinfo>();
Resources res = getResources(); // resource handle
Drawable drawable = res.getDrawable(R.drawable.browse_home);
list.add(new Taskinfo("Browse", drawable));
drawable = res.getDrawable(R.drawable.jewelry);
list.add(new Taskinfo("Whats New", drawable));
drawable = res.getDrawable(R.drawable.show);
list.add(new Taskinfo("Upcoming Show", drawable));
drawable = res.getDrawable(R.drawable.contact);
list.add(new Taskinfo("Contact Us", drawable));
adapter = new CustomAdapter(this, list);
resultPane.setAdapter(adapter);
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Taskinfo> list;
public CustomAdapter(Context context, List<Taskinfo> list) {
this.context = context;
this.list = list;
}
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_list_item, parent, false);
}
Taskinfo t = list.get(index);
RelativeLayout l = (RelativeLayout) view
.findViewById(R.id.testrelative);
l.setBackgroundDrawable(t.getImage());
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(t.getName());
return view;
}
}
single_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testrelative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/browse_home"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
</LinearLayout>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text=""
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
activity_main.xml
<RelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45sp"
android:id="#+id/llayout"
android:background="#drawable/navbar"
android:padding="3sp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Golden Stone"
android:textColor="#color/white"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_below="#id/llayout"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
Using ListView in such case is strange and unnecessary. Think about your constraints (like: what if items overflow?) and just use a proper layout manager. Like a vertical LinearLayout with the last item having a non-zero layout weight.
ListViews make sense only if you need an abstraction that generates list items on-the-fly.
You would want to set a background on the ListView. Currently your view should be going to the bottom, but the background of the ListView is transparent, so setting it white should achieve what you're asking.
<ListView ...
android:background="#android:color/white"
... />
if there are so few items, you can use the LinearLayout instead, and give weights for each of its items.
However, do note that android supports many devices and screens, so you might want to have a limitation of how small each row would be.
anyway, in case you wish to make each row the fitting height, you can check its size and then divide by the number of items.
in order to get the size of the listView , you can use this small snippet i've made .