Android Studio ListView onClickItem

时间:2016-06-30 09:05:34

标签: android xml

早上好,我绝望了,我无法创建列表元素的监听器。 我应该创建一个通过传递单击项的值来打开界面的侦听器,我该怎么办? 提前感谢您的帮助!

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>

适配器

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;
}
}

1 个答案:

答案 0 :(得分:1)

解决。

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;
}

}