onListItemClick on Activity扩展了ListActivity

时间:2013-03-17 13:09:53

标签: android listview

我尝试了一个listview,但是当我点击一个元素时,我没有我的祝酒词

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();

    }

}

MarkerListArrayAdapter

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>

由于

3 个答案:

答案 0 :(得分:0)

你应该使用:

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

方法。

让你Activityimplements AdapterView.OnItemClickListener

然后在onCreate添加此命令:

list.setOnItemClickListener(this);

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

你必须在MarkersListActivity类中设置setContentView(R.layout.activity_main)方法,你的listview ID 应该是“@android:id / list”

添加此布局 activity_main

<?php
$username = $POST["username"];
$password = $POST["password"];

mysql_connect("localhost","opticon2_rgstr","mario106");
mysql_select_db("opticon2_rgstr");

$result = mysql_query("select * from users where username='$username' and 
password='$password'");
$row = mysql_fetch_array($result);
if ($row['username'] == $username && $row['password'] == $password)
{
echo "Successfully logged in!";
}
else
{
echo "Username or password are incorrect!";
}
?>

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