我有一个列表视图如何连接到另一个活动 [英] I Have a listview how to connect that to another activity

查看:136
本文介绍了我有一个列表视图如何连接到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迷路了,这样我就可以请有如何链接列表项,以单独的活动帮助吗?我只是希望能够做一个动作,当我点击一行,该行包含数据当然,任何想法?谢谢
main.java:

I'm lost, so can I please have some help on how to link the list items to seperate activities?I just want to be able to do an action when I click on the row, with the data that the row contains of course any ideas? thanks main.java:

package com.sevenhack;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setListAdapter(new MyAdapter(this, 
                android.R.layout.simple_list_item_1, R.id.textView1,
                getResources().getStringArray(R.array.itemlist)));
    }

private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
        // TODO Auto-generated constructor stub
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.itemlist, parent , false);
        String[] items = getResources().getStringArray(R.array.itemlist);
        ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        tv.setText(items[position]);



        return row;
    }

}

}

布局> itemlist.xml:

layout>itemlist.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_marginTop="100dp"
    >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" android:src="@drawable/ic_securee"/>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_toLeftOf="@+id/imageView1"
    android:text="Large Text" android:layout_marginBottom="10dp"    
    android:layout_marginRight="5dp" android:textSize="20dp"/>

</RelativeLayout>


感谢。

推荐答案

您想要做的是以下内容:

What you want to do is the following:

1)实施ListActivity的onListItemClick功能。

1.) Implement the onListItemClick function of the ListActivity.

@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
   super.onListItemClick(l, v, position, id);
   MyItem myItem = (MyItem) this.getListAdapter().getItem(position);
   ... //will need to add more things here
}

2)你的'MyItem类必须实现Parcelable接口。这可以用Parcelabler,这里实现自动化: http://www.parcelabler.com/

3)发送的项目,新活动在意向。

3.) Send the item to the new Activity in an Intent.

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("argument", myItem);
startActivity(intent);

4)。在你的其他活动,从意图得到Parcelable。

4.) In your other activity, get the Parcelable from the Intent.

public class OtherActivity extends Activity
{
   private MyItem myItem;

   @Override
   public void onCreate(Bundle onSaveInstance)
   {
      super.onCreate(onSaveInstance);
      this.myItem = (MyItem) getIntent().getExtras().getParcelable("argument");
      ...
   }
}

5)你想与其他活动的项目是什么,比如显示其数据。为此,指定布局XML,并在的onCreate(...)函数找到ID的意见,并设置收到意向根据项目及其参数。

5.) Do what you want with the item in the other activity, such as display its data. For that, specify the layout XML, and in onCreate(...) function find the views by ID and set their parameters based on item received in Intent.

编辑:下面是一个示例我放在一起根据你的东西,它的工作原理:

Here's a sample I put together based on your stuff which works:

public class MyListActivity extends ListActivity
{
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setListAdapter(new MyAdapter(this, R.layout.itemlist, R.id.textView1, getResources()
            .getStringArray(R.array.hello_world)));
   }

   @Override
   public void onListItemClick(ListView l, View v, int position, long id)
   {
      super.onListItemClick(l, v, position, id);
      String myItem = (String) this.getListAdapter().getItem(position);
      Intent intent = new Intent(this, OtherActivity.class);
      intent.putExtra("hello", myItem);
      startActivity(intent);
   }

   private class MyAdapter extends ArrayAdapter<String>
   {

      public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings)
      {
         super(context, resource, textViewResourceId, strings);
         // TODO Auto-generated constructor stub
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent)
      {
         LayoutInflater inflater = (LayoutInflater)     getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View row = inflater.inflate(R.layout.itemlist, parent, false);
         String[] items = getResources().getStringArray(R.array.hello_world);
         TextView tv = (TextView) row.findViewById(R.id.textView1);
         tv.setText(items[position]);

         return row;
      }
   }
}

下一步:

public class OtherActivity extends Activity
{
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      String data = getIntent().getExtras().getString("hello");
      System.out.println(data);
   }
}

/res/values​​/array.xml:

/res/values/array.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="hello_world">
        <item name="hello">Hello</item>
        <item name="world">World</item>
    </string-array>
</resources>

/res/layout/itemlist.xml:

/res/layout/itemlist.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_marginTop="100dp"
    >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"    
    android:layout_marginRight="5dp" android:textSize="20dp"/>

</RelativeLayout>

AndroidManifest.xml中:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.exa.ex.testproject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.exa.ex.testproject.MyListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.exa.ex.testproject.OtherActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

这篇关于我有一个列表视图如何连接到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆