4

为了拥有一个彩色的 ListView,我创建了自己的 ArrayAdapter,但是当我想使用它时adapter.clear()adapter.add()它会给我带来错误。这是代码和错误。

XML 文件:

<ListView
    android:id="@+id/android:list"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:layout_margin="4dip"
    android:layout_weight="1">
</ListView>

适配器:

class stableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public stableArrayAdapter(Context context, int textViewResourceId, String [] objects) 
    {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.length; ++i) {
            mIdMap.put(objects[i], i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}

Java代码:

public class createtarget extends ListActivity
{
    stableArrayAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createtarget);


        lstView = getListView();
        lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
        lstView.setTextFilterEnabled(true);

        Target=new String []{""};
        adapter = new stableArrayAdapter(this,android.R.layout.simple_list_item_checked, Target);
        setListAdapter(adapter);
    }   
}

添加代码:

public void submit(View view) 
{

    adapter.add("Hello");
    } 

错误:

06-08 05:21:11.785: E/AndroidRuntime(3584): FATAL EXCEPTION: main
06-08 05:21:11.785: E/AndroidRuntime(3584): java.lang.IllegalStateException: Could not execute method of the activity
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.view.View$1.onClick(View.java:3599)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.view.View.performClick(View.java:4204)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.view.View$PerformClick.run(View.java:17355)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.os.Handler.handleCallback(Handler.java:725)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.os.Looper.loop(Looper.java:137)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at java.lang.reflect.Method.invoke(Method.java:511)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at dalvik.system.NativeStart.main(Native Method)
06-08 05:21:11.785: E/AndroidRuntime(3584): Caused by: java.lang.reflect.InvocationTargetException
06-08 05:21:11.785: E/AndroidRuntime(3584):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at java.lang.reflect.Method.invoke(Method.java:511)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.view.View$1.onClick(View.java:3594)
06-08 05:21:11.785: E/AndroidRuntime(3584):     ... 11 more
06-08 05:21:11.785: E/AndroidRuntime(3584): Caused by: java.lang.UnsupportedOperationException
06-08 05:21:11.785: E/AndroidRuntime(3584):     at java.util.AbstractList.add(AbstractList.java:404)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at java.util.AbstractList.add(AbstractList.java:425)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
06-08 05:21:11.785: E/AndroidRuntime(3584):     at net.learn2develop.UsingIntent.createtarget.submit(createtarget.java:136)
06-08 05:21:11.785: E/AndroidRuntime(3584):     ... 14 more
06-08 05:21:16.206: E/Trace(3614): error opening trace file: No such file or directory (2)
4

5 回答 5

7

试试下面的。有一个数组列表。将字符串添加到 arraylist 并在适配器上调用 notifydatasetchanged()

public class MainActivity extends ListActivity
{
stableArrayAdapter adapter;
ListView lstView; 
ArrayList<String> Target = new ArrayList<String>(); 
@Override
public void onCreate(Bundle savedInstanceState) 
{
   super.onCreate(savedInstanceState);
    lstView = getListView();
    lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
    lstView.setTextFilterEnabled(true);
    Target.add("hi");
    Target.add("hello");
    adapter = new stableArrayAdapter(this,android.R.layout.simple_list_item_checked, Target);
    setListAdapter(adapter);
    Target.add("myname");
    adapter.notifyDataSetChanged();
}   
class stableArrayAdapter extends ArrayAdapter<String> {


    public stableArrayAdapter(Context context, int textViewResourceId, ArrayList<String> target) 
    {
      super(context, textViewResourceId, target);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Target.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return Target.get(position);
    }

    @Override
    public int getPosition(String item) {
        // TODO Auto-generated method stub
        return super.getPosition(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

} 
}

正如肯建议的那样。

public class MainActivity extends ListActivity
   {

    ListView lstView; 
    ArrayList<String> Target = new ArrayList<String>(); 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
        lstView = getListView();
        lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
        lstView.setTextFilterEnabled(true);
        Target.add("hi");
        Target.add("hello");
       ;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, Target);
        setListAdapter(adapter);
        Target.add("myname");
        adapter.notifyDataSetChanged();
    }   
    }

在此处输入图像描述

于 2013-06-08T06:24:08.857 回答
3

我不知道这些对你有没有帮助..

根据我的意见,您应该检查您的 xml 文件。当您使用getListView()方法时,您应该声明您的 Listview 对象,例如

android:id = @android:id/list

或者

将这些行与您的 ArrayAdpater 一起使用

arrAdapt.setNotifyOnChange(true);

愿这些对你有帮助。。

于 2013-06-08T05:48:20.623 回答
0

您忘记初始化您的列表视图,如下所示:

lstView = (ListView) findViewById(R.id.yourlistview);

接着

lstView = getListView();
于 2013-06-08T05:42:54.937 回答
0

您遇到了这个问题

您实际上不需要扩展ArrayAdapter- 目前它没有为您做任何事情。

而是这样做:

 List<String> target = new ArrayList<String>();
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, target);

然后稍后您的.add()电话将起作用。

于 2013-06-08T06:01:54.937 回答
0

要使用 ListActivity,您必须使用 Listview 的 id 作为 - android:id="@android:id/list" 。

那么只有你可以通过调用 - getListView(); 来获取 ListView 的实例。方法 。

于 2013-06-08T06:05:08.757 回答