0

我正面临文本资源 ID 的错误。我不知道如何解决这个问题。

public class ExemptListView extends Activity {

DbHandler dbHandler;
Cursor c;
ArrayList<Devices> list;
MycustomAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);

    final ListView listview_var = (ListView) findViewById(R.id.listView1);
    dbHandler = new DbHandler(getApplicationContext());

    list = new ArrayList<Devices>();
    generateListofDevices();
    for (int i = 0; i < list.size(); i++) {
        Log.i("Dhiraj", list.get(i).devicename + " : "
                + list.get(i).IpAddress + " : " + list.get(i).checked);

    }

    adapter = new MycustomAdapter(this, R.layout.custom_checkboxlist, list);

    ListView lv=(ListView)findViewById(R.id.listView1);
    lv.setAdapter(adapter);
    Log.i("Dhiraj"," Adapter set");

    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.i("Dhiraj","lv.setitemclicklistener");
            Devices dv=(Devices)arg0.getItemAtPosition(arg2);
             //Toast.makeText(getApplicationContext(),
                //      "Clicked on Row: " + dv.devicename,
                  //    Toast.LENGTH_LONG).show();              
        }
    });

    checkbuttonclick();
}

private void generateListofDevices() {
    c = dbHandler.GetCursor(dbHandler.Devices_List_db);
    Log.i("Dhiraj", "generating lsit ");
    if (c != null) {
        if (c.moveToFirst())
            do {
                Boolean Checked = false;
                Log.i("Dhiraj", c.getString(1) + " : " + c.getString(2)
                        + " : " + c.getString(3));
                if (c.getString(3) == "Y")
                    Checked = true;
                Devices d = new Devices(c.getString(1), c.getString(2),
                        Checked);
                list.add(d);
            } while (c.moveToNext());
    }
}

public class Devices {
    String devicename = null;
    String IpAddress = null;
    Boolean checked = false;

    Devices(String devicename, String IpAddress, Boolean checked2) {
        this.devicename = devicename;
        this.IpAddress = IpAddress;
        this.checked = checked2;
    }

    public void setSelected(boolean selected) {
        this.checked = selected;
    }

}

private class MycustomAdapter extends ArrayAdapter<Devices> {
    private ArrayList<Devices> Deviceslist;

    public MycustomAdapter(Context context, int resource,
            ArrayList<Devices> DeviceList) {
        super(context, resource, DeviceList);
        this.Deviceslist = new ArrayList<Devices>();
        this.Deviceslist.addAll(DeviceList);
        Log.i("Dhiraj","MycustomAdapter constructor cmpleted");
    }

    private class ViewHolder {
        TextView Name;
        TextView IP;
        CheckBox Exempt;            
    }

    public View GetView(int position, View convertview, ViewGroup parent) {
        Log.i("Dhiraj"," GET View");
        ViewHolder holder = null;

        if (convertview == null) {
            Log.i("Dhiraj","convertview is null");
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertview = vi.inflate(R.layout.custom_checkboxlist, null);

            holder = new ViewHolder();
            holder.Name = (TextView) findViewById(R.id.DEVNAME);
            holder.IP = (TextView) findViewById(R.id.DEVADDR);
            holder.Exempt = (CheckBox) findViewById(R.id.EXEMPT);
            convertview.setTag(holder);

            holder.Exempt.setOnClickListener(new OnClickListener() {                    
                @Override
                public void onClick(View v) {

                    Log.i("Dhiraj"," on click listener");

                    CheckBox cb = (CheckBox) v;
                    Devices devices = (Devices) cb.getTag();
                    Toast.makeText(
                            getApplicationContext(),
                            "Clicked on Checkbox: " + cb.getText() + " is "
                                    + cb.isChecked(), Toast.LENGTH_LONG)
                            .show();
                    devices.setSelected(cb.isChecked());
                }
            });
        }
        else
        {
            holder=(ViewHolder)convertview.getTag();
        }
        Devices device=Deviceslist.get(position);
        holder.Name.setText(device.devicename);
        holder.IP.setText(device.IpAddress);
        holder.Exempt.setChecked(device.checked);
        Log.i("Dhiraj"," View Over");
        return convertview;
    }
}

public void checkbuttonclick(){
    Button Apply=(Button)findViewById(R.id.Apply);

    ArrayList<Devices> deviceslist=adapter.Deviceslist;
    for(int i=0;i<deviceslist.size();i++){
        Devices d=deviceslist.get(i);
        if(d.checked)
        Log.i("Dhiraj", d.devicename +" is Exempted");
    }
}

}

请帮忙

我的 LOGCAT 的输出:

10-14 03:50:13.882: E/ArrayAdapter(15058): You must supply a resource ID for a TextView
10-14 03:50:13.890: W/dalvikvm(15058): threadid=1: thread exiting with uncaught exception (group=0x40ab5210)
10-14 03:50:13.897: E/AndroidRuntime(15058): FATAL EXCEPTION: main
10-14 03:50:13.897: E/AndroidRuntime(15058): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.AbsListView.obtainView(AbsListView.java:2144)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.ListView.onMeasure(ListView.java:1155)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.View.measure(View.java:12751)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.View.measure(View.java:12751)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:295)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.View.measure(View.java:12751)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.View.measure(View.java:12751)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:295)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2120)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.View.measure(View.java:12751)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1106)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2558)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.os.Looper.loop(Looper.java:137)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.app.ActivityThread.main(ActivityThread.java:4697)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at java.lang.reflect.Method.invokeNative(Native Method)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at java.lang.reflect.Method.invoke(Method.java:511)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-14 03:50:13.897: E/AndroidRuntime(15058):    at dalvik.system.NativeStart.main(Native Method)
10-14 03:50:13.897: E/AndroidRuntime(15058): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
10-14 03:50:13.897: E/AndroidRuntime(15058):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
10-14 03:50:13.897: E/AndroidRuntime(15058):    ... 30 more
4

1 回答 1

0

ArrayAdapter需要您提供的资源用于 a ,除非您根据此处的文档TextView覆盖getView()以返回适当的视图类型。

看起来您没有getView()正确覆盖,因为您的方法被命名GetView()(带有大写字母)。如果您使用 java@Overrides注释,编译器应该选择这些类型的错误。

于 2013-10-13T22:32:44.583 回答