gpt4 book ai didi

java - 带有自定义 ArrayAdapter setOnItemClickListener 的 ListView 不会触发

转载 作者:行者123 更新时间:2023-12-02 04:48:37 25 4
gpt4 key购买 nike

我不明白为什么 setOnItemClickListener 不起作用。我有带有自定义 ListItem 布局的 ListView,并且我想在用户单击行时启动其他 Activity 。

我看到了很多有关此问题的主题,但没有任何帮助。

屏幕:

enter image description here

activity_main.xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/notes_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/addNote">
</ListView>

<TextView
android:id="@+id/empty_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/empty_notes"
android:visibility="gone"
android:gravity="center"
android:layout_above="@+id/addNote"
/>

<Button
android:id="@+id/addNote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_note"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

list_row.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="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >

<!-- Note text-->
<TextView
android:id="@+id/noteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold" />

<!-- Note createdAt -->
<TextView
android:id="@+id/noteCreatedAt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/noteText"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip" />

<!-- Note complete -->
<Button
android:id="@+id/btnCompleteNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Complete" />
</RelativeLayout>

RNoteAdapter.java:

public class RNoteAdapter extends ArrayAdapter<RNote> {
private Context context;
private int resource;
RNote[] data;
private RNoteRepository noteRepository;

public RNoteAdapter(Context context, int resource, RNote[] data) {
super(context, resource, data);
this.context = context;
this.resource = resource;
this.data = data;
this.noteRepository = new RNoteRepository(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RNoteHolder holder = null;

if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);

holder = new RNoteHolder();
holder.noteText = (TextView)row.findViewById(R.id.noteText);
holder.noteCreatedAt = (TextView)row.findViewById(R.id.noteCreatedAt);
holder.btnCompleteNote = (Button)row.findViewById(R.id.btnCompleteNote);

row.setTag(holder);
} else {
holder = (RNoteHolder)row.getTag();
}

final RNote note = data[position];

if(note.IsCompleted) {
holder.noteText.setPaintFlags(holder.noteText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
holder.noteText.setText(note.Text);

DateFormat df = new SimpleDateFormat("dd.MM.yyyy в HH:mm");
holder.noteCreatedAt.setText(df.format(note.CreatedAtUtc));

holder.btnCompleteNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
note.IsCompleted = true;
noteRepository.save(note);

RNoteAdapter.this.notifyDataSetChanged();
}
});

return row;
}

static class RNoteHolder {
TextView noteText;
TextView noteCreatedAt;
Button btnCompleteNote;
}
}

MainActivity.java

public class MainActivity extends ActionBarActivity {
private RNoteRepository _noteRepository = new RNoteRepository(this);

private Button addButton;
private ListView notesList;

RNoteAdapter adapter;

private void init() {
notesList = (ListView)findViewById(R.id.notes_list);
addButton = (Button)findViewById(R.id.addNote);

notesList.setEmptyView(findViewById(R.id.empty_list_item));
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showEditNoteActivity(0);
}
});

notesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showEditNoteActivity(id);
}
});
}

private void updateData() {
List<RNote> list = _noteRepository.getAll();
RNote[] array = list.toArray(new RNote[list.size()]);

adapter = new RNoteAdapter(this, R.layout.list_row, array);
notesList.setAdapter(adapter);
}

private void showEditNoteActivity(long noteId) {
Intent intent = new Intent(getApplicationContext(), EditNoteActivity.class);
intent.putExtra("noteId", noteId);
startActivityForResult(intent, 1);
}

private void showDialog(String text) {
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
return;
}
String msg = data.getStringExtra("response");
showDialog(msg);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init();
}

@Override
protected void onResume() {
super.onResume();
updateData();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

添加

android:descendantFocusability="blocksDescendants" 

到 ListView 的行根。

ArrayAdaptergetItemId 实现返回位置,而不是 RNote 的 id,因此您可能需要重写它以使其返回正确信息

关于java - 带有自定义 ArrayAdapter setOnItemClickListener 的 ListView 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29461208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com