0

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>

私のコードは次のとおりです。

カーソル 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;
        }

    }

}
4

2 に答える 2

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());
于 2012-10-10T10:53:58.610 に答える
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;
    }
}
于 2014-06-17T17:24:05.997 に答える