I am trying to display information from a database as checkboxes. Right now it works fine, but places one column of checkboxes on the screen. What I want to do is have it split into 2.
What it Does Now:
item1
item2
item3
item4
item5
What I Want:
item1 item2
item3 item4
item5
Preferably the new 2 column list would be evenly sized, each getting 50% of the screen, even if the text assigned is different lengths.
I have searched a lot, and tried like 3 different things. Havn't found anything that works. Below is my code as it is (creating one column).
private void listStudents()
{
st = (LinearLayout)findViewById(R.id.studTable);
ml = (RelativeLayout)findViewById(R.id.main);
ArrayList<ArrayList<String>> studentlst = new ArrayList<ArrayList<String>>();
studentlst = db.getAllStudentsRowsAsArrays();
final int sllen = studentlst.size();
final String student[][] = new String[sllen][3];
CheckBox cb[] = new CheckBox[sllen];
TextView pemail[] = new TextView[sllen];
int num = st.getChildCount();
if(num != 0) st.removeAllViews();
String tsl = sllen + "";
nos.setText(tsl);
for(int x=0; x < sllen; x++)
{
/************************
* student[x][case] *
* case options *
* 0 = id *
* 1 = name *
* 2 = email *
************************/
String curstudent = studentlst.get(x).toString();
student[x][0] = curstudent.substring(1,curstudent.indexOf(","));
student[x][1] = curstudent.substring(curstudent.indexOf(" ")+1,curstudent.lastIndexOf(","));
student[x][2] = curstudent.substring(curstudent.lastIndexOf(" ")+1, curstudent.length() - 1);
}
Arrays.sort(student, new Comparator<String[]>() {
#Override
public int compare(String[] entry1, String[] entry2) {
String name1 = entry1[1];
String name2 = entry2[1];
return name1.compareTo(name2);
}
});
for(int x=0;x<sllen;x++)
{
cb[x] = new CheckBox(this);
cb[x].setId(x+100);
cb[x].setText(student[x][1]);
pemail[x] = new TextView(this);
pemail[x].setText(student[x][2]);
pemail[x].setId(x+1000);
pemail[x].setVisibility(View.INVISIBLE);
st.addView(cb[x],x);
ml.addView(pemail[x],x);
}
}
XML File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/lblmainselclass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="82dp"
android:layout_marginTop="7dp"
android:text="#string/lblmainselectclass" />
<Spinner
android:id="#+id/mainclassspinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/lblmainselclass" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_below="#+id/lblmainselstudents"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="#+id/studTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/lblmainselstudents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mainclassspinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/lblselectstud" />
<Button
android:id="#+id/btnsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/btnlblsend" />
<Spinner
android:id="#+id/mainresponsespinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnsend"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp" />
<TextView
android:id="#+id/mainnumofstudents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/invis"
android:visibility="invisible" />
<TextView
android:id="#+id/classselected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/invis"
android:visibility="invisible" />
<TextView
android:id="#+id/responseselected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/invis"
android:visibility="invisible" />
<TextView
android:id="#+id/numchecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/invis"
android:visibility="invisible" />
Old question, but I found myself here so I'll go ahead and answer it. There are a couple ways, but here's how I did it.
First, get your data and populate an ArrayList of Checkboxes:
ArrayList<CheckBox> checkboxes = new ArrayList<CheckBox>();
for (String s : dataArray) {
CheckBox cBox = new CheckBox(myContext); //or inflate it from xml if you have a defined style
cBox.setText(s);
checkboxes.add(cBox);
}
Then create a class that extends BaseAdapter along with a supporting static class. We will bind the data to the adapter assuming your CheckBox ArrayList is an instance variable. If not, you can create a constructor in the BaseAdapter class access checkboxes that way.
private class CheckboxGridAdapter extends BaseAdapter {
#Override
public int getCount() {
return checkboxes.size();
}
#Override
public Object getItem(int pos) {
return checkboxes.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
final ViewPlaceHolder holder;
final CheckBox item = (CheckBox) getItem(pos);
if (convertView == null) {
holder = new ViewPlaceHolder();
holder.cb = (CheckBox) item;
convertView = holder.cb;
convertView.setTag(holder);
} else {
holder = (ViewPlaceHolder) convertView.getTag();
}
return convertView;
}
}
static class ViewPlaceHolder {
CheckBox cb;
}
Finally, back where you created the ArrayList of Checkboxes, create a GridView (either programmatically or by inflating an xml resource), then attach the BaseAdapter. Don't forget to set the number of columns, that's the whole point of this post after all.
GridView grid = new GridView(myContext);
grid.setNumColumns(2);
grid.setAdapter(new CheckboxGridAdapter());
You should use a custom ListView and a BaseAdapter for efficency if you have many entries.
You can find good examples to start with in the SDK ApiDemos (android-sdk/samples/android-x/ApiDemos).
You should use a GridView instead of your ScrollView. This way you can dynamically create your items and have two (or more columns)
Related
I have a ListView that shows list items (duh). When you click on a list item, another Activity opens. Part of the list item layout is a grey star, an ImageView. When you click on this ImageView, I don't want to open another Activity, but I want to change the color of the star to green (= mark the item as favourite) or back (= mark it as not favourite). I managed to do that with an OnClickListener, loading another ImageView on Click, and refreshing the adapter. But for the ImageView to change, after clicking it I need to leave the Activity and enter again. It doesn't refresh instantly. Why, and how can I change that? I've tried lots of different versions, so far nothing works. My ListViewAdapter extends BaseAdapter. Thank you!
public class ListViewAdapterKeysAToZ extends BaseAdapter {
private ArrayList<KeyTagIntern> keyTags;
private ObservableArrayList<KeyTagIntern> list;
private Context context;
TextView name;
TextView place;
ImageView star, favoriteStar;
public ListViewAdapterKeysAToZ(Context context, ObservableArrayList<KeyTagIntern> list)
{
this.context = context;
this.list = list;
keyTags = new ArrayList<>();
for (KeyTagIntern keytag : list) {
keyTags.add(keytag);
}
//(....)
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
KeyTagIntern key = (KeyTagIntern) getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_keys, parent, false);
}
name = (TextView) convertView.findViewById(R.id.text_keylist_item);
name.setText(key.getName());
place = (TextView) convertView.findViewById(R.id.text_keylist_item_place);
place.setText(key.getPlace());
star = (ImageView) convertView.findViewById(R.id.right_icon_keylist_item);
favoriteStar = (ImageView) convertView.findViewById(R.id.right_icon_keylist_item_favorite);
if (key.isFavorite())
{
star.setVisibility(View.INVISIBLE);
favoriteStar.setVisibility(View.VISIBLE)
favoriteStar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// This makes key.isFavourite() = false for the next time
Paper.book().delete(FAVORIT + String.valueOf(key.getKeyTagID()));
//Since notifyDataSetChanged() didn't work for me, I tried this - but no change
int index = list.indexOf(key);
list.remove(index);
list.add(index, key);
keyTags = new ArrayList<>();
for (KeyTagIntern keytag : list) {
keyTags.add(keytag);
}
notifyDataSetChanged();
}
});
}
// Then do the opposite for if (!key.isFavourite())
Und hier das xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_item_keys"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/MiddleDarkGrey">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/btn_list_item_keys"
android:layout_width="match_parent"
android:layout_height="#dimen/height_list_item"
android:layout_marginBottom="3dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="3dp"
android:background="#drawable/white_list_item"
android:paddingLeft="13dp"
android:paddingRight="10dp">
<ImageView
android:id="#+id/icon_keylist_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:visibility="visible"
app:srcCompat="#drawable/ic_key" />
<ImageView
android:id="#+id/icon_reserved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/chb_add_key"
android:layout_centerVertical="true"
android:visibility="invisible"
app:srcCompat="#drawable/ic_reservate_orange" />
<ImageView
android:id="#+id/icon_taken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/chb_add_key"
android:layout_centerVertical="true"
android:visibility="invisible"
app:srcCompat="#drawable/ic_taken_red" />
<TextView
android:id="#+id/text_keylist_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/icon_keylist_item"
android:layout_toRightOf="#+id/icon_keylist_item"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:text="Text"
android:textColor="#color/DarkGrey"
android:textSize="#dimen/text_list_item" />
<TextView
android:id="#+id/text_keylist_item_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_keylist_item"
android:layout_alignLeft="#+id/text_keylist_item"
android:layout_marginLeft="2dp"
android:layout_marginBottom="5dp"
android:text="Where is the key?"
android:textColor="#color/DarkGrey"
android:textSize="#dimen/text_list_item_sub" />
<ImageView
android:id="#+id/right_icon_keylist_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
app:srcCompat="#drawable/ic_fav_green" />
<ImageView
android:id="#+id/right_icon_keylist_item_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_fav_chosen"
android:visibility="invisible"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I think your approach should be something like this.
rather hiding and displaying an image just change source of it!
if (key.isFavorite())
{
favoriteStar.setImageResource(R.drawable.aaa);
favoriteStar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
favoriteStar.setImageResource(R.drawable.bbb);
// and vice-versa
and I don't thing you will be needing notifyDataSetChanged(); as you are making no changes in the Listdata actually!
In the end the answer was rather stupid, as it is so often, and you guys couldn't have helped me since I excluded the code at the beginning of my adapter class (added it now). It actually worked the whole time, but I didn't see it, since the listitem at the very end of the list was changed, not the selected one. This was due to me declaring the variables at the beginning of the adapter class, rather than inside the getView method.
I changed it to this and now it works perfectly:
public class ListViewAdapterKeysAToZ extends BaseAdapter {
private ArrayList<KeyTagIntern> keyTags;
private ObservableArrayList<KeyTagIntern> list;
private Context context;
public ListViewAdapterKeysAToZ(Context context, ObservableArrayList<KeyTagIntern> list) {
this.context = context;
this.list = list;
keyTags = new ArrayList<>();
for (KeyTagIntern keytag : list) {
keyTags.add(keytag);
}
// (...)
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
KeyTagIntern key = (KeyTagIntern) getItem(position);
TextView name;
TextView place;
ImageView star, favoriteStar;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_keys, parent, false);
}
name = (TextView) convertView.findViewById(R.id.text_keylist_item);
name.setText(key.getName());
place = (TextView) convertView.findViewById(R.id.text_keylist_item_place);
place.setText(key.getPlace());
star = (ImageView) convertView.findViewById(R.id.right_icon_keylist_item);
favoriteStar = (ImageView) convertView.findViewById(R.id.right_icon_keylist_item_favorite);
if (key.isFavorite()) {
star.setVisibility(View.INVISIBLE);
favoriteStar.setVisibility(View.VISIBLE);
}else {
star.setVisibility(View.VISIBLE);
favoriteStar.setVisibility(View.INVISIBLE);
}
star.setOnClickListener(v -> {
key.setFavorite(true);
Paper.book().write(FAVORIT + String.valueOf(key.getKeyTagID()), true);
notifyDataSetChanged();
});
favoriteStar.setOnClickListener(v -> {
key.setFavorite(false);
Paper.book().delete(FAVORIT + String.valueOf(key.getKeyTagID()));
notifyDataSetChanged();
});
I found many similar questions on StackOverflow, but can't seem to figure out this issue.
I'm trying to bind an ObservableCollection to a ListView so that when the contents of the collection change the ListView will automatically update. Unfortunately, when elements are added to the collection (via button click), the ListView isn't being updated. How can I make the ListView reflect the current contents of the collection?
Note: It seems that if I force layout with listView.RequestLayout(); the listView will contain the correct number of items. However, it won't necessarily contain the right items. For example, if I delete the first item and add a new one at the end, forcing a layout has no effect. If I force the layout after deleting the first item, then again after adding one at the end, the contents are correct.
Activity code
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
ObservableCollection<UserTask> allTasksCollection = new ObservableCollection<UserTask>();
while(count < 6)
{
allTasksCollection.Add(new UserTask("Task number " + count));
count++;
}
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Bind listview to all tasks
ListView listView = FindViewById<ListView>(Resource.Id.allTasksListView);
UserTaskListAdapter adapter = new UserTaskListAdapter(this, allTasksCollection);
listView.Adapter = adapter;
// Button click should add a new task and remove the first task.
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate {
allTasksCollection.Add(new UserTask("Task number " + count));
button.Text = string.Format("{0} tasks!", count++);
};
}
}
Adapter
public class UserTaskListAdapter : BaseAdapter<UserTask>
{
Activity context;
ObservableCollection<UserTask> list;
public UserTaskListAdapter(Activity _context, ObservableCollection<UserTask> _list)
: base()
{
this.context = _context;
this.list = _list;
}
public override int Count
{
get { return list.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override UserTask this[int index]
{
get { return list[index]; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.UserTaskRowItem, parent, false);
UserTask item = this[position];
view.FindViewById<TextView>(Resource.Id.Title).Text = item.Name;
return view;
}
}
Main XAML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rootLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/allTasksContainer">
<Button
android:id="#+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/allTasksListView" />
</LinearLayout>
</LinearLayout>
ListItem XAML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayoutHorizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CheckboxContainer">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkboxSelect" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextContainer">
<TextView
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Title" />
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DueDate" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Try to call NotifyDataSetChanged() on your adapter after updating its items, AFAIK ListView in Android doesn't observe the changes on your ObservableCollection:
Try this code:
button.Click += delegate {
allTasksCollection.Add(new UserTask("Task number " + count));
button.Text = string.Format("{0} tasks!", count++);
adapter.NotifyDataSetChanged();
};
You can also try to add a litener to your ObservableCollection, in your adapter, change the constructor to this:
this.list.CollectionChanged += (sender,args) => { NotifyDataSetChanged(); };
Take a look at the INotifyPropertyChanged interface. It will warn the listview that variables within an object within the listview have changed.
https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_bindings_to_mvvm/
Edit: misread your question. My bad.
I have a ListView in one of my activities that I have bound to an ArrayList using a custom ArrayAdapter. I have set an OnItemClickListener to the ListView which should call a method that starts another activity. However, I find that when I click on the ListView items, it only sometimes works. Sometimes it will start the activity as it should; other times it seems to detect the click (the ripple effect appears on the list item) but does nothing; other times it doesn't even appear to detect the click (the ripple effect doesn't appear).
I've tried all the usual suggestions that I've come across: blocking descendants on the parent view item, setting clickable and focusable to false on all the components of the item views, setting isEnabled to return true in the custom adapter, etc, but the behavior remains the same. Any help appreciated. Here is the relevant code:
Activity containing the ListView:
public class ViewCollectionActivity extends AppCompatActivity {
private final String className = this.getClass().getSimpleName();
private CollectionHandler collectionHandler;
private Context context;
private ArrayList<Game> displayedCollection;
private GameCollectionAdapter collectionAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_collection);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
context = this;
collectionHandler = CollectionHandler.getInstance(this);
TextView view = null;
if (collectionHandler.getDisplayedCollection().size() > 0) {
view = (TextView) findViewById(R.id.no_items_textview);
view.setVisibility(View.GONE);
}
String currentDate = collectionHandler.getDateLastSynchronised();
view = (TextView) findViewById(R.id.last_updated_textview);
view.setText("Last synchronised: " + currentDate + " Total games: " + String.valueOf(collectionHandler.getDisplayedCollection().size()));
collectionAdapter = collectionHandler.getCollectionAdapter();
ListView listView = (ListView) findViewById(R.id.collection_list_view);
listView.setAdapter(collectionAdapter);
AdapterView.OnItemClickListener collectionItemClickListener = new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
launchGameDetailsActivity(position);
}
};
listView.setOnItemClickListener(collectionItemClickListener);
}
public void launchGameDetailsActivity(int position){
Log.d(className,"Starting lauchGameDetailsActivity method");
collectionHandler.setSelectedGame(position);
Intent intent = new Intent(this,ViewGameDetailsActivity.class);
startActivity(intent);
Log.d(className, "Ending lauchGameDetailsActivity method");
}
The XML for the activity:
<?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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.bleachedlizard.ludome.viewcollection.ViewCollectionActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Synchronise Collection"
android:onClick="synchroniseCollection"/>
<TextView
android:id="#+id/last_updated_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Last synchronised: "
android:textAlignment="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display Collection"
android:visibility="gone"
android:onClick="displayCollection"/>
<ListView
android:id="#+id/collection_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/no_items_textview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="You have no items in your collection."
android:textAlignment="center"
android:textSize="20sp"/>
</LinearLayout>
The XML for the item views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/collection_item_layout"
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:id="#+id/collection_item_image"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#drawable/testimage"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
<TextView
android:id="#+id/collection_item_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="16dp"
android:singleLine="false"
android:textColor="#android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textIsSelectable="false"/>
<TextView
android:id="#+id/collection_item_plays"
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:padding="8dp"
android:textColor="#android:color/darker_gray"
android:text="Plays: 0"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textIsSelectable="false"/>
</LinearLayout>
The code for the custom adapter:
public class GameCollectionAdapter extends ArrayAdapter<Game> {
private ArrayList<Game> collection;
public GameCollectionAdapter(Context context, int resource, ArrayList<Game> collection){
super(context, resource, collection);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout gameView = (LinearLayout) convertView;
LayoutInflater mInflater = LayoutInflater.from(getContext());
if (gameView == null) {
gameView = (LinearLayout) mInflater.inflate(R.layout.collection_item_view, null);
}
//Game game = collection.get(position);
Game game = super.getItem(position);
if (game != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView gameTitle = (TextView) gameView.findViewById(R.id.collection_item_name);
TextView numOfPlays = (TextView) gameView.findViewById(R.id.collection_item_plays);
ImageView thumbnail = (ImageView) gameView.findViewById(R.id.collection_item_image);
// check to see if each individual textview is null.
// if not, assign some text!
if (gameTitle != null){
gameTitle.setText(game.getTitle());
}
if (numOfPlays != null){
numOfPlays.setText("Plays: " + String.valueOf(game.getNumOfPlays()));
}
if (thumbnail != null){
thumbnail.setImageBitmap(game.getThumbnail());
}
}
// the view must be returned to our activity
return gameView;
}
#Override
public boolean isEnabled(int position) {
return true;
}
}
I discovered what was causing the problem: the way I had set up the array that backed the ListView meant that it was downloading and storing the Bitmaps for every element in the array all the time. Once I changed the implementation so that it only downloaded the images as the ListView required them, then that seemed to improve performance and the onClickListener started to work fine.
The implementation I used was the exact same one shown here:
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
I think the issue is due to the position of the item selection whenever you click you have an list position which is passed to your method launchGameDetailActivity(int position) check with log or toast on item click what all the position you are getting do the needful.
Here is my code try this like this if it helps.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(RecipeClass.this, "Position is" + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RecipeClass.this, RecipeIngredients.class)
intent.putExtra("position", position);
startActivity(intent);
}
Check your arraylist value also whether they are not null.
I have a ListView and a custom adapter. Now i have red posts about getView() method being called multiple times, and most of them have to do with the wrap_content feature of the ListView.
I have changed my height, so that it can be match_parent but still, the method is being called.
I think it has something to do with adding TextViews dynamically in the adapter, and i don't know how to do it, so it works properly.
If there is an alternative, i am opened to it, the only reason why i put TextViews is so that i can have letters written in different colors.
Here is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
Row current = mList.get(position);
/* if the given channel row view is not being updated*/
if (v == null)
{
/* inflate layout */
LayoutInflater vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item2, null,false);
}
v = setLinearLayout(v,current);
return v;
}
Here is the setLinearLayout() method:
private View setLinearLayout(View v,Row current) {
ArrayList<Integer> winResults = current.checkNumbers(mResult,mType);
int numbers[] = current.getNumbers();
int N = Global.getNumberOfNumbers(mType);
boolean FLAG_SET = false;
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
Log.d("PAVLE",""+i+" row is: "+current.getStringNumbers());
// create a new textview
final TextView rowTextView = new TextView(v.getContext());
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,75);
rowTextView.setTextSize(24.0f);
rowTextView.setPadding(8, 8, 8, 8);
rowTextView.setGravity(Gravity.CENTER);
rowTextView.setBackgroundColor(Color.BLACK);
rowTextView.setTypeface(null, Typeface.BOLD);
rowTextView.setLayoutParams(lp);
if(mType == R.string.sans_topu && i == 5 && !FLAG_SET){
i--;
rowTextView.setTextColor(Color.WHITE);
rowTextView.setText("+");
FLAG_SET = true;
}
else {
// set some properties of rowTextView or something
if (mWin == Global.WIN || mWin == Global.LOSE) {
if (winResults.contains(numbers[i]))
rowTextView.setTextColor(Color.GREEN);
else rowTextView.setTextColor(Color.RED);
} else {
rowTextView.setTextColor(Color.WHITE);
}
if (numbers[i] < 10) {
rowTextView.setText("0" + numbers[i]);
} else rowTextView.setText("" + numbers[i]);
}
// add the textview to the linearlayout
LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll_item);
ll.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}
final TextView prize = new TextView(v.getContext());
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,75);
prize.setTextSize(24.0f);
prize.setPadding(8, 8, 8, 8);
prize.setGravity(Gravity.CENTER);
prize.setBackgroundColor(Color.BLACK);
prize.setTypeface(null, Typeface.BOLD);
prize.setTextColor(Color.WHITE);
prize.setLayoutParams(lp);
try {
String cash = findCorretAmmount(winResults, numbers);
prize.setText(cash);
mTotal.append(" "+cash);
}
catch (Exception e){
e.printStackTrace();
}
LinearLayout ll = (LinearLayout) v.findViewById(R.id.ll_item);
ll.addView(prize);
return v;
}
And a little bit of XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvRandomTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:paddingRight="8dp"
android:text="#string/your_ticket_numbers_"
android:textColor="#android:color/black"
android:textSize="28sp"
android:textStyle="bold"
/>
<View
android:id="#+id/divider4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/tvRandomTextView"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:background="#android:color/darker_gray"/>
<ListView
android:id="#+id/lvRowList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/divider4"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
>
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/delete"/>
<Button
android:id="#+id/btnShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/btn_share"/>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/done"/>
</RelativeLayout>
<TextView
android:id="#+id/tvResultLink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:autoLink="web"
android:gravity="center"
android:linksClickable="true"
/>
Also worth mentioning is that list_item2(LinearLayout) has height of 100dp, it's fixed size.
I think its not about textview.
The getView is always called multiple times. If you have 10 items on the screen to be shown, the getView going to be called 15 times, because the android creating views that are not on the screen. It`s good, because when the user start scrolling, it is not going to lagging.
After the user left the item, the view get`s recycle and reused by the adapter. Lets say, you have a list with 10000000 item, but you have 5 item on the screen at all time. In this case - to save power, and improve performance - the android going to create 10 list item, and this 10 item is going to recylce and refresh by content.
ViewHolder pattern
Please read this and use this patter to improve your code performance:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Google about ListView:
http://developer.android.com/guide/topics/ui/layout/listview.html
Tutorials:
http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/
For Example, like Google+ there are one big size and two small size display alternately until reach the last view. So is it possible to do this.
Like this picture:
http://upic.me/show/42297666
Google+ show each item in different size.
Thanks.
Yes.
1) You create a layout (my_layout) take care of alignments
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="1dp"
android:layout_gravity="left"
android:orientation="horizontal" >
<ImageView
android:id="#+id/contentImage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="5dp"
android:visibility="invisible"
android:src="#drawable/browse_product_thumbnail" />
<ImageView
android:id="#+id/contentImage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="5dp"
android:visibility="invisible"
android:src="#drawable/browse_product_thumbnail" />
2) Now write and adapter for this and getView() method . Something like below code
public class ItemListAdapter extends ArrayAdapter<LaunchItem> {
private int _resource;
private LayoutInflater _inflater;
private static Launcher _browse;
private static final String tag = "Adapterr";
public ItemListAdapter(Launcher activity , int resourceId, List<LaunchItem> objects)
{
super( activity, resourceId, objects );
_resource = resourceId;
_inflater = LayoutInflater.from(activity);
this._browse = activity;
}
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
convertView = ( LinearLayout ) _inflater.inflate( _resource, null );
String imagePath = "";
String name = "";
LaunchItem item = getItem( position );
}
}
You can decide what to display and their visibility on getView .
3) Use the custom adapter in your code .
List<LaunchItem> lList = new ArrayList<LaunchItem>();
lList.add(new LaunchItem(image1,image2 );
ItemListAdapter data = new ItemListAdapter (this,R.layout.my_layout,lList);
view.setAdapter(data);
-Preeya