当我单击列表中的元素时,我试图从gridView获取TextView的值。适配器由2个textViews组成。这是ListAdapter类。
public class ListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<Customer> CustomerList;
public ListAdapter(Context CustomerList, int customer_activity, ArrayList<Customer> Customer) {
this.context = CustomerList;
this.layout = customer_activity;
this.CustomerList = Customer;
}
@Override
public int getCount() {
return CustomerList.size();
}
@Override
public Object getItem(int position) {
return CustomerList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
TextView textViewJob, textViewName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if(row == null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout,null);
holder.textViewJob = (TextView)row.findViewById(R.id.tvJobAdap);
holder.textViewName = (TextView)row.findViewById(R.id.tvNameAdap);
row.setTag(holder);
}
else {
holder = (ViewHolder)row.getTag();
}
Customer Customer = CustomerList.get(position);
holder.textViewJob.setText(Customer.getJob());
holder.textViewName.setText(Customer.getName());
return row;
}
}
当我单击列表中的元素时(例如,在第三位置),我想获取与该ID对应的TextViewName(Customer.getName())。我试图在Toast.makeText的gridViewCustomer.setOnItemClickListener中解释它
GridView gridViewCustomer;
ArrayList<Customer> customer;
ListeAdapter adapter = null;
public String selectedItemText;
//Referenz of the SQLiteHelper class
public static SQLiteHelper sqLiteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Customer_activity);
sqLiteHelper = new SQLiteHelper(CustomerListe.this, "Customer.sqlite", null, 3);
//Customer List
gridViewCustomer = (GridView) findViewById(R.id.gvCustomerList);
customer = new ArrayList<>();
adapter = new ListeAdapter(this, R.layout.customer_activity, customer);
gridViewCustomer.setAdapter(adapter);
gridViewCustomer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
Toast.makeText(ListActivity.this, "You have clicked on the element with id =" + id, Toast.LENGTH_SHORT).show();
Toast.makeText(ListActivity.this, "Please how could I get the TextView (TextViewName) corresponding to that id ?", Toast.LENGTH_SHORT).show();
}
});
}
答案 0 :(得分:0)
这应该有效:
gridViewCustomer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
Toast.makeText(ListActivity.this, "You have clicked on the element with id =" + id, Toast.LENGTH_SHORT).show();
Toast.makeText(ListActivity.this, "You have clicked on the element with name=" + customer.get(position).getName(), Toast.LENGTH_SHORT).show();
}
});
希望这会有所帮助
答案 1 :(得分:0)
一种解决方案是,您可以像这样在适配器中创建接口
public interface OnAdapterItemClickListener{
public void click(View view,int position,String name);
}
和适配器内部的类
public void clickItem(OnAdapterItemClickListener listener){
this.listener =listener;
}
在适配器中将其定义为变量
public OnAdapterItemClickListener listerner;
现在在您的BindViewHolder中
holder.parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.click(view,position,name);
}
});
最后一步,您只需要在使用适配器的类中设置接口
chatUsersListAdapter.clickItem(new OnAdapterItemClickListener ChatUsersListAdapter.OnItemClick() {
@Override
public void click(View view,int position, String name) {
//perform any action
}
});