如何在点击android

时间:2015-05-11 19:12:54

标签: javascript android listview

我试图更改列表项单击中的行的图标但没有成功。我按照其他s / o问题得到的建议但结果没有成功。我尝试了类似

的方法



@Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               ImageView imageView = ((ImageView) view);

               imageView.setImageResource(R.drawable.open);

               imageView.invalidate();
&#13;
&#13;
&#13;

但到目前为止没有任何工作。所以经过这么多天的研究后我真的不得不在这里发布。所以我们将非常感谢任何帮助,建议或推荐。

我的适配器:

&#13;
&#13;
public class ContactListAdapter extends BaseAdapter {

    Context context;
    ArrayList<ContactListItems> contactList;

    public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {

        this.context = context;
        contactList = list;
    }

    @Override
    public int getCount() {

        return contactList.size();
    }

    @Override
    public Object getItem(int position) {

        return contactList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        ContactListItems contactListItems = contactList.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.contact_list_row, null);

        }
        TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno);
        tvSlNo.setText(contactListItems.getId());
        TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
        tvName.setText(contactListItems.getName());
        TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
        tvPhone.setText(contactListItems.getSalary());

        return convertView;
}}
&#13;
&#13;
&#13;

我填写listview的地方:

&#13;
&#13;
public class Inbox extends Fragment {
    String ParsingDta= "{\"Employee\":[{\"id\":\"101\",\"name\":\"Class:\",\"salary\":\"SPC112 has been postponed to Wed 2.30pm @SR6  \"},{\"id\":\"102\",\"name\":\"Exams:\",\"salary\":\"There is a change of venue of SPY312 exam from NCT7 to NCT5\"},{\"id\":\"103\",\"name\":\"Exams:\",\"salary\":\"There is a slight change of venue of SPY312 exam from NCT7 to NCT5\"},{\"id\":\"104\",\"name\":\"Class:\",\"salary\":\"There is a slight change of venue of SPY312 exam from NCT5 to NCT6\"}]}";
    String str="";
    ListView listView;
    Database database;
    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        database=new Database(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.inbox,container,false);
        database.getWritableDatabase();
        listView=(ListView)rootView.findViewById(R.id.listView);

        try
        {
            JSONObject jsonObject=new JSONObject(ParsingDta);

            JSONArray jsonArray= jsonObject.getJSONArray("Employee");

            for(int i=0;i<jsonArray.length();i++)

            {
                JSONObject jsonObject1=jsonArray.getJSONObject(i);

                String id=jsonObject1.getString("id").toString();

                String name=jsonObject1.getString("name").toString();

                String salary=jsonObject1.getString("salary").toString();

                database.insertData(id,name,salary);

                str+="\n Employee"+i+ "\n name:"+name+"\n id:"+id+"\n salary:" +salary+"\n";

                //textView1.setText(str);
            }
        }
        catch(JSONException e)
        {
            e.printStackTrace();
        }
        ArrayList<ContactListItems> contactList =database.fetchData();
        ContactListAdapter contactListAdapter = new ContactListAdapter(
        getActivity().getApplicationContext(), contactList);
        listView.setAdapter(contactListAdapter);
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               ImageView imageView = ((ImageView) view);

               imageView.setImageResource(R.drawable.open);

               imageView.invalidate();
               Intent intent=new Intent(getActivity().getApplicationContext(),InboxDetails.class);
               startActivity(intent);
           }
       });
        setListViewHeightBasedOnChildren(listView);
        return rootView;
    }

    private void setListViewHeightBasedOnChildren(ListView listView) {
        ContactListAdapter contactListAdapter  = (ContactListAdapter) listView.getAdapter();
        if (contactListAdapter == null)
            return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < contactListAdapter.getCount(); i++) {
            view = contactListAdapter.getView(i, view,listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (contactListAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}
&#13;
&#13;
&#13;

ContactListItems:

&#13;
&#13;
public class ContactListItems {
    String id;
    String name;
    String salary;
    public ImageView img;
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName( String name) {
        this.name = name;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

}
&#13;
&#13;
&#13;

contact_list_row.xml:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:background="@drawable/list_background_selector"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_slno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:textColor="#000000"
        android:textSize="10dp"
        android:typeface="monospace"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="42dp"
        android:layout_marginEnd="42dp" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="title"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/list"
        android:id="@+id/imageView"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="description"
        android:singleLine="true"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignLeft="@+id/tv_name"
        android:layout_alignStart="@+id/tv_name" />
</RelativeLayout>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

@Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              /*******************************************************************/
               ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
              /*******************************************************************/


               imageView.setImageResource(R.drawable.open);

               imageView.invalidate();