Android自定义列表视图行不会让我添加

时间:2012-10-10 10:44:17

标签: java android listview

我有一个listview,只使用simple_list_view1工作正常。但现在我需要在左边添加一个图标。所以我在res / layout /中创建了一个iconrow.xml。当我尝试将其设置为布局时,它会出错并且不会让我使用android.R.layout.iconrow,。显然我在这里做错了!它是一个正在构建的数组,用于填充数据。

我的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="wrap_content"
android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_delete" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

我的代码是:

Cursor c = db.rawQuery(SQLStatement,null);
    if(c.getCount()!= 0){

Log.e("LocationListView", "Found Items");   

c.moveToFirst();

ArrayList mItemName = new ArrayList();   final ArrayList mItemID = new ArrayList();

c.moveToFirst();
while(!c.isAfterLast()) {
     mItemName.add(c.getString(c.getColumnIndex("Name")));
     mItemID.add(c.getString(c.getColumnIndex(ColType)));
     c.moveToNext();
}
rowCount = mItemName.size();

listView = (ListView) findViewById(R.id.lvLocation);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.iconrow, android.R.id.text1, mItemName);

int[] colors = {0, 0xFFFF0000, 0}; 
listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
listView.setDividerHeight(1);
listView.setAdapter(adapter); 

listView.setClickable(true);

错误发生在android.R.layout.iconrow

任何帮助将不胜感激。

汤姆

编辑:这是我现在的代码 - tv.setText上的错误(mItemName.get(arg0).toString());

     c.moveToFirst();


      final ArrayList<String> mItemID = new ArrayList<String>();

        c.moveToFirst();
        while(!c.isAfterLast()) {
             mItemName.add(c.getString(c.getColumnIndex("Name")));
             mItemID.add(c.getString(c.getColumnIndex(ColType)));
             c.moveToNext();
        }
        rowCount = mItemName.size();

        listView = (ListView) findViewById(R.id.lvLocation);

//        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//                R.layout.iconrow, R.id.text, mItemName);
        tv.setText(mItemName.get(arg0).toString()); 

        int[] colors = {0, 0xFFFF0000, 0}; 
        listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
        listView.setDividerHeight(1);
        listView.setAdapter(new CustomAdapter());

        listView.setClickable(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            Object o = listView.getItemAtPosition(position);
            String StationObjectID = mItemID.get(position);

            Intent SwapPage = new Intent(arg1.getContext(), LocationListView.class);
            SwapPage.putExtra("ID", StationObjectID);
            SwapPage.putExtra("Type", Type);

            startActivityForResult(SwapPage, 0);

          }
        });

        } else {

            Log.e("LocationListView", "Not Found Items");   
            Context context = getApplicationContext();
            CharSequence text = "No data returned";
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();       

        }

        db.close();

    }

        public void goBack(View view) {

            if(Global.returnPage.equals("MainPage")) {  

            Intent addItem = new Intent(view.getContext(), MainPage.class);
            startActivityForResult(addItem, 0);

            } else {
                Intent addItem = new Intent(view.getContext(), LocationListView.class);
                addItem.putExtra("Type", Global.stepListType);
                Log.e("PushThrough", Global.stepListType);
                addItem.putExtra("ID", Global.stepListID);
                Log.e("PushThrough", Global.stepListID);
                startActivityForResult(addItem, 0);
            }
        }


        public void addItem(View view){
            Intent addItem = new Intent(view.getContext(), AddItem.class);
            addItem.putExtra("Count", rowCount);

            if(Adding.equals("Building")){

                addItem.putExtra("Type", "0");

            } else if(Adding.equals("Room")){

                addItem.putExtra("Type", "1001");
                addItem.putExtra("PrevID", dataID);

            } else if (Adding.equals("Area")){

                addItem.putExtra("Type", "532");
                addItem.putExtra("PrevID", dataID);
            }

                startActivityForResult(addItem, 0);
        }

        public void onResume()
        {
            super.onResume();

           Log.e("Tom", "resumed");
        }


        class CustomAdapter extends BaseAdapter
        {

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

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub

            LayoutInflater inf=getLayoutInflater();
            View v=inf.inflate(R.layout.iconrow, arg2,true);

            ImageView iv=(ImageView)v.findViewById(R.id.icon);
            TextView tv=(TextView)v.findViewById(R.id.text);

            return v;
        }

    }

}

2 个答案:

答案 0 :(得分:0)

将自定义适配器创建为

class CustomAdapter extends BaseAdapter
    {

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub

        LayoutInflater inf=getLayoutInflater();
        View v=inf.inflate(R.layout.iconrow, arg2,true);

        ImageView iv=(ImageView)v.findViewById(R.id.icon);
        TextView tv=(TextView)v.findViewById(R.id.text);

        return v;
    }

}

然后将其设置为您的列表

listview.setAdapter(new CustomAdapter());

答案 1 :(得分:0)

另一项实施如下:

public class CustomArrayAdapter extends ArrayAdapter<Applications>{
    private MainActivity context;
    private int layoutResourceId;
    private int textresourceId;
   ArrayList<Applications> objects=null;

    public CustomArrayAdapter(MainActivity context, int resource,ArrayList<Applications> objects) {
        super(context, resource);
        this.context = context;
        this.layoutResourceId = resource;
        this.objects = objects;
        Log.d("CustomArrayAdapter","Was here!!");
    }

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


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inf=getLayoutInflater();
        View convertView=inf.inflate(R.layout.iconrow, arg2,true);

        ImageView iv=(ImageView)v.findViewById(R.id.icon);
        TextView tv=(TextView)v.findViewById(R.id.text);

        return convertView;
    }
}