나는 Realtive Layout에서 ImageView와 textview를 사용하고 있습니다. 상대 레이아웃의 Onclcik 내가 텍스트보기의 가치를 얻을 필요가 있고 이미지 뷰를 변경해야합니다. Null 포인터 예외가 발생합니다. 여기에 어떤 도움을 주시면 감사하겠습니다클릭하면 RelativeLayout에서 ImageView와 TextView를 가져옵니다.
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int id=0;
for (int idx = 0; idx < dataFromDb.size(); idx++) {
id = idx + 1;
rel = (RelativeLayout) inflater.inflate(
R.layout.newdata, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 50, 0, 0);
txt = (TextView) rel.findViewById(R.id.fetchData);
txt.setText(dataFromDb.get(idx));
txt.setId(id);
img = (ImageView) rel.findViewById(R.id.tickbox);
img.setImageResource(R.drawable.tickbox1a);
img.setId(id);
rel.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(DoTask.this,
"Selected" + v.getId(),
Toast.LENGTH_LONG).show();
RelativeLayout im=(RelativeLayout)v;
ImageView i=(ImageView)im.findViewById(im);
i.setImageResource(R.drawable.tickbox1b);
}
});
content.addView(rel, params);
}
newData.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/tickbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/tickbox1a"
android:paddingBottom="10dp" />
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fetchData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:paddingTop="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/gray"
android:textSize="18sp" />
</RelativeLayout>
코드
활동 코드를입니다.
가 온 클릭 방법 – Badrinath
에서 예외가 왜 당신이 당신의 데이터베이스에서 루프 많은 레이아웃을 추가합니까하기해야 하는가? 나는 당신이'ListView'를 사용해야한다고 생각합니다. 그리고 당신의'OnClick',''새로운 OnClickListener' ('android.view.View.OnClickListener'에서'Dialog.OnClickListener'가 아니라 )가 아닌 것 같아요'new ImageView.OnClickListener()' –