ListView的item监听事件,并且把值传递给另一个activity

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView的item监听事件,并且把值传递给另一个activity相关的知识,希望对你有一定的参考价值。

技术分享
 
技术分享
思路:
先从服务器那里得到数据newsInfoList
在得到每一个位置的news集合
传递数据给另一个activity
 
关键代码:
lvnews.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
News news=newsInfoList.get(position);//关键代码
Intent intent = new Intent(MainActivity.this,EachNews.class);
intent.putExtra("tv_title", news.getTitle());
intent.putExtra("iv", news.getImageUrl());
intent.putExtra("tv_detail", news.getDetail());
intent.putExtra("tv_comment", news.getComment());
startActivity(intent);
}
});

从另一个activity得到数据

SmartImageView siv = (SmartImageView)findViewById(R.id.iv);
TextView tv_title = (TextView) findViewById(R.id.tv_title);
TextView tv_detail = (TextView) findViewById(R.id.tv_detail);
TextView tv_comment = (TextView) findViewById(R.id.tv_comment);
Intent intent = getIntent();

String iv= intent.getStringExtra("iv");
String title= intent.getStringExtra("tv_title");
String detail= intent.getStringExtra("tv_detail");
String comment= intent.getStringExtra("tv_comment");

tv_detail.setText(comment+"条评论");
tv_detail.setText(detail);
tv_title.setText(title);
siv.setImageUrl(iv);
完整代码
第一个activity
package com.example.news;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.domain.News;
import com.loopj.android.image.SmartImageView;

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";
protected static final int SUCCESS = 0;
protected static final int FAILED = 1;
private ListView lvnews;
private List<News>newsInfoList;
private TextView tv_title;
News news;
private Cursor cursor;
private Handler handler=new Handler(){

@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESS:
newsInfoList=(List<News>) msg.obj;
Myadater myadater = new Myadater();
lvnews.setAdapter(myadater);
break;
case FAILED:
Toast.makeText(MainActivity.this, "网络没有连接!!!", 0).show();
break;
default:
break;
}
}

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 
getNewsInfo();
//给listview设置监听
lvnews.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
News news=newsInfoList.get(position);
Intent intent = new Intent(MainActivity.this,EachNews.class);
intent.putExtra("tv_title", news.getTitle());
intent.putExtra("iv", news.getImageUrl());
intent.putExtra("tv_detail", news.getDetail());
intent.putExtra("tv_comment", news.getComment());
startActivity(intent);
}
});    
}

class Myadater extends BaseAdapter{

@Override
public int getCount() {
// TODO Auto-generated method stub
return newsInfoList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=null;    
News news=newsInfoList.get(position);
if(convertView==null){
LayoutInflater inflater = getLayoutInflater();
v= inflater.inflate(R.layout.item_listview, null);
}else{
v=convertView;
}
SmartImageView siv = (SmartImageView)v.findViewById(R.id.iv);
siv.setImageUrl(news.getImageUrl());
TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
tv_title.setText(news.getTitle());
TextView tv_detail = (TextView) v.findViewById(R.id.tv_detail);
tv_detail.setText(news.getDetail());
TextView tv_comment = (TextView) v.findViewById(R.id.tv_comment);
tv_detail.setText(news.getComment()+"条评论");    
return v;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

}
private void getNewsInfo() {
lvnews = (ListView) findViewById(R.id.lv);
new Thread(
new Runnable() {    
@Override
public void run() {
List<News> newsInfoList=getNewsFromIntent();
Message msg = new Message();
if(newsInfoList!=null){
msg.what=SUCCESS;
msg.obj=newsInfoList;
}else{
msg.what=FAILED;
}
handler.sendMessage(msg);    
}
}
).start();    
}

private List<News> getNewsFromIntent() {    
HttpClient client=null;
try {
client=new DefaultHttpClient();
HttpGet get=new HttpGet("http://xxxxxxxxx.28:8080/News/news.xml");//必须用本机iP地址模拟机会把localhost当做自己的地址
HttpResponse response=client.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode==200){
InputStream is = response.getEntity().getContent();
List<News> newsInfoList = getNewsListFromInputStream(is);
return newsInfoList;
}
else{
Log.i(TAG, "访问失败!!!"+statusCode);
}
} catch (Exception e) {    
e.printStackTrace();
}finally{
if(client!=null){
client.getConnectionManager().shutdown();
}
}    
return null;
}

private List<News> getNewsListFromInputStream(InputStream is) throws Exception {
XmlPullParser xp = Xml.newPullParser();
xp.setInput(is, "utf-8");
int type = xp.getEventType();
List<News> newsInfoList=null;
News news=null;
while(type!=XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
if("newlist".equals(xp.getName())){
newsInfoList=new ArrayList<News>();
}else if("news".equals(xp.getName())){
news=new News();
}else if("title".equals(xp.getName())){
news.setTitle(xp.nextText());
}else if("detail".equals(xp.getName())){
news.setDetail(xp.nextText());
}else if("comment".equals(xp.getName())){
news.setComment(xp.nextText());
}else if("image".equals(xp.getName())){
news.setImageUrl(xp.nextText());
}    
break;
case XmlPullParser.END_TAG:
if("news".equals(xp.getName())){
newsInfoList.add(news);
}
break;

default:
break;
}
type = xp.next();
}
return newsInfoList;
} 
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ListView 
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>

</RelativeLayout>

eachnews.xml

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

<com.loopj.android.image.SmartImageView 
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
/>
<TextView 
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是大标题志哥教你上塑料adasfsadfdsfdsgsd"
android:textSize="22sp"
android:singleLine="true"
/>
<TextView 
android:id="@+id/tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是正文志哥教你带崩三路adasfsadasdasdasdasidhsakjhdkjashdkjahskjdhsakjdfdsfdsgsd"
android:textSize="15sp"
android:textColor="@android:color/darker_gray"
android:lines="2"
/>
<TextView 
android:layout_marginLeft="230dip"
android:id="@+id/tv_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="65031条评论"
android:textColor="#ff0000"
/>
</LinearLayout>

item_listview.xml

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

<com.loopj.android.image.SmartImageView 
android:id="@+id/iv"
android:layout_width="90dp"
android:layout_height="70dp"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
/>
<TextView 
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是大标题志哥教你上塑料adasfsadfdsfdsgsd"
android:layout_toRightOf="@id/iv"
android:textSize="22sp"
android:singleLine="true"
/>
<TextView 
android:id="@+id/tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是正文志哥教你带崩三路adasfsadasdasdasdasidhsakjhdkjashdkjahskjdhsakjdfdsfdsgsd"
android:layout_toRightOf="@id/iv"
android:layout_below="@id/tv_title"
android:textSize="15sp"
android:textColor="@android:color/darker_gray"
android:lines="2"
/>
<TextView 
android:id="@+id/tv_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="65031条评论"
android:textColor="#ff0000"
android:layout_alignParentRight="true"
android:layout_below="@id/tv_detail"
/>
</RelativeLayout>

另一个activity

package com.example.news;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;

public class EachNews extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.eachnews);
SmartImageView siv = (SmartImageView)findViewById(R.id.iv);
TextView tv_title = (TextView) findViewById(R.id.tv_title);
TextView tv_detail = (TextView) findViewById(R.id.tv_detail);
TextView tv_comment = (TextView) findViewById(R.id.tv_comment);
Intent intent = getIntent();

String iv= intent.getStringExtra("iv");
String title= intent.getStringExtra("tv_title");
String detail= intent.getStringExtra("tv_detail");
String comment= intent.getStringExtra("tv_comment");

tv_detail.setText(comment+"条评论");
tv_detail.setText(detail);
tv_title.setText(title);
siv.setImageUrl(iv);
}

}

 

以上是关于ListView的item监听事件,并且把值传递给另一个activity的主要内容,如果未能解决你的问题,请参考以下文章

ListView Item点击监听事件失效

c# winform 中如何写事件监听 比如监听listview.items.count是不是发生变化

Listview的条目item内的点击响应事件

android listview里面能嵌套gridview吗

给Listview里面Item里的每一个控件怎么设定点击事件

怎样在Listview中分组显示Item.并且可以折叠