Reputation: 183
I have a listview
and for that i am using a BaseAdapter
. I want the value of listitem in that Activity
. Below is my code
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);
}
}
}
Thank in Adv
Upvotes: 1
Views: 2170
Reputation: 599
Activity code:
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
}
}
Adapter code:
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);
}
}
Upvotes: 0
Reputation: 11062
Use an Interface
to pass the value to Activity
public interface OnItemClickListener {
void onItemClick (Object value);
}
Implement the above listener in the Activity
and pass an instance of this to the adapter
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);
}
Upvotes: 2
Reputation: 1845
Try this
In your 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();
}
}
Upvotes: 0
Reputation: 360
You need to initialize instance for the Adapter and set to the userlist instead of just passing the unassigned adapter variable.
Upvotes: 0