2016-03-26 13 views
-1

Wenn ich auf die listView klicke, um die Checked Textansicht zu sehen, stoppt die App. hier ist die list_item.xmlCheckedTextView ist in ListView nicht anklickbar

 <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip" > 

    <CheckedTextView 
     android:id="@+id/checkedTextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:gravity="center" 
     android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" 
     /> 

</RelativeLayout> 

hier wird die XML-Aktivität:

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     > 

    </ListView> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="127dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Submit" /> 

</LinearLayout> 

hier ist der Code der Aktivität:

 package com.example.arf.mycitymyenvironment; 

import android.app.Activity; 
import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.CheckedTextView; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ProblemActivity extends Activity { 
    ListView listView ; 

    String[] values; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_problem); 
//Toast.makeText(ProblemActivity.this,"new activity",Toast.LENGTH_SHORT).show(); 
     listView = (ListView) findViewById(R.id.list); 
     values = new String[] { "Garbage","Water Pollution","Air Pollution" }; 

     // Define a new Adapter 
     // First parameter - Context 
     // Second parameter - Layout for the row 
     // Third parameter - ID of the TextView to which the data is written 
     // Forth - the Array of data 

     final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       R.layout.list_item, R.id.checkedTextView, values); 



     // Assign adapter to ListView 
     listView.setAdapter(adapter); 
     // we want multiple clicks 
     listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
listView.setOnItemClickListener(new CheckBoxClick()); 


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

       // ListView Clicked item index 
       int itemPosition = position; 

       // ListView Clicked item value 
       String itemValue = (String) listView.getItemAtPosition(position); 




       //checkbox 
       //checkBox.isChecked(); 

       // Show Alert 
       Toast.makeText(getApplicationContext(), 
         "Position :" + itemPosition + " ListItem : "+itemValue , Toast.LENGTH_LONG) 
         .show(); 


      } 


     }); 
     */ 
    } 

    public class CheckBoxClick implements AdapterView.OnItemClickListener { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      // TODO Auto-generated method stub 

      CheckedTextView ctv = (CheckedTextView)arg1; 
      if(ctv.isChecked()){ 
       Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show(); 
      }else{ 
       Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

} 

jedes Mal, ist der Fehler CheckedTextView ctv = (CheckedTextView)arg1;

was kann ich tun, um es zu lösen?

+0

Sie haben die 'CheckedTextView' in einem' RelativeLayout', so 'arg1' ist ein' RelativeLayout', die Sie nicht auf 'CheckedTextView' werfen können. Wenn Sie nichts anderes in Ihr Listenelement-Layout einfügen, können Sie das 'RelativeLayout' loswerden. Andernfalls rufen Sie 'findViewById()' auf 'arg1' auf, um die' CheckedTextView' zu erhalten. –

Antwort

-1
public class CheckBoxClick implements AdapterView.OnItemClickListener { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 

     CheckedTextView ctv = (CheckedTextView)findViewById(R.id.checkedTextView); 
     if(ctv.isChecked()){ 
      Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show(); 
     }else{ 
      Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

versuchen Sie diesen Code.

0

Try this:

public class CheckBoxClick implements AdapterView.OnItemClickListener { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 

     CheckedTextView ctv = (CheckedTextView)arg1.findViewById(R.id.checkedTextView); 
     if(ctv.isChecked()){ 
      Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show(); 
     }else{ 
      Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

Ihre View arg1 ist RelativeLayout. CheckedTextView ist Kind von RelativeLayout arg1 so können Sie es mit arg1.findViewById(R.id.checkedTextView); in jeder Reihe

+0

Vielen Dank. Es hat funktioniert! (^_^) – iamneaz

-1

Set OnClickListener für CheckedTextView bekommen und onClick Ereignis wechseln den Wert CheckedtextView.

Fügen Sie in Ihrer getView-Methode des ListView-Adapters folgenden Code hinzu.

checkedTextView.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         checkedTextView.setChecked(!checkedTextView.isChecked()); 
        } 
       }); 
Verwandte Themen