0

カスタムリストビューを作成しようとしていますが、本当に初心者であり、助けが必要です....コードは次のとおりです

public class Main_Activity extends ListActivity {

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

setListAdapter((ListAdapter) new MyAdapter(this, 
        android.R.layout.simple_list_item_1,R.id.textView1,
        getResources().getStringArray(R.array.categories)));

}

private class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = inflater.inflate(R.layout.list_item, parent, false);
        String[] items = getResources().getStringArray(R.array.categories);

        ImageView image = (ImageView)row.findViewById(R.id.textView1);
        TextView text =(TextView)row.findViewById(R.id.textView1);

        text.setText(items[position]);

        if(items[position].equals("Life")){
            image.setImageResource(R.drawable.lifeico);
        }

        else if(items[position].equals("Corporate")){
            image.setImageResource(R.drawable.corpico);
        }

        else if(items[position].equals("umash")){
            image.setImageResource(R.drawable.umashico);
        }


        return row;
    }
}

//その後、リストビュー レイアウト 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="fill_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</LinearLayout>

// 複合ドローアブル レイアウトを使用する

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/lifeico"
android:drawablePadding="5dp"
android:text="@string/textview"
android:textSize="25sp" >

</TextView> 

//そしてアイテムのリソースリスト

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="categories">
    <item name="Life">Individual Life</item>
    <item name="Corporate">Corporate Insurance</item>
    <item name="Umash">Umash Funeral Services</item>
</string-array>

</resources>
4

3 に答える 3

1

ArrayAdapter の代わりに BaseAdapter を使用してみてください。BaseAdapter からアダプターを継承する 2 つの重要なポイント:

最初:配列アダプターは、コンストラクターを介して配列内のデータを取得します。したがって、getView() で getStringArray を再度呼び出す必要はありません。ArrayAdapter.getItem(index) を使用して、指定した位置のオブジェクトを取得できます。

2 番目: ConvertView (getView メソッドの 2 番目のパラメーター) は、null でない場合にデータを設定するために使用できるスケープ ビューです。この方法では、新しいビューを作成または拡張する必要はなく、ListView によって以前にリリースされたビューを使用できます。

于 2012-12-23T13:28:24.710 に答える
0

続行するのに十分な情報が提供されていない (可能であればエラーのログキャットや、ログ ステートメントに到達していないなど)。

View rowあなたが膨らませているxmlに問題があるに違いないと思いますView row = inflater.inflate(R.layout.list_item,。それは間違った名前かもしれませんが、レイアウト全体で、TextView? もしそうなら、あなたは(imageView &を追加して)あなたのメインレイアウトと同じように、などViewGroupRelativeLayout入れるべきです。LinearLayoutしかし、最も憂慮すべきことはおそらく次のとおりです。

    ImageView image = (ImageView)row.findViewById(R.id.textView1); <-
    TextView text =(TextView)row.findViewById(R.id.textView1); <-

同じ ID で 2 つの異なるビューを参照していることがわかりますか? 最新の画像コマンドがそれを台無しにしている可能性があります。

getViewさらに、 の各行に対して呼び出されるため、 で不要なことを実行しないことをお勧めしListViewます。それがコンストラクターを介して物を渡すことです(無視したように見えるアイテム)ため、それらは一度だけ行われます。次のようにしてみてください。

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

setListAdapter((ListAdapter) new MyAdapter(
        this, 
        R.layout.list_item,  <--
        R.id.textView1,  <------
        R.id.imageView1, <------ make sure these are all correct
        getResources().getStringArray(R.array.categories)));

}

private class MyAdapter extends ArrayAdapter<String>{

    private int resource;
    private int textViewResourceId;
    private int imageViewResourceId;
    private String[] strings;
    private LayoutInflater inflater;

    public MyAdapter(Context context, int resource, int textViewResourceId, 
            int imageViewResourceId, String[] strings) {
        super(context, resource, textViewResourceId, strings);
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.imageViewResourceId  = imageViewResourceId;
        this.strings = strings;
        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        View row = inflater.inflate(resource, parent, false);

        ImageView image = (ImageView)row.findViewById(imageViewResourceId);
        TextView text =(TextView)row.findViewById(textViewResourceId);

        text.setText(strings[position]);

        if(strings[position].equals("Life")){
            image.setImageResource(R.drawable.lifeico);
        }

        else if(strings[position].equals("Corporate")){
            image.setImageResource(R.drawable.corpico);
        }

        else if(strings[position].equals("umash")){
            image.setImageResource(R.drawable.umashico);
        }


        return row;
    }

コンストラクターのパラメーターがすべて同じマスター クラスにある場合は、すべてのコンストラクター パラメーターをそのまま渡すこともできます。

于 2012-12-23T13:39:14.233 に答える
0

どこからともなく ImageView を作成しています。

logcat で ClassCastException を取得する必要があります。LinearLayout を使用して TextView と ImageView の両方を持つ、実際の ImageView (TextView ではない) をレイアウトに追加します。

また、パフォーマンスのためにビューをリサイクルする必要があります。

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View row = convertView;
    if(row == null){
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        row = inflater.inflate(R.layout.list_item, parent, false);
   }
   ....
}
于 2012-12-23T13:40:45.200 に答える