如何根据ListView项目选择更新Activity UI?

时间:2016-07-11 11:23:21

标签: android listview custom-adapter

我正在布局文件中创建一个包含Activity [包含ListViewButton]的示例应用程序。 ListView是自定义的,包含[Label / Name和CheckBox]。我想编写一些代码,这些代码将根据列表项Button检查[T / F],从ListView的适配器类更改CheckBox的文本。

2 个答案:

答案 0 :(得分: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");
                    }
                }
            });

答案 1 :(得分: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 
}

在适配器类中:

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
                }
            });