I am trying to build a listview that has a checkbox next to each item. In addition, I want some of the items, when I click on them, to go to a new activity (It will take me to a new list with checkboxes). The problem is that if I click on each item, not matter which particular area of item, the box is checked. I need the box to be checked only when I click on the checkbox. In nnother words, I want to be able to check each item only when I click the checkbox, and anywhere else on each item, start a new activity.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
<ListView
android:id="@+id/fruitselector"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
checkboxlayout.xml:
<CheckedTextView xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:padding="20sp"
/>
MainActivity: The Checkbox Works, but the Onclick for a new acvivity does not. (The Main2 and the Main3 Acivity are the new activities).
package com.example.jesse.languageswitcher;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> selectedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedItems=new ArrayList<String>();
}
public void onStart(){
super.onStart();
ListView chl=(ListView) findViewById(R.id.fruitselector);
chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] items={"Apple","Pear","Peach","watermelon","Orange","Grape"};
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,R.layout.checkboxlayout,R.id.checkbox,items);
chl.setAdapter(aa);
chl.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = ((TextView) view).getText().toString();
if(selectedItems.contains(selectedItem))
selectedItems.remove(selectedItem);
else
selectedItems.add(selectedItem);
if (position == 0) {
Intent myintent = new Intent(view.getContext(), Main2Activity.class);
startActivityForResult(myintent, 0);
}
if (position == 1) {
Intent myintent = new Intent(view.getContext(), Main3Activity.class);
startActivityForResult(myintent, 1);
}
}
});
}
}
Here is my old code, in which the starting a new activity worked. The xml files are the same except there is no checkboxlayout.xml
public class MainActivity extends AppCompatActivity {
String items[] = new String[]{
"Apple", "Orange", "Pear"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent myintent = getIntent();
String value = myintent.getStringExtra("myintent");
TextView textview = (TextView)findViewById(R.id.textView2);
textview.setText(value);
ListView listView = (ListView) findViewById(fruitselector);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
////This is the code that starts a new activity if I click on certain items.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
Intent myintent = new Intent(view.getContext(), Main2Activity.class);
startActivityForResult(myintent, 0);
}
if (position==1){
Intent myintent = new Intent(view.getContext(), Main3Activity.class);
startActivityForResult(myintent, 1);
}
}
});
}}
Aucun commentaire:
Enregistrer un commentaire