您的位置:首页 > 其它

对listview中的item中的控件进行监听

2012-11-15 19:05 295 查看
对于这个问题,今天我也是反复试验了很多次啊,花了很长一直没有得到解决,网上查了很多资料,但是还是解决不了自己的问题。

题目是这样的:一个QueryActivity。另一个OrderListActivity。




点击查询,讲一个list对象传递给OrderListActivity(这个前面我也分享过具体的代码了)


点击任意一个item中的查询按钮,能够得到移除的效果。
下面分享一下具体地代码
activity_query.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" >

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查询"></Button>
</RelativeLayout>


activity_order_list.xml
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

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

</LinearLayout>


order_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/MyListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip">

<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/edit05"
android:enabled="false"
android:editable="false"
android:focusable="false" />"

<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/edit06"
android:enabled="false"
android:editable="false"
android:focusable="false" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>"
</LinearLayout>


java代码

QueryActivity.java

OrderListActivity.java

package com.example.listview;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class QueryActivity extends Activity {
private Button button;
private List<Map<String, Object>> list;
String result="[{\"city\":\"changsha\",\"street\":\"no2\"},{\"city\":\"beijing\",\"street\":\"no5\"}]";
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
this.button=(Button)findViewById(R.id.button);
button.setOnClickListener(new buttonClick());
String result1="{\"address\":"+result+"}";
Log.d("result1",result1);
try{
JSONObject json=new JSONObject(result1);
JSONArray jsonlist=json.getJSONArray("address");
list = new ArrayList<Map<String, Object>>();
list=JsonHelper.toList(jsonlist);
Log.d("list***",list.toString());
}
catch(Exception e){

}
}
class buttonClick implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(QueryActivity.this,OrderListActivity.class);
intent.putExtra("list", (Serializable)list);
QueryActivity.this.startActivity(intent);
Log.d("list",(Serializable)list+"");package com.example.listview;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class QueryActivity extends Activity {
private Button button;
private List<Map<String, Object>> list;
String result="[{\"city\":\"changsha\",\"street\":\"no2\"},{\"city\":\"beijing\",\"street\":\"no5\"}]";
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
this.button=(Button)findViewById(R.id.button);
button.setOnClickListener(new buttonClick());
String result1="{\"address\":"+result+"}";
Log.d("result1",result1);
try{
JSONObject json=new JSONObject(result1);
JSONArray jsonlist=json.getJSONArray("address");
list = new ArrayList<Map<String, Object>>();
list=JsonHelper.toList(jsonlist);
Log.d("list***",list.toString());
}
catch(Exception e){

}
}
class buttonClick implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(QueryActivity.this,OrderListActivity.class);
intent.putExtra("list", (Serializable)list);
QueryActivity.this.startActivity(intent);
Log.d("list",(Serializable)list+"");

}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_query, menu);
return true;
}
}


} } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_query, menu); return true; }}



其中还有一个工具类JsonHelper,在前面的博客中已经分享过了,这里就不写了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: