Android / JAVA - 如何在列表视图单元格上设置点击功能
Android / JAVA - How to set click function on List view cell
我正在尝试向以编程方式填充的列表视图添加点击功能。我遵循了一个教程,它没有给我任何错误,并且据我所知它应该可以工作,但是当我点击一个单元格时它完全没有任何作用。没有 crash/action 发生。
这是我的代码:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class domainViewer extends ActionBarActivity {
public List<Domain> domains = new ArrayList<Domain>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_domain_viewer);
populateDomainList();
populateViewList();
RegisterClickCallback();
}
private void populateViewList() {
ArrayAdapter<Domain> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.domainsListView);
list.setAdapter(adapter);
}
private void populateDomainList() {
domains.add(new Domain("cheese.com", true, "€15,50" , R.drawable.deselected));
domains.add(new Domain("cheese.nl", true, "€15,50" , R.drawable.deselected));
}
@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_domain_viewer, 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);
}
private class MyListAdapter extends ArrayAdapter<Domain> {
public MyListAdapter() {
super(domainViewer.this, R.layout.item_view, domains);
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
Domain currentDomain = domains.get(position);
ImageView imageView = (ImageView)itemView.findViewById(R.id.imgSelected);
imageView.setImageResource(R.drawable.deselected);
TextView domainText = (TextView) itemView.findViewById(R.id.tvDomain);
domainText.setText(currentDomain.getDomain());
TextView priceText = (TextView) itemView.findViewById(R.id.tvPrice);
priceText.setText(currentDomain.getPrice());
return itemView;
//return super.getView(position, convertView, parent);
}
}
private void RegisterClickCallback() {
ListView list = (ListView) findViewById(R.id.domainsListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Domain clickedDomain = domains.get(position);
String message = position + " - " + clickedDomain.getDomain();
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
您需要在适配器中实现点击事件 class ,
public OnItemClickListener onItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
}
};
然后在设置适配器的地方创建监听器,
private void populateViewList() {
ArrayAdapter<Domain> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.domainsListView);
list.setAdapter(adapter);
list.setOnItemClickListener(adapter.onItemClick);
}
我正在尝试向以编程方式填充的列表视图添加点击功能。我遵循了一个教程,它没有给我任何错误,并且据我所知它应该可以工作,但是当我点击一个单元格时它完全没有任何作用。没有 crash/action 发生。
这是我的代码:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class domainViewer extends ActionBarActivity {
public List<Domain> domains = new ArrayList<Domain>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_domain_viewer);
populateDomainList();
populateViewList();
RegisterClickCallback();
}
private void populateViewList() {
ArrayAdapter<Domain> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.domainsListView);
list.setAdapter(adapter);
}
private void populateDomainList() {
domains.add(new Domain("cheese.com", true, "€15,50" , R.drawable.deselected));
domains.add(new Domain("cheese.nl", true, "€15,50" , R.drawable.deselected));
}
@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_domain_viewer, 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);
}
private class MyListAdapter extends ArrayAdapter<Domain> {
public MyListAdapter() {
super(domainViewer.this, R.layout.item_view, domains);
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
Domain currentDomain = domains.get(position);
ImageView imageView = (ImageView)itemView.findViewById(R.id.imgSelected);
imageView.setImageResource(R.drawable.deselected);
TextView domainText = (TextView) itemView.findViewById(R.id.tvDomain);
domainText.setText(currentDomain.getDomain());
TextView priceText = (TextView) itemView.findViewById(R.id.tvPrice);
priceText.setText(currentDomain.getPrice());
return itemView;
//return super.getView(position, convertView, parent);
}
}
private void RegisterClickCallback() {
ListView list = (ListView) findViewById(R.id.domainsListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
Domain clickedDomain = domains.get(position);
String message = position + " - " + clickedDomain.getDomain();
Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
您需要在适配器中实现点击事件 class ,
public OnItemClickListener onItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
}
};
然后在设置适配器的地方创建监听器,
private void populateViewList() {
ArrayAdapter<Domain> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.domainsListView);
list.setAdapter(adapter);
list.setOnItemClickListener(adapter.onItemClick);
}