I created an android actionbar tab application, with 4 tabs, one of the tab is a
fragment class which is used to contain a list view,to display a list of static information:
However, I have an error at this line of code:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
These are my 2 xml files used to create a list view:
listoflockers.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
lockerinforow.xml
<?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="horizontal"
>
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/email"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<TextView
android:id="#+id/cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
public class myLockerFragment.java{
.....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mylocker, container, false);
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>
();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Locker No : " + i);
hm.put("cur","Location : " + currency[i]);
hm.put("size","Size : " + size[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur","size" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur,R.id.size};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) rootView.findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
return rootView;
}
Please help
I am confused at this line:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
giving me the error:
The method getBaseContext() is undefined for the type myLockerFragment
you can use getActivity() inside a Fragment to get the context that holds the Fragment itself
Do this-
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,
R.layout.listoflockers, from, to);
SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), aList, R.layout.listoflockeres, from, to);
You need to use the getActivity() method inside Fragments
Besides using getBaseContext() you need to use the getActivity() in Fragments to get the context of the Fragment.
Try out as below:
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,R.layout.listoflockers, from, to);
Related
I will put a picture on top of a custom list view for the header image, but the image is repeated, how can I make it can't click and non-repeatable?
this is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/abs__background_holo_light" >
<ImageView
android:id="#+id/detailImage"
android:layout_width="wrap_content"
android:layout_height="140dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/pantaigoacina" />
<RelativeLayout
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/detailImage"
android:background="#color/abs__background_holo_light" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="5dip"
android:layout_marginTop="20dip"
android:background="#color/abs__background_holo_light"
android:padding="5dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="15dip"
android:layout_height="15dip"
android:src="#drawable/arrow"
android:contentDescription="#string/app_name"/>
</LinearLayout>
<TextView
android:id="#+id/NamaLokasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Pantai Sipelot"
android:textColor="#040404"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/detailLokasi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/NamaLokasi"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Deskripsi Konten, Lokasi Berada di Desa Wilayah Malang"
android:textColor="#343434"
android:textSize="12sp" />
</RelativeLayout>
</RelativeLayout>
this is my adapter :
String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), aList, R.layout.fragment_detail, from,
to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
Remove the image form the xml.
ImageView iv = new ImageView(getActivity());
// set image to imageview
ListView list = getListView();
list.addHeaderView(headerView);
The docs
public void addHeaderView (View v)
Added in API level 1
Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Parameters
v The view to add.
Edit:
#Override
public void onActivityCreated(Bundle savedInstanceStat)
{
super.onActivityCreated(savedInstanceState);
list = getListView();
ImageView imageHeaderView = new ImageView(getActivity());
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pantaigoacina));
list.addHeaderView(imageHeaderView);
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < DaerahWisata.pantai.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("pantai", DaerahWisata.pantai[i]);
hm.put("detail", "Lokasi : \r\n" + DaerahWisata.detailp[i]);
aList.add(hm);
}
String[] from = { "pantai", "detail" };
int[] to = { R.id.NamaLokasi, R.id.detailLokasi };
SimpleAdapter adapter = new SimpleAdapter(getActivity()
.getBaseContext(), aList, R.layout.fragment_detail, from,
to);
setListAdapter(adapter);
}
Edit, if want to set imageview from other xml layout :
list = getListView();
Context context = getListView().getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image_header, null);
list.addHeaderView(view);
Try below code:-
ImageView imageHeaderView = new ImageView(this);//use getActivity() on fragment
imageHeaderView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.deckblatt));
myList.addHeaderView(imageHeaderView);
I have a fragment with 4 tabs, in one tab i need to show a listview. I have got this far on my code but have a couple errors and i'm not sure if i'm just not doing it right or if i'm missing something simple. Here is my code that i'm using
fragment activity
public class SupportFragment extends SherlockFragment{
String[] supporters = new String[] {
"Gulf Shores",
"Central",
"Spanish Fort",
"Jackson Heights",
"Summerdale",
"Atlas",
"Robertsdale",
"Eastern Shore"
};
int[] images = new int[] {
R.drawable.gulfshores,
R.drawable.central,
R.drawable.spanishfort,
R.drawable.jacksonheights,
R.drawable.summerdale,
R.drawable.atlas,
R.drawable.robertsdale,
R.drawable.easternshore
};
String[] church = new String[]{
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ",
"Chruch of Christ"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.tab_support_layout, null);
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("sup", "Supporters : " + supporters[i]);
hm.put("chur","Church : " + church[i]);
hm.put("icon", Integer.toString(images[i]) );
aList.add(hm);
}
String[] from = {"sup", "chur", "icon"};
int[] to = {R.id.icon, R.id.sup, R.id.chur};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
return root;
}
}
I am getting an error on these two lines of code from this
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
and
ListView listView = (ListView) findViewById(R.id.listview);
the error i'm getting is:
The method getBaseContext() is undefined for the type SupportFragment
The method findViewById(int) is undefined for the type SupportFragment
xml for listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/tableback" >
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
and the listview layout
<?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="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/sup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<TextView
android:id="#+id/chur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
any help you can give is greatly appreciated
You do not have a FragmentActivity you have a Fragment, so of course you can't get the Context with getBaseContext(), you have to use getActivity() instead.
You also have to inflate a View to use findViewById, if you want to do that in a Fragment. However if you want to use Tabs you need a FragmentActivity which hadels your Fragments.
You can inflate a View like this in your fragments onCreateView() method:
View view = inflater.inflate(R.layout.myLayout, null);
Then you can call findViewById like this:
[...]view.findViewById(...);
I have been playing with the list activity tutorial here:
http://developer.android.com/resources/tutorials/views/hello-listview.html
which tells you to start off extending List activity.
by public class Main extends ListActivity {
Which is based on inflating a textview only layout.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
If I want to customize the layout more by adding images,and an extra linear layout above the list adapter etc- is it possible using this method- if so how do I do it?
It is possible by using a SimpleAdapter.
Here is an example :
// Create the item mapping
String[] from = new String[] { "title", "description" };
int[] to = new int[] { R.id.title, R.id.description };
Now "title" is mapped to R.id.title, and "description" to R.id.description (defined in the XML below).
// Add some rows
List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "First title"); // This will be shown in R.id.title
map.put("description", "description 1"); // And this in R.id.description
fillMaps.add(map);
map = new HashMap<String, Object>();
map.put("title", "Second title");
map.put("description", "description 2");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);
setListAdapter(adapter);
This is the corresponding XML layout, here named row.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
I used two TextViews but it works the same with any kind of view.
I keep getting this error:
Unable to start activity ComponentInfo{com.Nostradamus/com.Nostradamus.Contactenlijst}: java.lang.NullPointerException
And I don't know what I do wrong.
It has something to do with the last code:
lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
This is my java code:
public class Contactenlijst extends Activity {
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] {"voornaam", "achternaam", "geboortedatum", "adres", "postcode", "woonplaats", "email", "telefoon"};
int[] to = new int[]{R.id.voornaam, R.id.achternaam, R.id.geboortedatum, R.id.adres, R.id.postcode, R.id.woonplaats, R.id.email, R.id.telefoon};
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Log.d("test","test2");
// Get the data (see above)
JSONObject json = Database
.getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");
Log.d("test","test3");
try {
JSONArray contactinfo = json.getJSONArray("contactlijst");
Log.d("test","test4");
// Loop the Array
for (int i = 0; i < contactinfo.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = contactinfo.getJSONObject(i);
map.put("voornaam", e.getString("staff_name"));
map.put("achternaam", e.getString("staff_lastname"));
map.put("geboortedatum", e.getString("staff_dateofbirth"));
map.put("adres", e.getString("staff_address"));
map.put("postcode", e.getString("staff_address_postal"));
map.put("woonplaats", e.getString("staff_address_city"));
map.put("email", e.getString("staff_email"));
map.put("telefoon", e.getString("staff_phone"));
mylist.add(map);
Log.e("test",map.toString());
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
}
}
And these are my xmls:
Contactenlijst
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/voornaam"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/achternaam"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/geboortedatum"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/adres"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/postcode"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/woonplaats"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/email"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/telefoon"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
Contactview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data">
</TextView>
</LinearLayout>
and Listitem
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
you didn't set the layout using
setContentView(R.layout.yourlayout);
so without add layout you are using your listview that's why you getting NullPointerException.
add this above line after
super.onCreate(savedInstanceState);
or before the use listview object
You need to call setContentView(...) before lv = (ListView) findViewById(R.id.list);
setContentView(...) inflates the XML file for your layout. If you don't do this, then lv will be null.
I've been doing this for a few months now and I've found it almost impossible to find an example where the ListView is a ListView not a ListActivity. In my program all I'm wanting to do is have half the screen a ListView and the other half something else so the ListView can be moved around in the layout. Does anyone know any such examples?
I have created implementation of ListView, not ListActivity.
list_produk.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:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvListProduk"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.98"
tools:listitem="#layout/list_produk_row" >
</ListView>
<Button
android:id="#+id/btnListProdukBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back_" />
</LinearLayout>
list_produk_row.xml:
<?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" >
<TextView
android:id="#+id/tvProdukName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
ListProduk.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_produk);
String[] from = new String[] {"prd_name"};
int[] to = new int[] {R.id.tvProdukName};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("prd_name", "BlackBerry");
fillMaps.add(map);
map = new HashMap<String, String>();
map.put("prd_name", "Android");
fillMaps.add(map);
map = new HashMap<String, String>();
map.put("prd_name", "iPhone");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_produk_row, from, to);
ListView listView = (ListView) findViewById(R.id.lvListProduk);
listView.setAdapter(adapter);
}
list_produk_row.xml, that contains a TextView, is the layout for list item of ListView.