gpt4 book ai didi

Android ListView 和事件冒泡

转载 作者:行者123 更新时间:2023-11-30 04:35:35 24 4
gpt4 key购买 nike

我有一个带有 ListView 的 Android 应用程序。我创建了自己的项目布局,其中有一个 CheckBox,后跟两个 TextView 和一个 Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip"></CheckBox>
<RelativeLayout android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_width="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentRight="true" android:text="Button"></Button>
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_toLeftOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_width="match_parent"></TextView>
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="TextView" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignRight="@+id/textView1"></TextView>
</RelativeLayout>
</LinearLayout>

当用户点击行时,我想要不同的行为,具体取决于他们点击的是复选框、按钮还是行本身(即除复选框或按钮之外的任何内容)。到目前为止,这是我尝试实现的(使用 setOnItemClickListener):

package com.camelconsultants.shoplist;

import java.util.ArrayList;
import java.util.HashMap;

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

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class HelloAndroid extends ListActivity implements OnItemClickListener
{
ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();

/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );

GetData();

setListAdapter
(
new SimpleAdapter
(
this.getBaseContext(),
Items,
R.layout.multiitem,
new String[]{ "Code", "Description" },
new int[]{ R.id.textView1, R.id.textView2 }
)
);

ListView lv = getListView();
lv.setTextFilterEnabled( true );

lv.setOnItemClickListener( this );
}

public void onItemClick( AdapterView<?> parent, View view, int position, long id )
{
Toast.makeText
(
getApplicationContext(),
parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT
).show();
}

void GetData()
{
HashMap<String, String> Item;

try
{
JSONObject JsonObject = new JSONObject( this.getResources().getString(R.string.Json) );
JSONArray JsonArray = JsonObject.getJSONArray( "Items" );

for ( int i = 0; i < JsonArray.length(); i++ )
{
Item = new HashMap<String, String>();
Item.put( "Code", JsonArray.getJSONObject(i).getString("Code") );
Item.put( "Description", JsonArray.getJSONObject(i).getString("Description") );

Items.add( Item );
}
}
catch( JSONException Bummer )
{
Bummer.printStackTrace();
}
}
}

当我运行应用程序并单击其中一个 TextView 时,没有任何反应!

解决这个问题的最佳方法是什么?

最佳答案

我发现我可以在 xml 定义中为 View 设置回调。这是更新后的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linearLayout1" android:clickable="true" android:onClick="onClick" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip" android:onClick="onCheckBoxClick"></CheckBox>
<RelativeLayout android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_width="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentRight="true" android:text="Button" android:onClick="onButtonClick"></Button>
<TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_toLeftOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_width="match_parent"></TextView>
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="TextView" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignRight="@+id/textView1"></TextView>
</RelativeLayout>
</LinearLayout>

我已经用适当的回调钩子(Hook)更新了代码:

package com.camelconsultants.shoplist;

import java.util.ArrayList;
import java.util.HashMap;

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

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class HelloAndroid extends ListActivity
{
ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();

/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );

GetData();

setListAdapter
(
new SimpleAdapter
(
this.getBaseContext(),
Items,
R.layout.multiitem,
new String[]{ "Code", "Description" },
new int[]{ R.id.textView1, R.id.textView2 }
)
);

ListView lv = getListView();
lv.setTextFilterEnabled( true );
}

@SuppressWarnings("unchecked")
private HashMap<String, String> getItem( View view )
{
HashMap<String, String> Item = null;

ListView listView = getListView();

int Position = listView.getPositionForView( view );

if ( listView.getItemAtPosition(Position) instanceof HashMap )
Item = (HashMap<String, String>)listView.getItemAtPosition( Position );

return Item;
}

public void onClick( View view )
{
HashMap<String, String> Item = getItem( view );
if ( Item == null )
return;

Toast.makeText
(
getApplicationContext(),
Item.get("Code") + ", " + Item.get( "Description" ),
Toast.LENGTH_SHORT
).show();
}

public void onCheckBoxClick( View view )
{
HashMap<String, String> Item = getItem( view );
if ( Item == null )
return;

Toast.makeText
(
getApplicationContext(),
"CheckBox! - " + Item.get("Code") + ", " + Item.get( "Description" ),
Toast.LENGTH_SHORT
).show();
}

public void onButtonClick( View view )
{
HashMap<String, String> Item = getItem( view );
if ( Item == null )
return;

Toast.makeText
(
getApplicationContext(),
"Button! - " + Item.get("Code") + ", " + Item.get( "Description" ),
Toast.LENGTH_SHORT
).show();
}

void GetData()
{
HashMap<String, String> Item;

try
{
JSONObject JsonObject = new JSONObject( this.getResources().getString(R.string.Json) );
JSONArray JsonArray = JsonObject.getJSONArray( "Items" );

for ( int i = 0; i < JsonArray.length(); i++ )
{
Item = new HashMap<String, String>();
Item.put( "Code", JsonArray.getJSONObject(i).getString("Code") );
Item.put( "Description", JsonArray.getJSONObject(i).getString("Description") );

Items.add( Item );
}
}
catch( JSONException Bummer )
{
Bummer.printStackTrace();
}
}
}

getItem 函数通过将 View 向上传递到 getItemAtPosition 来返回当前项目的 HashMap。很酷的一点是, View 是否嵌套并不重要。

但是,我假设创建我自己的适配器并使用 setTag 方法更有效。但就目前而言,上面的代码适合我的目的!

关于Android ListView 和事件冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6906807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com