ImageButton이있는 행이있는 ListView가 있습니다. 특정 행을 클릭하면 작동합니다. 새로운 예상 활동이 시작됩니다. 그러나 ImageButton을 클릭하면 이것이 행 내의 항목이면 아무 일도 일어나지 않습니다. imageButton이 강조 표시되지만 ImageButton의 onClick 내에서 인쇄는 실행되지 않습니다. 누구든지 내가 어떻게 해결할 수 있는지 말해 줄 수 있습니까? 내 코드는 다음과 같습니다.
SimpleCursorAdapter menuItems2 = new SimpleCursorAdapter(
this, R.layout.todo_row, matrixCursor, columnNames, to);
ToDolv.setAdapter(menuItems2);
ToDolv.setClickable(true);
ToDolv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {
final String article = (String) todoIDArray.get(position);
globalVariable.setArtID(article);
Intent intent1 = new Intent(MainActivity.this, AddToDo.class);
startActivity(intent1);
finish();
ImageButton chkDone = (ImageButton) findViewById(R.id.chkDone);
chkDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
System.out.println("I am in position "+ position);
}
});
}
});
다음은 ImageButton이있는 행에 대한 XML입니다.
<?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"
android:background = "#5a9b3e"
android:alpha = "0.7"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="@+id/id"
android:textColor="#5a9b3e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/heading"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:alpha = "1.0"
android:layout_alignParentTop="true"
android:ellipsize="marquee"
android:singleLine="false"
android:textSize="12sp"
/>
<ImageView
android:id="@+id/icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:padding="2dp"
android:layout_toRightOf="@+id/heading"
android:layout_toEndOf="@+id/heading"
/>
<ImageView
android:id="@+id/lights"
android:layout_width= "20dp"
android:layout_height="20dp"
android:padding="2dp"
android:layout_toRightOf="@+id/icon"
android:layout_toEndOf="@+id/icon"
/>
<ImageButton
android:id="@+id/chkDone"
android:layout_width="20dp"
android:layout_height="20dp"
android:padding="0dp"
android:background="?android:attr/listChoiceBackgroundIndicator"
android:layout_toRightOf="@+id/lights"
android:layout_toEndOf="@+id/lights"
android:src ="@drawable/checkbox"
android:scaleType="fitXY"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:alpha = "1.0"
android:layout_marginStart="2sp"
android:layout_marginLeft="2sp"
android:layout_marginTop="2sp"
android:textSize="12sp"
android:layout_toRightOf="@+id/chkDone"
android:layout_toEndOf="@+id/chkDone"/>
</RelativeLayout>
매우 감사합니다!
편집하다
제안 사항에 따라 행을 표시하기 위해 사용자 지정 어댑터 클래스를 만들었습니다. 그래서 다음과 같이 코드를 변경했습니다. 이제 Main 활동에 다음 코드 줄이 있습니다.
ListView yourListView = (ListView) findViewById(R.id.list);
SimpleCursorAdapter menuItems2 = new SimpleCursorAdapter(
this, R.layout.todo_row, matrixCursor, columnNames, to);
CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, R.layout.todo_row, menuItems2);
yourListView .setAdapter(customAdapter);
그리고 CustomListViewAdapter는 다음과 같습니다.
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomListViewAdapter(Context context, int resourceId,
SimpleCursorAdapter menuItems2) {
super(context, resourceId);
this.context = context;
System.out.println("I am in the custom Adapter class "+ context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
System.out.println("This is the get view");
View row = convertView;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.todo_row, parent, false);
}
ImageButton chkDone = (ImageButton) row.findViewById(R.id.chkDone);
chkDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
System.out.println("I am in position "+ position);
}
});
return row;
}
}
XML은 동일합니다.
컴파일되지만 아무 작업도 수행하지 않고 행도 표시하지 않습니다. matrixCursor를 빌드하는 코드를 게시해야하는지 알려주세요.
대단히 감사합니다.
목록을 작성하는 데 사용할 수있는 개체 목록을 허용하는 사용자 지정 어댑터를 만들어야합니다.
목록에 필요한 정보를 포함하는 클래스를 만들어야합니다. 당신이 다음과 같이 말한 것에서 :
public class RowItem {
private String _columnName;
private Drawable _drawable;
public RowItem(String columnName, Drawable drawable) {
_columnName = columnName;
_drawable = drawable;
}
public String getColumnName() {
return _columnName;
}
public Drawable getDrawable() {
return _drawable;
}
}
그런 다음 목록에서 원하는 항목에서 이러한 개체를 만듭니다.
ArrayList<RowItem> rowItems = new ArrayList<>();
//Create a number of row items and add them to your list
RowItem myItem1 = new RowItem("String I want for column name", myDrawable);
rowItems.add(myItem1);
RowItem 개체 목록을 만든 후 다음과 같이 사용자 지정 어댑터를 만듭니다.
CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, R.layout.todo_row, rowItems);
그런 다음 이전에 시도했던 것처럼 생성자를 통해 사용자 지정 어댑터에 RowItem 개체 목록을 전달할 수 있습니다.
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
ArrayList<RowItem> _rowItems;
public CustomListViewAdapter(Context context, int resourceId,
ArrayList<RowItem> rowItems) {
super(context, resourceId);
this.context = context;
_rowItems = rowItems;
System.out.println("I am in the custom Adapter class "+ context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
System.out.println("This is the get view");
View row = convertView;
RowItem item = _rowItems.get(position);
// you can now get your string and drawable from the item
// which you can use however you want in your list
String columnName = rowItem.getColumnName();
Drawable drawable = rowItem.getDrawable();
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.todo_row, parent, false);
}
ImageButton chkDone = (ImageButton) row.findViewById(R.id.chkDone);
chkDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
System.out.println("I am in position "+ position);
}
});
return row;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다