0

次のチュートリアル リンクを使用してリストビューを作成しましたhttp://www.ezzylearning.com/tutorial.aspx?tid=1763429

この結果は、下の画像のようなアクティビティのリストです ここに画像の説明を入力

ここで、個々の行をクリックして別のアクティビティ画面に移動したいと考えています。次の画面に移動するという考えはありますが、各行の正しい位置を見つけることができません。私はアンドロイドについてあまり知識がないので、あまりにもばかげている場合は質問をしてください。

コードは上記のリンクから入手できますが、.xml ファイルと .java を添付しています。ここでも。

main.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

リストhttp://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.htmlでsetOnItemSelectedListenerを使用します

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 に答える
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 に答える