2016-07-11 2 views
0

Activity [ListViewおよびButtonを含む]を含むサンプルアプリケーションをレイアウトファイルに作成しています。 ListViewは[ラベル/名前とチェックボックス]を含むカスタムです。私はリスト項目CheckBoxチェック[T/F]に基づいてListViewのアダプタクラスからButtonのテキストを変更するコードを書きたいと思います。ListViewアイテムの選択に基づいてアクティビティUIを更新するにはどうすればよいですか?

活動に
+0

@SathishKumarJ。 Buttonインスタンスは、アダプタクラスから処理する必要があるActivityクラスにあるためです。質問をよくお読みください。 – VVB

+0

私はあなたがインターフェイスを使用できると思います。 checkBoxコールバックのonCheckに基づいて、テキストを変更することができます.. – Raghavendra

+0

@Raghavendra慎重に&上記のコメントを読んでください。 – VVB

答えて

0
 listView.setOnItemClickListener(new OnItemClickListener() 
      { 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
       { 
        // When clicked, show a toast with the TextView text 
        AppListOfAllApps Selecteditems = (AppListOfAllApps) parent.getItemAtPosition(position); 
        if (view != null) 
        { 
         CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1); 
         Selecteditems = (AppListOfAllApps) checkBox.getTag(); 
         //here you will get the selected item you may also get the text from it accordingly and then using using button variable just set text 
button.settext("whatever"); 
        } 
       } 
      }); 
0

アダプタクラスで

public class Your_Activity extends Activity implements OnCheckListener// Implement your listener here 

@Override 
public void OnCheck(int position) { 
    // TODO Auto-generated method stub 
    // notify your activity component here 
} 

:上記の方法は、UIを更新するのに役立ちますけれどもアダプタクラスは、それをどのように管理するか来る

private OnCheckListener listener; 

public interface OnCheckListener { 
    public void OnCheck(int position); 
} 

public Your_adapter_constructor(OnCheckListener listener) { 
    // TODO Auto-generated constructor stub 
    this.listener = listener; 
} 

// On your getView() 
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        listener.OnCheck(position);// If you want to pass some value add it here 
       } 
      }); 
+0

あなたのコードを試してみてください。うまくいきませんでした。アクティビティのリスナメソッドの中だけではありません。 – VVB

関連する問題