Naar content
Trending apps
  • Inbox by Gmail

  • Maps: Navigatie en OV

  • WhatsApp Messenger

  • Messenger

  • Facebook

Trending games
  • Minecraft Earth

  • Dr. Mario World

  • Harry Potter: Wizards Unite

  • Breaking Bad: Criminal Elements

  • The Elder Scrolls: Blades

Trending smartphones
  • Samsung Galaxy Note 10 Lite

  • Realme X2 Pro

  • Samsung Galaxy S20 Ultra

  • Moto G8 Plus

  • Samsung Galaxy A51

Nieuwste tablets
  • Samsung Galaxy Tab S6

  • Samsung Galaxy Tab A 10.5

  • Samsung Galaxy Tab S4

  • Samsung Galaxy Tab S3 9.7

  • Asus Zenpad 3S 10

tim687

tim687

  • Lid sinds 11 juli 2011
  • Berichten 418
  • Reputatie 0
  • #1
  • 22 oktober 2013
  • 17:39

Beste ontwikkelaars,
Ik ben momenteel zwaar geïrriteerd omdat ik een ListView met icoontjes wil hebben. Dit is blijkbaar heel moeilijk en niks lukt (geen duidelijke uitleg)

Ik heb dit als de code voor mijn normale ListView:

ListView wordt geïnnitialiseerd…. (als lvWiFi)


Main activity:

lvWiFi = (ListView) findViewById(R.id.lvWiFi);
static ArrayList<String> AP = new ArrayList<String>();
ArrayAdapter<String> adp;
protected void onCreate(Bundle ............){
.........
adp = new ArrayAdapter<String>(this, R.layout.item, AP);// android.R.layout.simple_list_item_1
lvWiFi.setAdapter(adp);
lvWiFi.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(
					@SuppressWarnings("rawtypes") AdapterView parent,
					View view, int position, long id) {
				fullLine = lvWiFi.getItemAtPosition(position).toString();
				fullLine = fullLine.replace("  ", "\n");
				selected = lvWiFi.getItemAtPosition(position).toString();
				selected = selected.substring(0, selected.indexOf("[") - 1);
				showDialog();
				return true;
			}
		});
		lvWiFi.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(
					@SuppressWarnings("rawtypes") AdapterView parent,
					View view, int position, long Clickid) {
				// TODO Auto-generated method stub
				fullLine = lvWiFi.getItemAtPosition(position).toString();
				fullLine = fullLine.replace("  ", "\n");
				selected = lvWiFi.getItemAtPosition(position).toString();
				selected = selected.substring(0, selected.indexOf("[") - 1);
				Log.d("StartupScreen", selected);
				showInfoDialog();
				// infoDialog.GetInfo();
			}
		});
}
public static Bitmap WiFiSignalStrength(int strength, int passProtected){
		Bitmap SignalIcon = null;
		Bitmap bmp = null;
		Bitmap locked = null;
		
		switch (strength){
		
		case 0:
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_no_signal);
			break;
		
		case 1:
			
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_weakest);
			break;	
		case 2:
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_stronger);
			break;
		case 3:
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_strongest);
			break;
		}
		
		switch (passProtected){
		case 0:
			locked = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_lock_lock);
			break;
		
		}
		
		if (bmp != null){
		 Bitmap bmOverlay = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
	        Canvas canvas = new Canvas(bmOverlay);
	        canvas.drawBitmap(bmp, new Matrix(), null);
	        	if(locked != null){
	        		canvas.drawBitmap(locked, 0, 0, null);
	        	}
	        	SignalIcon = bmOverlay;
		
		
	}
		
		
		return SignalIcon;
	}
public void ScanForPoints(View view) {
		AP.clear();
		adp.clear();
AP.add(WiFiSignalStrength(signalStrght, passProtected) + results.SSID + "  " + results.capabilities + "  BSSID: "
						+ results.BSSID.toString() + "  Frequency: "
						+ results.frequency / 1000 + " GHz" + "  Level (dB): "
						+ String.valueOf(results.level));
				}

item.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    
<ImageView 
    android:id="@+id/itemImageView" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:layout_alignParentLeft="true"/>
    
<TextView 
    android:id="@+id/SSIDname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/itemImageView"
    android:layout_toRightOf="@+id/itemImageView"
    android:textSize="15sp"
    android:padding="5dp"/>
<TextView 
    android:id="@+id/SSIDcapabilities"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/SSIDname"
    android:layout_gravity="center"
    android:gravity="right"
    android:layout_marginTop="2dip"
    android:layout_toRightOf="@+id/itemImageView"
    android:textSize="8dp"
    android:padding="5dp"/>
</RelativeLayout>

Wanneer ik de app uitvoer krijg ik deze foutmelding:
10-22 17:36:41.068: E/AndroidRuntime(22187): FATAL EXCEPTION: main
10-22 17:36:41.068: E/AndroidRuntime(22187): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.AbsListView.obtainView(AbsListView.java:2308)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.ListView.measureHeightOfChildren(ListView.java:1442)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.ListView.onMeasure(ListView.java:1306)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.View.measure(View.java:15696)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.View.measure(View.java:15696)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.View.measure(View.java:15696)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.View.measure(View.java:15696)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.View.measure(View.java:15696)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2225)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1290)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1527)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1190)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4860)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:766)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.Choreographer.doCallbacks(Choreographer.java:575)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.Choreographer.doFrame(Choreographer.java:542)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.os.Handler.handleCallback(Handler.java:725)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.os.Looper.loop(Looper.java:158)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.app.ActivityThread.main(ActivityThread.java:5751)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at java.lang.reflect.Method.invoke(Method.java:511)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at dalvik.system.NativeStart.main(Native Method)
10-22 17:36:41.068: E/AndroidRuntime(22187): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
10-22 17:36:41.068: E/AndroidRuntime(22187): 	at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
10-22 17:36:41.068: E/AndroidRuntime(22187): 	... 36 more


Hoe moet ik een (custom) ListView met icoontjes maken?
Wie kan mij hierbij helpen door een goed begrijpende “handleiding” te schrijven met code die ik makkelijk kan aanpassen. Mijn lontje voor dit onderdeel mbt de icoontjes is erg kort geworden.

Heeeel erg bedankt als jullie mij kunnen helpen!!
Groet,
Tim

Arieblabla

Arieblabla

  • Lid sinds 14 mei 2012
  • Berichten 415
  • Reputatie 0
  • #2
  • 31 oktober 2013
  • 18:05

je moet zelf een custom adapter maken. Ff googelen en je krijgt een goede handleiding.

tim687

tim687

  • Lid sinds 11 juli 2011
  • Berichten 418
  • Reputatie 0
  • #3
  • 31 oktober 2013
  • 18:42

Kan jij er misschien eentje sturen? Ik heb al gezocht maar krijg allemaal onduidelijke handleidingen :-(

Bedankt,
Tim

Arieblabla

Arieblabla

  • Lid sinds 14 mei 2012
  • Berichten 415
  • Reputatie 0
  • #4
  • 1 november 2013
  • 14:34

Op aw zelf staat er eentje. Een van de devTutorials gaat over listviews: http://androidworld.nl/nieuws/devtutorial-7-arraylist-en-itemlayout-via-baseadapter-aan-listview-koppelen/

Kort samengevat:
Je moet een nieuwe class aanmaken, die BaseAdapter extend. Deze moet er ongeveer zo uit komen te zien:

package com.example.example;
public class YourAdapter extends BaseAdapter {
        
        ArrayList<String> mInput;
        LayoutInflater mInflater;
        
        public TweetsListAdapter(Context context, ArrayList<String> input){
                mInput = new ArrayList<String>();
                mInput.addAll(input);
                notifyDataSetChanged();
                mInflater = LayoutInflater.from(context);
        }
        public int getCount() {
                return mInput.size();
        }
        public Tweet getItem(int position) {
                return mInput.get(position);
        }
        public long getItemId(int position) {
                return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.your_listitem, null);
                        holder = new ViewHolder();
                        holder.mTxtvTitle = (TextView) convertView.findViewById(R.id.txtv_your_title);
                        holder.mTxtvSubtitle = (TextView) convertView.findViewById(R.id.txtv_your_subtitle);
                        convertView.setTag(holder);
                } else {
                        holder = (ViewHolder) convertView.getTag();
                }
                String item = getItem(position);
                holder.mTxtvTitle.setText(item);
                holder.mTxtvSubTitle.setText(item);
                return convertView;
        }
    private class ViewHolder {
        TextView mTxtvTitle;
        TextView mTxtvSubTitle;
    }
}

En om deze dan aan je listview te geven gebruik je deze code:
YourAdapter adapter = new YourAdapter(this, mInput);
mYourListview.setAdapter(adapter);

Bewerkt (1 november 2013 14:42)
tim687

tim687

  • Lid sinds 11 juli 2011
  • Berichten 418
  • Reputatie 0
  • #5
  • 2 november 2013
  • 15:06

Dankje voor het voorbeeld!!
Kan je me nog 1 ding uitleggen?
Hoe voeg ik afbeeldingen toe aan de items?

En tenslotte heb ik nog een vraagje:
Gebruik ik deze code op de goede manier?

public static Bitmap WiFiSignalStrength(int strength, int passProtected){
		Bitmap SignalIcon = null;
		Bitmap bmp = null;
		Bitmap locked = null;
		
		switch (strength){
		
		case 0:
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_no_signal);
			break;
		
		case 1:
			
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_weakest);
			break;	
		case 2:
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_stronger);
			break;
		case 3:
			bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.signal_strongest);
			break;
		}
		
		switch (passProtected){
		case 0:
			locked = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_lock_lock);
			break;
		
		}
		
		if (bmp != null){
		 Bitmap bmOverlay = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
	        Canvas canvas = new Canvas(bmOverlay);
	        canvas.drawBitmap(bmp, new Matrix(), null);
	        	if(locked != null){
	        		canvas.drawBitmap(locked, 0, 0, null);
	        	}
	        	SignalIcon = bmOverlay;
		
		
	}
		
		
		return SignalIcon;
	}
Hartelijk bedankt,
Tim

deepspace

deepspace

  • Lid sinds 31 oktober 2013
  • Berichten 14
  • Reputatie 0
  • #6
  • 2 november 2013
  • 20:18

Hm, het ziet er goed uit. Ik zou zeggen probeer het gewoon

tim687

tim687

  • Lid sinds 11 juli 2011
  • Berichten 418
  • Reputatie 0
  • #7
  • 2 november 2013
  • 23:19

Oke bedankt! Ik zal het volgende week proberen ivm een pta week op school.
Bedankt,
Ik hou jullie op de hoogte!
Tim

Reageer

Om te reageren, dien je te zijn ingelogd. Druk op de onderstaande knop om in te loggen of maak een nieuwe account aan.

Inloggen Registreren