我输了,所以我可以请一些关于如何将列表项链接到单独的活动的帮助吗?我只是希望能够在点击行时执行操作,并且数据是行包含任何想法吗?谢谢 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;
}
}
}
布局&GT; 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>
感谢。
答案 0 :(得分:3)
您需要实施onListItemClick(....)
。您可以在Activity
。
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// ListView Clicked item index
int itemPosition=position;
// ListView Clicked item value
String itemValue=(String)l.getItemAtPosition(position);
Toast.makeText(yourActivity.this,"Click :Position :"+itemPosition+"ListItem : " +itemValue,Toast.LENGTH_SHORT).show();;
}
点击list item
后,调用onItemClick
中的ListView Item Click Listener
方法
然后传递您Intent
中的选定值。
答案 1 :(得分:0)
您想要做的是以下内容:
1。)实现ListActivity的onListItemClick函数。
@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&#39; class必须实现Parcelable接口。这可以使用Parcelabler自动执行,此处为:http://www.parcelabler.com/
3。)将项目发送到意图中的新活动。
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("argument", myItem);
startActivity(intent);
4.。)在你的其他活动中,从意图中获取Parcelable。
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查找视图,并根据Intent中收到的项设置其参数。
编辑:这是我根据你的东西放在一起的样本:
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:
<?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:
<?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:
<?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>