ListView is giving me a blank page - android

Im trying to get ListView to display a list of accounts but its returning a blank page. Im not getting any error messages either. I have looked around for similar issues but none of the solution is a fix to this problem. Grateful if someone could help.
FILE > ListActivity.java
public class ListeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_liste);
Guichet guichet = new Guichet();
ArrayList<Epargne> epargneList = new ArrayList<Epargne>(guichet.listEpargne);
EpargneAdapter adapter = new EpargneAdapter(ListeActivity.this, R.layout.liste_comptes, epargneList);
final ListView list = (ListView) findViewById(R.id.maListe);
list.setAdapter(adapter);
}
}
FILE > EpargneAdapter.java
private ArrayList<Epargne> epargneList;
private Context context;
private int viewRes;
private Resources res;
public EpargneAdapter(Context context, int textViewResourceId, ArrayList<Epargne> versions) {
super(context, textViewResourceId, versions);
this.epargneList = versions;
this.context = context;
this.viewRes = textViewResourceId;
this.res = context.getResources();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.
LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(viewRes, parent, false);
}
final Epargne epargne = epargneList.get(position);
if (epargne != null) {
final TextView numero = (TextView) view.findViewById(R.id.numeroCompte);
final TextView solde = (TextView)view.findViewById(R.id.soldeCompte);
String numeroC = res.getString(R.string.numero) + " " + epargne.getNumeroCompte();
numero.setText(numeroC);
String SoldeC =res.getString(R.string.solde) + " " + epargne.getSoldeCompte();
solde.setText(SoldeC);
}
return view;
}
}
FILE > activity_liste.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".ListeActivity">
<ListView
android:id="#+id/maListe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
/>
</LinearLayout>
FILE > liste_Compte.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp">
<ImageView android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:src="#mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/numeroCompte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<TextView
android:id="#+id/soldeCompte"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
FILE > Guichet.java
public class Guichet extends AppCompatActivity {
ArrayList<Epargne> listEpargne = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
setTitle("Guichet automatique ATM");
initListEpargne(listEpargne);
}
public void initListEpargne(ArrayList<Epargne> listEpargne) {
Epargne epargne1 = new Epargne();
epargne1.setNumeroCompte("E0001");
epargne1.setSoldeCompte(10000);
listEpargne.add(epargne1);
Epargne epargne2 = new Epargne();
epargne2.setNumeroCompte("E0002");
epargne2.setSoldeCompte(10000);
listEpargne.add(epargne2);
Epargne epargne3 = new Epargne();
epargne3.setNumeroCompte("E0003");
epargne3.setSoldeCompte(10000);
listEpargne.add(epargne3);
Epargne epargne4 = new Epargne();
epargne4.setNumeroCompte("E0004");
epargne4.setSoldeCompte(10000);
listEpargne.add(epargne4);
}
}
Thanks a lot!

You are not initializing the listEpargne when you call it.
But lets forget it. You create a class that extends the AppCompatActivity and try to create that class with new keyword :o.
The classes which extend from the AppCompatActivity should create only by intent and than to start.
You can make a class called Guichet who don't extends the AppCompatActivity and there provide a method getData(). This method will return that list. But don't forgot to initialize and fill that list :).

Related

Changing the background of list items by click on a separate button

I have a listView with some items now I want to change the background color or item text color by click on a separate button which IS NOT on the list . How can I do that in my android app?
This is my main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayAdapter = new SubArrayAdapter(this, dataItemList);
ListView lv = findViewById(R.id.mylist);
lv.setAdapter(arrayAdapter);
ImageButton btn_check = findViewById(R.id.button_checkout);
btn_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// how can I access the list items within this click-handler method
});
List Item Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listitems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="#+id/yes_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:paddingRight="5dp"
android:visibility="invisible"
/>
<ImageView
android:id="#+id/no_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:paddingRight="5dp"
android:visibility="invisible"
/>
<TextView
android:id="#+id/paragraph_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/yes_image"
android:layout_toRightOf="#id/yes_image"
android:textColor="#color/paragraph_items_colro"
android:textSize="16sp"
android:textStyle="normal" />
</RelativeLayout>
Activity_main xml layout file:
android.support.constraint.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:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:textDirection="ltr"
tools:context=".MainActivity">
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#android:color/transparent"
android:divider="#color/colorAccent"
android:dividerHeight="1dp"
android:listSelector="#0f0"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
<LinearLayout
android:id="#+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#id/mylist">
<ImageButton
android:id="#+id/button_checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/checkout_9"
android:src="#drawable/checkout_9"
app:layout_constraintTop_toBottomOf="#id/mylist"
tools:layout_editor_absoluteX="106dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
And I have myown adapter class that in the getview() method I populated the list items
this inner class is located in mainactivity:
private class SubArrayAdapter extends ArrayAdapter<DataItem> {
private List<DataItem> items;
private Context context;
public SubArrayAdapter(#NonNull Context context, #NonNull List<DataItem>
items) {
super(context, R.layout.listitems, items);
this.items = items;
this.context = context;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull
ViewGroup parent) {
DataItem dataitem1 = items.get(position);
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listitems, null);
TextView tv = (TextView)
view.findViewById(R.id.paragraph_description);
tv.setText(dataitem1.getDescription());
tv.setTag(position + 100);
ImageView yes_iv = view.findViewById(R.id.yes_image);
int yes_res = context.getResources().getIdentifier("yes",
"drawable", getPackageName());
yes_iv.setImageResource(yes_res);
yes_iv.setTag(position + 200);
//yes_iv.setVisibility(View.VISIBLE);
ImageView no_iv = view.findViewById(R.id.no_image);
int no_res = context.getResources().getIdentifier("no", "drawable",
getPackageName());
no_iv.setImageResource(no_res);
no_iv.setTag(position + 300);
return view;
}
}
}
I've noticed You have set android:listSelector for ListView in Your activity_main.xml - which was giving You an ability to change item's background on single click.
So I decided to check out how it works in ListView class.
I've looked at source code of ListView and searched for phrase selector. There was the line:
} else if (mAction == STATE_REQUEST_FOCUS) {
final int childIndex = mPosition - mFirstPosition;
final View child = getChildAt(childIndex);
if (child != null) {
child.requestFocus();
}
mAction = -1;
}
final View child = getChildAt(childIndex);
Following these steps - I knew solution for Your question problem. Here it is:
ImageButton imageButton = findViewById(R.id.button_checkout);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int hardCodedPoositionToSelect = 1;
View childAt = listView.getChildAt(hardCodedPoositionToSelect);
if (childAt != null) {
//change background
childAt.setBackgroundColor(Color.RED);
//change textView color of current view
TextView paragraphDescription = childAt.findViewById(R.id.paragraph_description);
paragraphDescription.setTextColor(Color.MAGENTA);
}
}
});
You can see I am calling getChildAt() on listView to obtain current item's view basing on item's position from hardCodedPoositionToSelect.

Implement RecyclerView with CardView and GridView

Im trying to implement a RecyclingView that contains a CardView which contains a GridView. Not getting any errors just nothing is showing. Just a white screen when I try to run the app.
public class MainActivity extends AppCompatActivity {
TextView mWeatherTemp, mWeatherDescript;
RecyclerView.Adapter adapter;
Context context;
GridView gridView;
String[] mTime = {
"1:00",
"2:00",
"3:00",
"4:00",
"5:00",
"6:00",
"7:00",
"8:00",
"9:00",
"10:00",
"11:00",
};
String[] mDegreeTemp = {
"8°",
"12°",
"43°",
"100°",
"32°",
"12°",
"58°",
"39°",
"29°",
"86°",
"70°",
};
int[] imageId = {
R.mipmap.ic_launcher
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
Toolbar mToolbar = (Toolbar) findViewById(R.id.mMainToolbar);
setSupportActionBar(mToolbar);
mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.weather_cool));
mWeatherTemp = (TextView) findViewById(R.id.tvWeatherTemp);
mWeatherDescript = (TextView) findViewById(R.id.tvWeatherText);
Typeface robotDisplay3 = Typeface.createFromAsset(getApplicationContext().getAssets(),
"font/Roboto-Regular.ttf");
mWeatherTemp.setTypeface(robotDisplay3);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.mHourlyRV);
HourlyGridAdapter adapter = new HourlyGridAdapter(getApplicationContext(), mTime, mDegreeTemp, imageId);
gridView = (GridView) findViewById(R.id.hourlyGridView);
gridView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings, menu);
return super.onCreateOptionsMenu(menu);
}
}
public class HourlyGridAdapter extends BaseAdapter {
private Context mContext;
private String[] mTime;
private String[] mDegreeTemp;
private final int[] imageId;
public HourlyGridAdapter(Context context, String[] mTime, String[] mDegreeTemp, int[] imageId) {
mContext = context;
this.imageId = imageId;
this.mDegreeTemp = mDegreeTemp;
this.mTime = mTime;
}
#Override
public int getCount() {
return 0;
}
#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) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.hourly_single, null, false);
holder = new ViewHolder();
holder.timeText = (TextView) convertView.findViewById(R.id.timePlaceHolder);
holder.degreeText = (TextView) convertView.findViewById(R.id.degreePlaceHolder);
holder.weatherImage = (ImageView) convertView.findViewById(R.id.weatherIconHolder);
holder.timeText.setText(mTime[position]);
holder.degreeText.setText(mDegreeTemp[position]);
holder.weatherImage.setImageResource(imageId[position]);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView timeText, degreeText;
ImageView weatherImage;
}
}
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TODAY" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/forecast_card_divider" />
<GridView
android:id="#+id/hourlyGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:horizontalSpacing="12dp"
android:numColumns="4"
android:verticalSpacing="12dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/timePlaceHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/timePlaceHolderText"
android:layout_gravity="center"/>
<ImageView
android:id="#+id/weatherIconHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:layout_gravity="center"/>
<TextView
android:id="#+id/degreePlaceHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12°"
android:layout_gravity="center"/>
</LinearLayout>
Im not even sure if having a GridView inside a CardView all inside a recycled view is possible. Any help trying to create this would be creately appreicated.
PS. I know im not calling recyclerView thats declared as that doesnt work and does crash the app and the GridView is null. Not really sure what to do here. Not sure if I need to create an adapter for the recyclerview as all the textViews and ImageView is in the grid not the cardview.
You are initializing and setting adapter for gridview which is inside each item of recyclerview not in recycler adapter but in activity. Consider removing all gridview-related code to recyclerview's adapter.

Android - listView not showing

I try to make a list view with content from realm, but problem is that nothing shows when I open activity in emulator, just white screen.
Code is
public class ListActivity extends AppCompatActivity {
ListView list;
Realm realm;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_items);
realm = Realm.getDefaultInstance();
RealmResults<ArticleList> results = realm.where(ArticleList.class).equalTo("category", "Танковые сражения").findAll();
ListActivityListAdapter adapter = new ListActivityListAdapter(this, results);
list = (ListView) findViewById(R.id.listactivity_listview);
list.setAdapter(adapter);
button = (Button) findViewById(R.id.button);
}
}
public class ListActivityListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final RealmResults<ArticleList> articleList;
public ListActivityListAdapter(Activity context, RealmResults<ArticleList> articleList) {
super(context, R.layout.listview_listactivity_item);
this.context = context;
this.articleList = articleList;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.listview_listactivity_item, null, true);
TextView title = (TextView) rowView.findViewById(R.id.listview_item_title);
TextView subtitle = (TextView) rowView.findViewById(R.id.listview_item_subtitle);
ImageView image = (ImageView) rowView.findViewById(R.id.listview_item_imageView);
ArticleList item = articleList.get(position);
title.setText(item.getTitle());
subtitle.setText(item.getSubtitle());
String imageName = item.getImage();
File imageFile = ImageStorage.getImage(imageName, getContext());
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeFile(context.getFilesDir() + "/tanks/" + imageName + ".jpg");
} catch (Exception e) {
e.printStackTrace();
}
image.setImageBitmap(bitmap);
return rowView;
}
}
activity_list_of_items.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.me.demo2.ListActivity"
tools:showIn="#layout/listview_main_list">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listactivity_listview"
android:layout_gravity="center_horizontal|top"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="164dp" />
listview_listactivity_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="213dp"
android:id="#+id/listview_item_imageView"
android:layout_gravity="center_horizontal|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listview_item_title"
android:paddingTop="100dp"
android:layout_gravity="center_horizontal|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listview_item_subtitle"
android:paddingTop="150dp"
android:layout_gravity="center_horizontal|top" />
What did I do wrong ?
I think you're missing the getCount() method in your ArrayAdapter. Also, dont forget about #Override annotation for all the method you're overriding.
Try adapter.notifyDataChanged() after list.setAdapter(adapter);

Android CardsLib, how to add and set custom values in list item?

I'm working with this library:
https://github.com/gabrielemariotti/cardslib
I'm trying to add some text views into cardslib layout item.
Custom text view in layout for single item:
<?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="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/card_main_inner_simple_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/card_main_inner_secondary_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/card_main_inner_name_to_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIS TEXT I WANT DYNAMICALLY CHANGE"
android:textColor="#color/list_gray"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
I would like to ask, how can i dynamically set and change this textview with id card_main_inner_name_to_call directly from the code?
CardsLIb Library offers only setTitle methods for header and card but nothing like the setTitleById or something similar.
How can I simple do it?
Thanks for any advice
If you would like to build a card with this layout you can do something like this.
You have to extend your class and override the setupInnerViewElements method
public MyCard extends Card{
public String title1; //just an example... use gettes and setters
public String title2;
public MyCard(Context context){
super(context, R.layout.your_layout);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
TextView tx= (TextView)view.findById(R.id.card_main_inner_simple_title);
tx.setText(title1);
//.... set the other ui elements
}
}
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//......
MyCard card = new Mycard(this);
card.title1 = "....";
card.title2 = "....";
//Set card in the cardView
CardView cardView = (CardView) getActivity().findViewById(R.id.carddemo);
cardView.setCard(card);
}
}
If you are working with a List, you can use the same card class:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//......
ArrayList<Card> cards = new ArrayList<Card>();
for (int i=0;i<50;i++){
MyCard card = new Mycard(this);
card.title1 = "....";
card.title2 = "....";
cards.add(card);
}
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this,cards);
CardListView listView = (CardListView) this.findViewById(R.id.myList);
if (listView!=null){
listView.setAdapter(mCardArrayAdapter);
}
}
I've never used this library but from documentation I can suspect that it should be handled just like normal list. In adapter you should fill view and by notifydatasetchanged() you can refresh it.
https://github.com/gabrielemariotti/cardslib/blob/master/doc/QUICKUSAGE.md
EDIT:
I've created very primitive sample below.
Activity:
public class MyActivity extends Activity {
private static final int MAX_LENGTH = 20;
private ArrayList<Card> cards = new ArrayList<Card>();
private CardAdapter arrayAdapter;
private HashMap<Integer, String> optionalValues = new HashMap<Integer, String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
arrayAdapter = new CardAdapter(getBaseContext(), cards, optionalValues);
for(int i = 0; i < 10; i++) {
Card card = new Card(getBaseContext());
CardHeader header = new CardHeader(getBaseContext());
header.setTitle("ASDF");
card.setInnerLayout(R.layout.card1_layout);
card.addCardHeader(header);
cards.add(card);
}
CardListView listView = (CardListView) findViewById(R.id.card_list);
listView.setAdapter(arrayAdapter);
Button btn = (Button) findViewById(R.id.btn_rand);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String rnd = random();
Toast.makeText(getBaseContext(), "Generated for 2nd card: " + rnd, Toast.LENGTH_SHORT).show();
optionalValues.put(1, rnd);
arrayAdapter.notifyDataSetChanged();
}
});
}
public static String random() {
Random generator = new Random();
StringBuilder randomStringBuilder = new StringBuilder();
int randomLength = generator.nextInt(MAX_LENGTH);
char tempChar;
for (int i = 0; i < randomLength; i++){
tempChar = (char) (generator.nextInt(96) + 32);
randomStringBuilder.append(tempChar);
}
return randomStringBuilder.toString();
}}
Adapter:
public class CardAdapter extends CardArrayAdapter {
private final LayoutInflater mInflater;
private HashMap<Integer, String> opts;
public CardAdapter(Context context, List<Card> cards, HashMap<Integer, String> optionalValues) {
super(context, cards);
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.opts = optionalValues;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView tv = (TextView) v.findViewById(R.id.text1);
if(position == 1 && opts.get(1) != null) {
tv.setText(opts.get(1));
} else {
//items are recycled so let's get back to default values
tv.setText("default");
}
return v;
}}
Main activity 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"
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=".MyActivity">
<Button
android:id="#+id/btn_rand"
android:layout_centerHorizontal="true"
android:text="Randomize 2nd text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<it.gmariotti.cardslib.library.view.CardListView
android:id="#+id/card_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="60dp"
/>
Card inner layout ("card1_layout.xml"):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/carddemo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp"
android:orientation="vertical">
<TextView
android:id="#+id/text1"
android:text="default"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text2"
android:text="default2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Listview custom Row - not high enough

I have a listview which contains custom rows. When I show the listview with it's custom rows, all the rows only have a height of about 2px. I tried it on an Android 4.0 device, and on an emulator which has Android 2.x. I added a screenshot of the problem.
Prepare, here comes quite a lot of code:
This is my code for my custom row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="105px"
android:background="#fff"
android:orientation="vertical"
android:paddingLeft="15dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8px"
android:gravity="top"
android:text="name"
android:textColor="#322a37"
android:textSize="11pt" />
</RelativeLayout>
The code in my activity:
public class ListActivity extends BaseActivity {
private ArrayList<User> m_items;
private ObjectAdapter m_adapter;
private EditText textSearch;
private ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
textSearch = (EditText) findViewById(R.id.textSearch);
listView = (ListView) findViewById(R.id.listView);
m_items = new ArrayList<User>();
//(fill m_items, goes well)
this.m_adapter = new ObjectAdapter(this, R.layout.row_text, m_items);
listView.setAdapter(this.m_adapter);
}
private class ObjectAdapter extends ArrayAdapter<User> {
private ArrayList<User> items;
#SuppressWarnings("unchecked")
public ObjectAdapter(Context context, int textViewResourceId,
ArrayList<User> 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.row_text, null);
}
User u = items.get(position);
if (u != null) {
TextView top = (TextView) v.findViewById(R.id.toptext);
if (top != null) {
top.setText(u.getName());
}
}
return v;
}
}
}
Anyone any clue why this is happening?
The line:
android:layout_height="0dip"
does not strike me as being particularly clever.
Edit your custom row xml and correct the hight of the TextView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="105px"
android:background="#fff"
android:orientation="vertical"
android:paddingLeft="15dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8px"
android:gravity="top"
android:text="name"
android:textColor="#322a37"
android:textSize="11pt" />

Categories

Resources