尝试使用复选框

时间:2016-11-30 19:32:37

标签: java android listview checkbox

所以我有一个带有listview的应用程序。单击列表视图项时,它将打开一个新活动。我在这个新活动中放置了一个复选框,当它被选中时,它应该在前一个屏幕上的listview项旁边放置一个复选标记。我正在考虑这个错误,因为我正在尝试从第二个活动中设置复选框,该活动具有与列表视图不同的内容视图。我希望我解释得那么好。

使用复选框代码

继承我的RouteDetails.java
public class RouteDetails extends AppCompatActivity {

ImageView routeImage;
String routeName;
CheckBox routeCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_route_details);

    //back button for route details view
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    ///////checkbox///////////////////////////////////////////
    routeCheckBox = (CheckBox) findViewById(R.id.routeCheckBox);
    final ImageView checkImageView = (ImageView) findViewById(R.id.checkImageView);

    routeCheckBox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {
          if (routeCheckBox.isChecked())
          {
              checkImageView.setImageResource(R.drawable.birdsboroicon);
          }
        }
    });
    /////////////////////////////////////////////


    //sets actionbar title
    routeName = getIntent().getExtras().getString("routeName");
    getSupportActionBar().setTitle(routeName);

    //TextView for route details
    final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
    routeDetailsView.setText(getIntent().getExtras().getCharSequence("route"));

    //ImageView for route details
    routeImage = (ImageView) findViewById(R.id.routeImage);
    final int mImageResource = getIntent().getIntExtra("imageResourceId", 0);
    routeImage.setImageResource(mImageResource);

并且继承了包含我试图根据复选框状态设置的imageview的custom_row.xml。

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="51dp"
    android:id="@+id/routeText"
    android:layout_weight="1"
    android:textSize="18sp"
    android:gravity="center_vertical"
    android:textColor="@android:color/black"
    android:typeface="normal"
     />

<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:id="@+id/checkImageView" />

所以我希望在按下后退按钮后,复选标记位于listview项旁边。

如果是相关的话,我会包含其他代码

继承我的RouteDetails.xml,其中包含复选框

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zach.listview.RouteDetails">


<CheckBox
    android:text="Route Climbed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/routeCheckBox"
    android:gravity="center" />

<TextView
    android:text="TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/routeDetailsView"
    android:textSize="18sp"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:layout_below="@id/routeCheckBox"/>


<ImageView
    android:layout_below="@id/routeDetailsView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/routeImage"
    android:scaleType="fitCenter"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:adjustViewBounds="true" />


</RelativeLayout>
</ScrollView>

My Custom adapter.java,用于创建每个listview行

class CustomAdapter extends ArrayAdapter<CharSequence>{

    public CustomAdapter(Context context, CharSequence[] routes) {
        super(context, R.layout.custom_row ,routes);
    }

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

    LayoutInflater routeInflater = LayoutInflater.from(getContext());
    View customView = convertView;
    if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}

    CharSequence singleRoute = getItem(position);
    TextView routeText = (TextView) customView.findViewById(R.id.routeText);
    routeText.setText(singleRoute);

return customView;
}

my mainavtivity.java,这是填充列表视图的地方

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Action Bar customization
    final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(R.layout.actionbar);

    setContentView(R.layout.activity_main);

    ///// fill listview numbers I want to add
    final String[] routeListviewNumbers = getResources().getStringArray(R.array.routeNumbers);

    //fill list view with xml array of routes
    final CharSequence[] routeListViewItems = getResources().getTextArray(R.array.routeList);
    //fills route detail text view with xml array info
    final CharSequence[] routeDetail= getResources().getTextArray(R.array.routeDetail);
    //fills route detail image view with xml array of images
    final TypedArray image = getResources().obtainTypedArray(R.array.routeImages);
    //image.recycle();

    //custom adapter for list view
    ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
    final ListView routeListView = (ListView) findViewById(R.id.routeListView);
    routeListView.setAdapter(routeAdapter);

    routeListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    CharSequence route = routeListViewItems[position];

                    int imageId = (int) image.getResourceId(position, -1);
                    if (route.equals(routeListViewItems[position]))
                    {
                        Intent intent = new Intent(view.getContext(), RouteDetails.class);
                        intent.putExtra("route", routeDetail[position]);
                        intent.putExtra("imageResourceId", imageId);
                        intent.putExtra("routeName", routeListViewItems[position]);
                        startActivity(intent);
                    }
                }
            }
    );

}
}

和我的activitymain.xml是listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="0dp"

tools:context="com.example.zach.listview.MainActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/routeListView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

编辑:所以我把它添加到了我的RouteDetails.java

routeCheckBox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {
          if (routeCheckBox.isChecked())
          {
              //checkImageView.setImageResource(R.drawable.checkmark);
              Intent check = new Intent(RouteDetails.this,CustomAdapter.class);
              check.putExtra("checkImageResource", R.drawable.checkmark);
              startActivity(check);
          }
        }
    });

这是我的customAdapter.java

////////trying to set checkmark/////
    ImageView checkImageView = (ImageView) customView.findViewById(R.id.checkImageView);
    checkImageView.setImageResource(((Activity)    getContext()).getIntent().getIntExtra("checkImageResource",0));
////////////////////////////////////////

但这也不起作用。它说致命异常无法找到明确的活动类。我这样做是因为CustomAdapter不是一个活动,但是customadapter链接到我的custom_row,其中包含我要添加复选框的imageview。我该怎么做?我不确定还有什么可以尝试

编辑:如果有人有任何建议,我仍然没有找到解决方案!

0 个答案:

没有答案