我有一个listview
,因为我正在使用BaseAdapter
。我希望listitem的值在Activity
中。以下是我的代码
MainActivity.java
ArrayList<String> dkulist = new ArrayList<>();
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
userList = (ListView) findViewById(R.id.userList);
userList.setAdapter(adapter);
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView,final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.listitem, parent, false);
}
TextView Name = (TextView) convertView.findViewById(R.id.name);
Name.setText("Dummy name");
final boolean click_flag = false;
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(click_flag) {
view.setBackgroundColor(Color.BLUE); // this is also not working
// How to get this name value in my MainActivity.java
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
}
}
感谢Adv
答案 0 :(得分:2)
使用Interface
将值传递给Activity
public interface OnItemClickListener {
void onItemClick (Object value);
}
在Activity
中实现上述侦听器,并将其实例传递给适配器
private final OnItemClickListener mListener;
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setBackgroundColor(Color.BLUE); // this will work now
// This is how to get this name value in my MainActivity.java
mListener.onItemClick(value);
}
答案 1 :(得分:0)
您需要初始化Adapter的实例并设置为userlist,而不是仅传递未分配的适配器变量。
答案 2 :(得分:0)
活动代码:
public class MainActivity implements CustomAdapter. OnItemClickListener
ArrayList<String> dkulist = new ArrayList<>();
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
userList = (ListView) findViewById(R.id.userList);
userList.setAdapter(adapter);
}
@override
public void onItemClick (Object value){
// do your stuff here
}
}
适配器代码:
public class CustomAdapter extends BaseAdapter {
private final OnItemClickListener mListener;
public CustomAdapter(Context context){
// this will register the listener with MainActivity.java
mListener = (OnItemClickListener) context
}
@Override
public View getView(int position, View convertView,final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.listitem, parent, false);
}
TextView Name = (TextView) convertView.findViewById(R.id.name);
Name.setText("Dummy name");
final boolean click_flag = false;
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(click_flag) {
view.setBackgroundColor(Color.BLUE); // this is also not working
// How to get this name value in my MainActivity.java
// Add below code to delegate the value to activity
mListener.onItemClick(value);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
}
public interface OnItemClickListener {
void onItemClick (Object value);
}
}
答案 3 :(得分:0)
试试这个
在您的MainAcitivity.java中,
userList.setOnItemClickListener(new ListItemClickListener());
private class ListItemClickListenerimplements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String text = ((TextView) view.findViewById(R.id.name)).getText().toString();
}
}