2011-07-15 6 views
1

私はAndroid ProjectでListActivityを持っています。そのコード:ListActivityからonListItemClickのコントロールを得る方法

public class ListActivityApp extends ListActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list); 
    ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> map; 

    map = new HashMap<String, String>();   
    map.put("title", "my note"); 
    map.put("img", String.valueOf(R.drawable.paper)); 
    listItem.add(map); 

    map = new HashMap<String, String>();   
    map.put("title", "second"); 
    map.put("img", String.valueOf(R.drawable.paper)); 
    listItem.add(map); 

    map = new HashMap<String, String>();   
    map.put("title", "cos tam"); 
    map.put("img", String.valueOf(R.drawable.paper)); 
    listItem.add(map); 

    SimpleAdapter myAdapter = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list, 
      new String[] {"img", "title"}, new int[] {R.id.img, R.id.title}); 
    setListAdapter(myAdapter);  
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    String selection = getListAdapter().getItem(position).toString(); 
    Toast.makeText(this, selection, 0).show(); 
}} 

マイたList.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<ListView 
android:id="@android:id/list" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"  
/> 
<TextView 
android:id="@android:id/empty" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="Empty" 
/> 

そして、私のitem.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<ImageView 
    android:id="@+id/img" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    /> 
<TextView android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    /> 
</LinearLayout> 

私が持っている最初の問題(解決):上エミュレータ私は3つのテキストビュー "空の"テキストを見ることができますが、私はそれらのそれぞれをクリックしたときに私は正しい "タイトル"とトーストを見ることができます。私が間違っていることは何ですか?別のプロジェクトでは、正しく動作します。 - 私はこれを解決しました。この中で:

SimpleAdapter myAdapter = new SimpleAdapter (this.getBaseContext(), listItem,  R.layout.list, new String[] {"img", "title"}, new int[] {R.id.img, R.id.title}); 

私は3番目の引数としてR.layout.item、ないR.layout.listを使用する必要があります。

第2の問題:私のリストアイテムのオブジェクトをonListItemClick(..)メソッドで取得するにはどうすればよいですか?私はもちろん、位置とIDを持っています。 l.getItemAtPosition(position) getListAdapter()。getItem(position) これらのメソッドは、すべてのアイテム(私の場合はimgとtitle)を返します。たとえば、TextViewのテキストを変更したり、onListItemClick(..)でImageViewを非表示に設定するにはどうすればよいですか?

+1

私はこの中のTextViewからテキストを取得することができます方法: 'HashMap map =新しいHashMap (); \t \t map =(HashMap )getListAdapter()。getItem(position); String temp =(String)map.get( "title"); 'しかし、私はできません:' TextView tv =(TextView)map.get( "title"); ' ListActivityからListViewの既存のコントロールを変更するにはどうすればよいですか? – woyaru

答えて

0

、クリックされた項目からのTextViewとImageViewのを取得するビュー、Vを使用し、onClickの()イベントのあなたに与えられ、次の操作を行います。

TextView tv = (TextView)v.findViewById(R.id.textview); 
tv.setText("example text"); 
ImageView image = (ImageView)v.findViewById(R.id.image); 
image.setVisibility(ImageView.INVISIBLE); 
関連する問題