web-dev-qa-db-ja.com

リストビューのさまざまなアイコン

リストビューを作成する必要があります。各アイテムには、右側に個別のアイコンが表示されます。コードを書きましたが、機能しませんか?どうすれば続行できますか?アプリケーションがクラッシュする

コード

public class LoginMenu extends ListActivity
{

TextView maintext, subtext;
QuickContactBadge icon;

private static final String[] menuitems = { "Availability", "Messages",
"Greetings", "Address Book", "Calls", "Settings" };

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
maintext = (TextView) findViewById(R.id.tvMainText);
subtext = (TextView) findViewById(R.id.tvSubText);
setListAdapter(new IconicAdapter());    
}    
class IconicAdapter extends ArrayAdapter<String>
{    
public IconicAdapter()
{
super(LoginMenu.this, R.layout.menu, R.id.tvMainText, menuitems);
// TODO Auto-generated constructor stub
}   
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View row = super.getView(position, convertView, parent);
icon = (QuickContactBadge) findViewById(R.id.iContactBadge);
if (menuitems[0] != null)
{
icon.setImageResource(R.drawable.menu_availability);
}
else if (menuitems[1] != null)
{
icon.setImageResource(R.drawable.menu_messages);
}    
else if (menuitems[2] != null)
{
icon.setImageResource(R.drawable.menu_greetings);
}
else if (menuitems[3] != null)
{
icon.setImageResource(R.drawable.menu_contacts);    
}
else if (menuitems[4] != null)
{
icon.setImageResource(R.drawable.menu_calls);
}
else if (menuitems[5] != null)
{
icon.setImageResource(R.drawable.menu_settings);
}
return (row);
}    
}
}

IMAGE私のXMLファイルには、下の図のように配置された画像、テキストビュー、および別のサブテキストが含まれています

Image of the screen needed

LOGCAT

10-11 04:46:15.088: E/AndroidRuntime(726): FATAL EXCEPTION: main
10-11 04:46:15.088: E/AndroidRuntime(726): Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.avst.callxpressmobile/com.example.avst.callxpressmobile.MenuScreen}: Java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'Android.R.id.list'
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2059)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2084)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.ActivityThread.access$600(ActivityThread.Java:130)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1195)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.os.Handler.dispatchMessage(Handler.Java:99)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.os.Looper.loop(Looper.Java:137)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.ActivityThread.main(ActivityThread.Java:4745)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Java.lang.reflect.Method.invokeNative(Native Method)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Java.lang.reflect.Method.invoke(Method.Java:511)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:786)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
10-11 04:46:15.088: E/AndroidRuntime(726):  at dalvik.system.NativeStart.main(Native Method)
10-11 04:46:15.088: E/AndroidRuntime(726): Caused by: Java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'Android.R.id.list'
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.ListActivity.onContentChanged(ListActivity.Java:243)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.Android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.Java:259)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.Activity.setContentView(Activity.Java:1867)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.example.avst.callxpressmobile.MenuScreen.onCreate(MenuScreen.Java:22)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.Activity.performCreate(Activity.Java:5008)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1079)
10-11 04:46:15.088: E/AndroidRuntime(726):  at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2023)
10-11 04:46:15.088: E/AndroidRuntime(726):  ... 11 more
13
chitranna

ここで簡単なコードを取得できます。これは、リストの各行に画像とテキストを表示します。

http://www.mkyong.com/Android/android-listview-example/

enter image description here

ただし、行数が多く、各行の画像をサーバーからフェッチする場合は、次のように使用することをお勧めします。

https://github.com/thest1/LazyList

11
Archie.bpgc

Aを使用するだけですHashMap<String,String>行の詳細(画像、2つのテキスト)が含まれます。これを参照してください リンク それはあなたが望むものを達成するのに役立ちます。

enter image description here

6
K_Anas

原因:Java.lang.RuntimeException:コンテンツには、id属性が「Android.R.id.list」であるListViewが必要です。

ListActivityを使用しており、コンテンツsetContentView()も設定しています。 ListActivityにはデフォルトでListViewがあり、xmlレイアウトは必要ありません。ただし、xmlレイアウトを提供する場合、レイアウトにはIDが_@Android:id/list_に設定されたListView要素が必要です。

内容については、いつでもMenuでmenu.xmlを膨らませて、データ配列のように使用できます。幸い、各MenuItemにはタイトルとアイコンがあります。また、Menuにはsize()メソッドとgetItem()メソッドがあります。

2
S.D.

以下のいくつかのリンクは、要件に従って同じコードを使用するのに役立ちます。

http://www.androidhive.info/2012/02/Android-custom-listview-with-image-and-text/

http://wptrafficanalyzer.in/blog/listview-with-images-and-text-using-simple-adapter-in-Android/

http://Android-example-code.blogspot.in/p/dynamic-custoized-list-view-in-Android.html

http://www.softwarepassion.com/Android-series-custom-listview-items-and-adapters/

[〜#〜]または[〜#〜]

   if (menuitems[0] != null) {
              if(menuitems[0].equalsIgnoreCase("Availability")
                {
                    icon.setImageResource(R.drawable.menu_availability);
                 // Set your another textview and another subtext here....
                 }
         } 
  // Set another conditions also as per above code

ありがとう

2
SBJ