I am trying to get multiple items checked in a list view I have in my dialog box.
This is the Java code:
String names[] ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
LayoutInflater inflater = activity.getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.test, null);
alertDialog.setView(convertView);
alertDialog.setTitle("List");
ListView lv = (ListView) convertView.findViewById(R.id.my_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemsCanFocus(false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, names);
lv.setAdapter(adapter);
alertDialog.show();
It shows the list in my dialog box okay, but when I click/press an item nothing happens. I don't see any tick or anything visually to indicate it's been selected/checked.
Here's my XML code:
<?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">
<ListView
android:id="#+id/my_list"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
What am I doing wrong?
You need to change :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_list_item_1, names);
To :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_list_item_mutiple_choice, names);
Related
I want to show a list of cars in an Android Dialog and it keeps showing nothing.
Note: purchasedCars.getCars() is not empty
Here is the code:
findViewById(R.id.buy).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(BuyCarActivity.this);
View dialogView=getLayoutInflater().inflate(R.layout.dialog_list,null);
builder.setView(dialogView);
ListView listView = dialogView.findViewById(R.id.listView2);
listView.setAdapter(new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, purchasedCars.getCars()));
builder.show();
}
});
check your inflated layout its seems to look like this one
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
and can you test if you change purchasedCars.getCars()
to - > new String[]{"test","app"}
for example
listView.setAdapter(new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, new String[]{"test","app"}));
OR
List<String> list = new ArrayList<>();
list.add("test");
list.add("app");
listView.setAdapter(new ArrayAdapter<>(view.getContext(), android.R.layout.simple_list_item_1, list));
check if thats work if yes .. you need to take a look for what purchasedCars.getCars() return .
I am trying this cool library that uses Ken Burn effect here (his github page)
I got it to work but I cant add different Icons on different rows. I tried to use Mkyongs CustomAdaptor example but what he used is a layout with TextView and ImageView. What I am using is ListView. I am already able to change the text font and size of the ListView but lost trying to add icons. How do you do this?
Below is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#drawable/blur4"
tools:context="com.gio.hia.hiarewired.NoBoringActionBarActivity">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
<FrameLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="#dimen/header_height">
<com.gio.hia.hiarewired.KenBurnsView
android:id="#+id/header_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/picture0" />
<ImageView
android:id="#+id/header_logo"
android:layout_width="#dimen/header_logo_size"
android:layout_height="#dimen/header_logo_size"
android:layout_gravity="center"
android:src="#drawable/ic_header_logo" />
</FrameLayout>
</FrameLayout>
This is my Activity Class
private void setupListView() {
ArrayList<String> FAKES = new ArrayList<String>();
//for (int i = 0; i < 1000; i++) {
FAKES.add("Row 1");
FAKES.add("Row 2");
FAKES.add("Row 3");
FAKES.add("Row 4");
//}
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
//mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_config, FAKES));
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { ...
I used a custom R.layout.list_text_config to change the font
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:textColor="#color/colorWhite"/>
In his example he used a layout inflater How do I use my Custom Adapter with a layout inflator or am I looking at the problem on the wrong direction?
I usually just do this
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
but how do I do that when he used a layout inflater like below
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
Like #Arslan said this can be done with a custom adapter. You are actually doing it correct
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
That actually creates the list with the icons. What you need to add to that is the header image to the mPlaceHolderView but instead of using mListView use the list that you created with your "custom adapter". I placed the modified line of code from the original line of code so you can easily see the difference.
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, list, false);
//mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
list.setAdapter(adapter);
Now and since your no longer using the mListView you should probably lookout for instances of mListView and replace it with your list
I have a list view on this XML:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_above="#+id/txtMsg" />
I want to add items to the list view in my code. This is my code:
ListView mesList = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter;
if(messages.size()>0) {
adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, messages);
}
messages is an ArrayList of String and is not empty. For some reason the list view is empty on the screen. What should I put instead of android.R.layout.activity_list_item? I think this is my problem. Thank you in advance!
set adapter of listview
mesList.setAdapter(adapter);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, messages);
Hi i'm trying to add a relative view to a merge adapter but it's currently scrolling separetly to the lists so does anyone know how i can add a relative layout with an image view and a text view to a merge adapter?
my aim is to have it look like this
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
and to have this all scroll as if it was all one list
heres my attempt so far
arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
arrayAdapter3 = new ArrayAdapter<String>(this, R.layout.single_item, newsList3);
ListView list = getListView();
list.setTextFilterEnabled(true);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View header = inflater.inflate( R.layout.redcell, list, false);
setListAdapter (arrayAdapter);
adapter = new MergeAdapter();
adapter.addView(header);
adapter.addAdapter(arrayAdapter);
adapter.addView(header);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
}
redcell.xml
<?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="wrap_content"
android:orientation="vertical"
android:id="#+id/redcelllayout" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="#drawable/titlerect" />
<TextView
android:id="#+id/redheadertext"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:paddingLeft="15dp"
android:paddingTop="3dp"
android:textColor="#color/white"
android:textSize="15dp"
android:textStyle="bold"/>
</RelativeLayout>
Instead of creating them in the program, create an xml file for that view, inflate it, then add it to the adapter.
Like this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header1 = inflater.inflate(R.layout.redbar_title);
View header2 = inflater.inflate(R.layout.redbar_title);
View header3 = inflater.inflate(R.layout.redbar_title);
adapter = new MergeAdapter();
adapter.addView(header1);
adapter.addAdapter(arrayAdapter);
adapter.addView(header2);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header3);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
Set each list layout_height to wrap_content, and wrap everything within a ScrollView.
Perhaps it would be easier if you could make your headers part of the adapter itself, as separators, and use a single list instead of 3.
I have a ListView, which is backed by an ArrayAdapter. They are defined as such:
ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item);
listView.setAdapter(listAdapter);
list_item.xml contains the following:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="16sp">
</TextView>
What I would like to do when adding an item to the list, is edit the TextView associated with the item, like changing it's colour for example. How would I go about doing this?
You could override the method getView() of your ArrayAdapter (link to the reference) for example:
ListView listView = (ListView)findViewById(R.id.listview);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.list_item){
public View getView (int position, View convertView, ViewGroup parent){
View v = super.getView(position,convertView,parent);
TextView tx = (TextView) v;
//do something with tx
return v;
}
};
listView.setAdapter(listAdapter);
You should go watch this Google I/O talk: http://www.google.com/events/io/2010/sessions/world-of-listview-android.html it explains the basics of ListView and how you would go about doing these sorts of things.