因此,我查看了许多其他答案,但无法让我的应用程序按照我的意愿运行。我基本上想要具有文本和 的列表视图,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;
}
}