0

我使用以下教程链接http://www.ezzylearning.com/tutorial.aspx?tid=1763429创建了一个列表视图

结果是,活动列表如下图 在此处输入图像描述

现在,我想通过单击单个行导航到不同的活动屏幕。我确实有导航到下一个屏幕的想法,但是我无法找到每一行的正确位置。我对android了解不多,所以如果我的问题太愚蠢,请直言不讳。

虽然代码在上面的链接中可用,但我附上了 .xml 文件和 .java。这里也是。

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"> 

     <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

listview_item_row.xml

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

     <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

     <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

天气.java

public class Weather {
    public int icon;
    public String title;
    public Weather(){
        super();
    }

    public Weather(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }
}

WeatherAdapter.java

public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;

    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data[position];
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

MainActivity.java

public class MainActivity extends Activity {

    private ListView listView1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Weather weather_data[] = new Weather[]
        {
            new Weather(R.drawable.weather_cloudy, "Cloudy"),
            new Weather(R.drawable.weather_showers, "Showers"),
            new Weather(R.drawable.weather_snow, "Snow"),
            new Weather(R.drawable.weather_storm, "Storm"),
            new Weather(R.drawable.weather_sunny, "Sunny")
        };

        WeatherAdapter adapter = new WeatherAdapter(this, 
                R.layout.listview_item_row, weather_data);


        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);

        listView1.setAdapter(adapter);
    }

任何帮助将不胜感激谢谢

4

5 回答 5

4

你需要附上OnItemClickListener

listView1.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView <?> adapterView, View view, int position, long id) {
        // position is index of the row that was clicked
    }
});
于 2012-06-28T10:22:07.577 回答
1

在哪里listView1.setOnItemClickListener?你有没有设置setOnItemClickListener to listView1?.和在下面的代码 onItemClick 有position

listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

            }
        });
于 2012-06-28T10:20:33.160 回答
1

你应该做这样的事情

listView1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //do something
        }
    });
于 2012-06-28T10:20:37.787 回答
1

使用 setOnItemSelectedListener 和列表http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html

listView1.addHeaderView(header);
listView1.setAdapter(adapter);

listView1.setOnItemSelectedListener(
    new OnItemSelectedListener() {
        @Override
        public void onItemSelected(
            AdapterView<?> parent, View view, int pos, long id
        ) {
             //where pos is your index 
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {}

    }
);
于 2012-06-28T10:22:21.163 回答
1

试试这个方法...

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {


    Toast.makeText(context, position+"", Toast.LENGTH_LONG).show();

                                Intent go_detail_Activity_Intent= new Intent(context,Target_Activity.class);
                                go_detail_Activity_Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                context.startActivity(go_detail_Activity_Intent);


                        }
                    });

我认为它对你有帮助。

于 2012-06-28T10:23:50.577 回答