Grundsätzlich erstellen Sie zwei Aktivitäten. Sagen wir, MyListViewActivity enthält eine ListView, deren Elemente nichts anderes sind als benutzerdefinierte Ansichten, die ein ImageView und ein TextView enthalten. dann eine CheckBoxActivity, die die CheckBox-Elemente enthält. Wenn auf ein Listenelement in MyListViewActivity geklickt wird, starten Sie die CheckboxAttivity mit der Methode startActivityForResult().
Schließlich, in der CheckBoxActivity, wenn ein CheckBox-Element aktiviert ist wird sein TextValue zurück an MyListViewActivity zum Aktualisieren der entsprechenden ImageView gesendet. unter Code-Schnipsel Siehe:
Layout-Datei für MyListViewActivity: activity_my_list_view.xml
<?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_my_list_view"
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"
tools:context="YourPackageNameHere.MyListViewActivity" >
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
Layout-Datei für benutzerdefinierte Liste Artikel: list_single.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_gravity="center_horizontal|center_vertical" />
</LinearLayout>
</TableRow>
</TableLayout>
Layout-Datei für CheckBoxActivity: activity_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_checkbox"
android:orientation="vertical"
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"
tools:context="YourPackageNameHere.CheckboxActivity">
<CheckBox
android:text="CheckBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox1" />
<CheckBox
android:text="CheckBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox2" />
</LinearLayout>
Code für MyListViewActivity:
package YourPackageNameHere;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MyListViewActivity extends AppCompatActivity {
ImageView img;
ListView list;
private static final int MY_REQUEST_CODE= 1;
String[] itemNames = {
"Google Plus",
"Twitter",
"Windows",
"Bing",
"Itunes",
"Wordpress",
"Drupal"
} ;
Integer[] imageId = {
R.drawable.ic_home_black_18dp,
R.drawable.ic_account_circle_black_18dp,
R.drawable.ic_fingerprint_black_18dp,
R.drawable.ic_help_black_18dp,
R.drawable.ic_settings_black_18dp,
R.drawable.ic_power_settings_new_black_18dp,
R.drawable.ic_directions_run_black_18dp
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list_view);
CustomList adapter = new CustomList(MyListViewActivity.this, itemNames, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MyListViewActivity.this, "You Clicked at " + itemNames[+ position], Toast.LENGTH_SHORT).show();
//get the image that has been clicked
img= (ImageView) view.findViewById(R.id.img);
//Starting the CheckBoxAtivity for result
Intent mIntent = new Intent(getBaseContext(), CheckboxActivity.class);
startActivityForResult(mIntent, MY_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
switch (result){
case "CheckBox1":
img.setImageResource(R.drawable.ic_gps_fixed_black_18dp);
break;
case "CheckBox2":
img.setImageResource(R.drawable.cast_ic_notification_0);
break;
}
}
}
}//onActivityResult
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemNames;
private final Integer[] imageId;
public CustomList(Activity context, String[] itemNames, Integer[] imageId) {
super(context, R.layout.list_single, itemNames);
this.context = context;
this.itemNames = itemNames;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(itemNames[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
}
Code für ChecboxActivity:
package YourPackageNameHere
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
public class CheckboxActivity extends AppCompatActivity {
CheckBox mCheckBox1;
CheckBox mCheckBox2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
mCheckBox1 = (CheckBox) findViewById(R.id.checkBox1);
mCheckBox2 = (CheckBox) findViewById(R.id.checkBox2);
mCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(mCheckBox1.isChecked()){
sendResultToLisViewActivity((String) mCheckBox1.getText()); //send the text string of mCheckBox1 to MyListViewActivity
}
}
});
mCheckBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(mCheckBox2.isChecked()){
sendResultToLisViewActivity((String) mCheckBox2.getText()); //send the text string of mCheckBox1 to MyListViewActivity
}
}
});
}
public void sendResultToLisViewActivity(String mStringExtra){
Intent returnIntent = new Intent();
returnIntent.putExtra("result",mStringExtra);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}
schließlich in Ihrem MainActivity onCreate() -Methode starten Sie MyListViewActivity wie folgt:
Intent i = new Intent(MainActivity.this, MyListViewActivity.class);
startActivity(i);
Ps: ich dies bezeichnet link zum Erstellen der benutzerdefinierten ListView mit Bildern und Text.
fügen Sie Ihre Protokoll Katze Fehler, wenn Sie den Code – ctu
Ich werde nicht in der Lage, das tun, bis später heute Abend bin ich weg von meinem Computer jetzt. Aber es sagt, customAdapter ist keine Aktivität, daher kann ich diese Codezeile nicht verwenden: checkImageView.setImageResource (((Aktivität) getContext()). GetIntent(). GetIntExtra ("checkImageResource", 0)); Ich habe keine Ahnung, ob ich überhaupt auf dem richtigen Weg bin, um zu versuchen, was ich mache. Gibt es einen einfacheren Weg? – zsh5032
Das ist genau der Punkt, den ich hervorheben wollte. Sie können die Methode startactivity() für eine nicht aktive Klasse nicht aufrufen. Sie können dies [Tutorial] (http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html) überprüfen. Ich schätze das was du machen willst. – ctu