无法单击自定义ListView

时间:2016-04-26 14:39:02

标签: android listview onclicklistener

我整天都被困住了。找不到我的错误。

我创建了一个Custom ListView,我只想点击它来在HourListView中启动一个新的意图。

public class CustomList extends ArrayAdapter<String> {

private final ArrayList<String> hourList;

private final Activity context;

public CustomList(Activity context,
                ArrayList<String> hourList) {
    super(context, R.layout.hour_list_adapter, hourList);
    this.context = context;
    this.hourList = hourList;


}


@Override
public View getView(int position, View view, ViewGroup parent) {
    View rowView = view;

    ViewHolder viewHolder = null;


   // LayoutInflater inflater = context.getLayoutInflater();
   // View rowView= inflater.inflate(R.layout.hour_list_adapter, parent, false);



    if(rowView == null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.hour_list_adapter, parent, false);

        viewHolder = new ViewHolder();

        viewHolder.type_frequenceTV = (TextView) rowView.findViewById(R.id.type_frequence);
        viewHolder.dateTV = (TextView) rowView.findViewById(R.id.date);
        viewHolder.dureeTV = (TextView) rowView.findViewById(R.id.duree);
        viewHolder.commentaireTV = (TextView) rowView.findViewById(R.id.commentaire);

        viewHolder.simuIV = (ImageView) rowView.findViewById(R.id.simulateur);
        viewHolder.doubleIV = (ImageView) rowView.findViewById(R.id.doubleFrequence);



        rowView.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ViewHolder)rowView.getTag();
    }



    viewHolder.type_frequenceTV.setText(HourListActivity.hourObjects.get(position).get("type_frequence").toString());

    viewHolder.dureeTV.setText(HourListActivity.hourObjects.get(position).get("duree").toString());
    viewHolder.commentaireTV.setText(HourListActivity.hourObjects.get(position).get("comment").toString());


    Date dateAndTime =  (Date) HourListActivity.hourObjects.get(position).get("date");

    SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yy à HH:mm");
    String formatted = format1.format(dateAndTime.getTime());
    viewHolder.dateTV.setText("Le " + formatted);



    if ((Boolean) HourListActivity.hourObjects.get(position).get("double"))
    {
        viewHolder.doubleIV.setVisibility(View.VISIBLE);
    }
    else
    {
        viewHolder.doubleIV.setVisibility(View.GONE);
    }

    if ((Boolean) HourListActivity.hourObjects.get(position).get("simulateur"))
    {
        viewHolder.simuIV.setVisibility(View.VISIBLE);
    }
    else
    {
        viewHolder.simuIV.setVisibility(View.GONE);
    }


    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


    return rowView;



}

static class ViewHolder
{
    TextView type_frequenceTV;
    TextView dateTV ;
    TextView dureeTV ;
    TextView commentaireTV ;
    ImageView simuIV ;
    ImageView doubleIV ;

}

}

然后这是我希望能够点击的HourListActivity。

public class HourListActivity extends AppCompatActivity {

//ArrayAdapter<String> arrayAdapter;
CustomList adapter;
ListView hourListView;
static ArrayList<String> hourList;
static ArrayList<String> hourListID;
static ArrayList<ParseObject> hourObjects = new ArrayList<ParseObject>();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hour_list);

    hourList = new ArrayList<>();
    hourListID = new ArrayList<>();

    hourListView = (ListView) findViewById(R.id.listView);

    adapter = new CustomList(HourListActivity.this,hourList);

    //arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hourList);

    hourListView.setAdapter(adapter);

    hourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            System.out.println("CLIC !!!!!!!!!!!!!!!!!!!!!");
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Frequence");

            query.getInBackground(hourListID.get(position), new GetCallback<ParseObject>() {

                public void done(ParseObject object, ParseException e) {
                    if (e == null) {


                        Intent intent = new Intent(getApplicationContext(), ModifyHourActivity.class);
                        intent.putExtra("objectID", object.getObjectId());
                        startActivity(intent);


                    } else {
                        // something went wrong
                    }
                }
            });


        }
    });


    ParseQuery<ParseObject> query = ParseQuery.getQuery("Frequence");

    query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());

    System.out.println("username: " + ParseUser.getCurrentUser().getUsername() );

    query.orderByDescending("date");



    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e  == null)
            {


                if (objects.size() > 0)
                {

                    hourObjects.clear();

                    for (ParseObject frequence : objects)
                    {
                        String result = "";
                        result += frequence.get("date") + " - "
                                + frequence.get("type_frequence") + " - "
                                + frequence.get("duree") + " - "
                                + frequence.get("comment") + " - "
                                + frequence.get("double") + " - "
                                + frequence.get("simulateur") + " - ";


                        System.out.println("Résultat : " + result);

                        hourList.add(result.toString());
                        hourListID.add(frequence.getObjectId());

                        ParseObject newHour = new ParseObject("Temp");

                        newHour.put("username", frequence.get("username"));
                 //       newHour.put("id", frequence.get("objectId"));
                        newHour.put("type_frequence", frequence.get("type_frequence"));
                        newHour.put("date", frequence.get("date"));
                        newHour.put("duree", frequence.get("duree"));
                        newHour.put("comment", frequence.get("comment"));
                        newHour.put("double", frequence.get("double"));
                        newHour.put("simulateur", frequence.get("simulateur"));

                        hourObjects.add(newHour);



                    }

                    Toast.makeText(HourListActivity.this, "Liste des heures récupérée !",
                            Toast.LENGTH_LONG).show();

                    adapter.notifyDataSetChanged();

                }



            }
            else
            {


            }

        }
    });

最后我的自定义视图的xml文件:

  <?xml version="1.0" encoding="utf-8"?>
 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
   android:layout_height="match_parent>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:layout_margin="5dp"
    >

    <TextView
        android:id="@+id/type_frequence"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="DEP S"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/buttonTextColor"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"


        />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:orientation="horizontal">



            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:textSize="15dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="12 avril 2016 - 08:00"
                android:layout_marginLeft="5dp"


                />



            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical">
                <TextView
                    android:id="@+id/duree"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"

                    android:text="1.00"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/buttonTextColor"
                    android:layout_alignParentEnd="true"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"


                    />

            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/commentaire"
                android:layout_width="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_height="wrap_content"
                android:text="Commentaire"   
                />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true">
                <ImageView
                    android:id="@+id/doubleFrequence"
                    android:layout_width="35dp"
                    android:layout_height="35dp"

                    android:layout_column="1"



                    android:src="@drawable/double_96" />
                <ImageView
                    android:id="@+id/simulateur"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_column="1"

                    android:src="@drawable/simu_96"



                    android:layout_toStartOf="@+id/doubleFrequence" />
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

提前感谢您的帮助! :)

2 个答案:

答案 0 :(得分:1)

在适配器中删除此部件会发生什么?

rowView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    }
});

我想会发生的事情是之后创建了你在活动中定义了ItemClickListener,导致ClickListener在运行时被你的适配器中的一个重写。

答案 1 :(得分:0)

就我而言,如果您遇到setOnClickListener错误,可以试试这个 转到工具 - &gt;与Gradle同步。

当我被意图问题困扰时,它帮助了我!