Lorenzo Longiave
Lorenzo Longiave

Reputation: 35

Android Studio ListView onClickItem

Good morning, I am in despair, I can't create a Listener to the list elements. Should I create a listener that opens me an interface by passing the value of the item clicked, how can I do? Thank you in advance for your help!

package it.a65plus.appseipuntozero;
public class ContactApp extends Activity{
private View mContentView;
private DBHandler db = new DBHandler(this);
private ContactAdapter adapter;
private ListView listView;
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.contact_list);
    ArrayList<Contatto> arrayOfUsers = new ArrayList<Contatto>();
    arrayOfUsers = db.RUB_fetchAllRows();
    // Create the adapter to convert the array to views
    adapter = new ContactAdapter(this, arrayOfUsers);
  // Attach the adapter to a ListView
     listView = (ListView) findViewById(R.id.lvUsers);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i,long l) {
            Log.d("Rubrica: ",String.valueOf(adapterView.getItemAtPosition(i)));
        }
    });
    listView.setAdapter(adapter);

    mContentView = findViewById(R.id.textView9);
    mContentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //toggle();
        }
    });



}

@Override
protected void onResume() {
    super.onResume();
    ApplyCycle.activityResumed();
}

@Override
protected void onPause() {
    super.onPause();
    ApplyCycle.activityPaused();
}

xml_

            <ListView
                android:id="@+id/lvUsers"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_weight="1"
                android:nestedScrollingEnabled="true">
            </ListView>

Adapter

public class ContactAdapter extends ArrayAdapter<Contatto> {
public ContactAdapter(Context context, ArrayList<Contatto> users) {
    super(context, 0, users);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Contatto user = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_row, parent, false);
    }
    // Lookup view for data population
    TextView tvName = (TextView) convertView.findViewById(R.id.name);
    TextView tvHome = (TextView) convertView.findViewById(R.id.phonenumber);
    // Populate the data into the template view using the data object
    String I=new Integer(user.GetIDAccount()).toString();
    tvName.setText(user.GetUserName());
    tvHome.setText(I);
    // Return the completed view to render on screen
    return convertView;
}
}

Upvotes: 0

Views: 124

Answers (1)

Lorenzo Longiave
Lorenzo Longiave

Reputation: 35

solved.

public class ContactAdapter extends ArrayAdapter<Contatto> {
public ContactAdapter(Context context, ArrayList<Contatto> users) {
    super(context, 0, users);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Contatto user = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_row, parent, false);
    }
    // Lookup view for data population
    TextView tvName = (TextView) convertView.findViewById(R.id.name);
    TextView tvHome = (TextView) convertView.findViewById(R.id.phonenumber);
    // Populate the data into the template view using the data object
    String I=new Integer(user.GetIDAccount()).toString();
    tvName.setText(user.GetUserName());
    tvHome.setText(I);
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("putta","lol "+position);
        }
    });
    // Return the completed view to render on screen
    return convertView;
}

}

Upvotes: 1

Related Questions