android onListItem单击活动扩展列表活动

anauzrmj  于 2022-12-31  发布在  Android
关注(0)|答案(3)|浏览(91)

我尝试使用列表视图,但当我单击某个元素时,没有我的吐司

public class MarkersListActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] titles = new String[] {"Titre 1", "Titre 2", "Titre 3", "Titre 4"};
        String[] des = new String[] {"des 1", "des 2", "des 3", "des 4"};

        setListAdapter( new MarkerListArrayAdapter(this, titles, des));

}

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

    //get selected items
    String selectedValue = (String) getListAdapter().getItem(position);
    Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}

标记列表数组适配器

public class MarkerListArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] titleText;
private final String[] descriptionText;

public MarkerListArrayAdapter(Context context, String[] titles, String[] description) {
    super(context, R.layout.marker_list_layout, titles);
    this.context = context;
    this.titleText = titles;
    this.descriptionText = description;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.marker_list_layout, parent, false);
    TextView title = (TextView) rowView.findViewById(R.id.title);
    TextView description = (TextView) rowView.findViewById(R.id.description);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.image);

    title.setText(titleText[position]);
    description.setText(descriptionText[position]);

    imageView.setBackgroundColor(Color.argb(255,255,0,0));

    return rowView;
}
}

该xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:src="@drawable/transparent" >
</ImageView>

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30sp" >
    </TextView>

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="15sp" />

</LinearLayout>

</LinearLayout>

谢啦,谢啦

kupeojn6

kupeojn61#

您应该使用:

public void onItemClick(AdapterView<?> parent, View v, int position, long id)

方法。
让您Activityimplements AdapterView.OnItemClickListener
然后在onCreate中添加以下命令:

list.setOnItemClickListener(this);
00jrzges

00jrzges2#

您尚未调用onListItemClick()super...请执行以下操作:

protected void onListItemClick(ListView l, View v, int position, long id){ 
    super.onListItemClick(l, v, position, id);
}
30byixjq

30byixjq3#

您必须在MarkersListActivity类中设置setContentView(R.layout.activity_main)方法,并且您的列表视图ID应为“@android:id/list”
添加此布局activity_main

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingLeft="8dp"
     android:paddingRight="8dp">

 <ListView android:id="@android:id/list" //Important this one.
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#00FF00"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"/>

 </LinearLayout>

查看listactivity https://developer.android.com/reference/android/app/ListActivity.html的官方文档

相关问题