0

当您单击列表视图中的项目时,我想要在项目上显示此图像,对于列表视图中的所有项目,它都是相同的图像,我该怎么做?那是行:

<?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="wrap_content"

    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="75dp" >

        <TextView
            android:id="@+id/tv_fname"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1"
            android:text="ans" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/tv_lname"
            android:layout_marginLeft="44dp"
            android:layout_toRightOf="@+id/tv_fname" >

        </RelativeLayout>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/blackbar" />

        <TextView
            android:id="@+id/tv_lname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/tv_fname"
            android:text="ques" />
            --that's the picture i want to make visible when clicked
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/tv_fname"
            android:layout_marginRight="74dp"
            android:src="@drawable/checker"
            android:visibility="invisible" />

    </RelativeLayout>

</LinearLayout>

这是代码:

package com.example.lamder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class GetActivity extends Activity {
    RegistrationAdapter adapter_ob;
    RegistrationOpenHelper helper_ob;
    SQLiteDatabase db_ob;
    ListView nameList;
    Button registerBtn;
    Cursor cursor;
    static boolean[] check=new boolean[100];
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getact);
        nameList = (ListView) findViewById(R.id.lv_name);
        registerBtn = (Button) findViewById(R.id.btn_register);
        adapter_ob = new RegistrationAdapter(this);
        cursor=adapter_ob.queryName();
        Context context = getApplicationContext();
        CharSequence text = "select questions by clicking on them";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        for(int i=0;i<check.length;i++)
            check[i]=false;
        String[] from = { helper_ob.FNAME, helper_ob.LNAME };   

        int[] to = { R.id.tv_fname, R.id.tv_lname };
        cursor = adapter_ob.queryName();
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
                R.layout.row, cursor, from, to);
        nameList.setAdapter(cursorAdapter);
        nameList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {       
            ImageView img=(ImageView) findViewById(R.id.imageView2);
             if(check[position]==false)
            {
                    check[position]=true;
                    img.setVisibility(View.VISIBLE);
            }

                else  
                {
                           check[position]=false;
                           img.setVisibility(View.INVISIBLE);   
                }




            }
        });
4

1 回答 1

0
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {       
    ImageView img=(ImageView) view.findViewById(R.id.imageView2);
    if(check[position]==false){
        check[position]=true;
        img.setVisibility(View.VISIBLE);
    }else{
         check[position]=false;
         img.setVisibility(View.INVISIBLE);   
    }
}

如果您试图获取View用户单击列表项的一部分,请尝试使用上述代码获取视图并更改可见性。

于 2013-08-05T12:18:35.517 回答