0

因此,我查看了许多其他答案,但无法让我的应用程序按照我的意愿运行。我基本上想要具有文本和 的列表视图,check mark然后right是左侧的添加按钮。现在我的列表视图显示了,但检查图像从未改变。

编辑:在有用的评论之后,我发现可以使用模拟器上的箭头(向上/向下)选择行,这突出显示了我想要的行。但是,当我单击该行时,它并没有像我想要的那样被选中。此外,如果它有助于在对话框中使用列表视图。

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_selected="true" 
        android:drawable="@drawable/accept_on" />
    <item 
        android:drawable="@drawable/accept" />
</selector>

行 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:background="#EEE">

    <ImageButton 
        android:id="@+id/goToMapButton"
        android:src="@drawable/go_to_map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"    />

    <TextView 
        android:id="@+id/itemName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"     />

    <Button 
        android:id="@+id/checkButton"
        android:background="@drawable/item_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"     />
</LinearLayout>

地图适配器:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MapAdapter extends ArrayAdapter<String>{

    Context context; 
    int layoutResourceId;    
    String data[] = null;
    LayoutInflater inflater;
    LinearLayout layout;

    public MapAdapter(Context context, int layoutResourceId, String[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public String getItem(int position) {
        return data[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();

        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.map_item_row, null);
            layout = (LinearLayout)convertView.findViewById(R.id.layout);
            holder.map = (ImageButton)convertView.findViewById(R.id.goToMapButton);
            holder.name = (TextView)convertView.findViewById(R.id.itemName);
        //holder.check = (Button)convertView.findViewById(R.id.checkButton);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        layout.setBackgroundColor(0x00000004);
        holder.name.setText(getItem(position));

        return convertView;
    }

    static class ViewHolder
    {
        ImageButton map;
        TextView name;
        Button check;
    }
}
4

2 回答 2

0

像这样试试

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Planet to display
        Planet planet = (Planet) this.getItem(position);

        // The child views in each row.
        CheckBox checkBox;
        TextView textView;

        // Create a new row view
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.simplerow, null);

            // Find the child views.
            textView = (TextView) convertView.findViewById(R.id.rowTextView);
            checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);

            // Optimization: Tag the row with it's child views, so we don't
            // have to
            // call findViewById() later when we reuse the row.
            convertView.setTag(new PlanetViewHolder(textView, checkBox));

            // If CheckBox is toggled, update the planet it is tagged with.
            checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Planet planet = (Planet) cb.getTag();
                    planet.setChecked(cb.isChecked());
                }
            });
        }
        // Reuse existing row view
        else {
            // Because we use a ViewHolder, we avoid having to call
            // findViewById().
            PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
                    .getTag();
            checkBox = viewHolder.getCheckBox();
            textView = viewHolder.getTextView();
        }

        // Tag the CheckBox with the Planet it is displaying, so that we can
        // access the planet in onClick() when the CheckBox is toggled.
        checkBox.setTag(planet);

        // Display planet data
        checkBox.setChecked(planet.isChecked());
        textView.setText(planet.getName());

        return convertView;
    }

请参阅此示例 http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

于 2012-10-30T04:10:02.760 回答
0

尝试将选择器设置为背景而不是 src

于 2012-10-30T03:53:54.023 回答