我有自定义列表视图。列表视图的每一行都包含一个复选框,2个文本视图和一个图像视图。我一直在点击列表视图的任何一行打开另一个活动,但onItemClick根本不工作。
public class SavedAddress2 extends AppCompatActivity implements AdapterView.OnItemClickListener {
private Toolbar toolbar;
private ListView listView1;
List<list_addr> rowitems;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_address2);
toolbar = (Toolbar) findViewById(R.id.tool_bar3);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
rowitems = new ArrayList<list_addr>();
listView1 = (ListView) findViewById(R.id.addr_list);
list_addr_adapter adapter = new list_addr_adapter(this,
R.layout.items_sav_addr2, SavedAddress.rowitems);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main.this,Second.class);
startActivity(intent);
}
//...
}
是因为列表中有复选框吗?因为我已经为其他列表视图应用了相同的方法,并且它们完美地工作。
答案 0 :(得分:0)
尝试将您的复选框设置为无法调焦,例如:
android:focusable="false"
android:focusableInTouchMode="false"
希望它有所帮助。
答案 1 :(得分:0)
,将此属性添加到根布局
a = 255 - a
如果列表视图有按钮或排序,则焦点位于您案例的项目上,则为复选框 并将其添加到复选框
android:descendantFocusability="blocksDescendants"
答案 2 :(得分:0)
你传递了错误的类上下文。
Intent intent = new Intent( Main.this ,Second.class);
startActivity(意向);
,您的课程为SavedAddress2
将其更改为
Intent intent = new Intent( SavedAddress2.this ,Second.class);
startActivity(意向);
public class SavedAddress2 extends AppCompatActivity implements AdapterView.OnItemClickListener {
private Toolbar toolbar;
private ListView listView1;
List<list_addr> rowitems;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.saved_address2);
toolbar = (Toolbar) findViewById(R.id.tool_bar3);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
rowitems = new ArrayList<list_addr>();
listView1 = (ListView) findViewById(R.id.addr_list);
list_addr_adapter adapter = new list_addr_adapter(this,
R.layout.items_sav_addr2, SavedAddress.rowitems);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Main.this,Second.class);
startActivity(intent);
}
//...
}
请告诉我这是否适合您!:)