使用onClick和ToggleButton在列表视图中设置Android文本

时间:2015-11-28 23:39:44

标签: android

在Android应用程序中使用列表视图,该视图使用ToggleButton记录按下的次数,并在每次单击时显示数字。我无法获取View对象,以便每次使用setText()将文本设置为新数字。我可以使用标签来获取位置,但是每次我使用它来获取我想要的View对象时它似乎都会给出一个空指针异常。也许我认为这完全错了,但我似乎无法弄明白。

package com.eventappucsd.activity;


import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView; 
import android.widget.Toast;
import android.widget.ToggleButton;
import com.eventappucsd.backend.Event;


import java.util.List;

public class EventsCustomAdapter extends ArrayAdapter<Event>  {

private LayoutInflater mLayoutInflater;
private static FragmentManager sFragmentManager;
private ContentResolver mContentResolver;
private Context mContext;
private final String LOG_TAG = EventsCustomAdapter.class.getSimpleName();

public EventsCustomAdapter(Context context, FragmentManager fragmentManager){

    super(context, android.R.layout.simple_list_item_2);
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    sFragmentManager = fragmentManager;
}
@Override
public View getView(final int position, final View convertView, final     ViewGroup parent) {
    View view;
    if(convertView == null) {
        //custom event layout
        view = mLayoutInflater.inflate(R.layout.custom_event, parent, false);

    } else {
        view = convertView;
    }
    final Event event = getItem(position);
    final int _id = event.getId();
    final String name = event.getEventName();
    final String date = event.getDate();
    final String time = event.getTime();
    final String location = event.getLocation();
    final String description = event.getDescription();
    final int numVotes = event.getNumVotes();

    ((TextView) view.findViewById(R.id.event_name)).setText(name);
    ((TextView) view.findViewById(R.id.event_date)).setText(date);
    ((TextView) view.findViewById(R.id.event_location)).setText(location);
    ((TextView) view.findViewById(R.id.event_numVotes)).setText(numVotes + " Votes");

    //get the context so that the object called is not null for db updates
    mContentResolver = getContext().getContentResolver();
    /*
    make the event clickable and transition into the ViewActivity
     */
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //setting up the data needed to be made available by the ViewEventActivity.class
            Intent eventView = new Intent(getContext(), ViewEventActivity.class);
            eventView.putExtra(EventsContract.EventsColumns.EVENTS_ID, String.valueOf(_id));
            eventView.putExtra(EventsContract.EventsColumns.EVENTS_NAME, name);
            eventView.putExtra(EventsContract.EventsColumns.EVENTS_DATE, date);
            eventView.putExtra(EventsContract.EventsColumns.EVENTS_TIME, time);
            eventView.putExtra(EventsContract.EventsColumns.EVENTS_LOCATION, location);
            eventView.putExtra(EventsContract.EventsColumns.EVENTS_DESCRIPTION,description);
            getContext().startActivity(eventView);
        }
    });

    final ToggleButton upvoteButton = (ToggleButton) view.findViewById(R.id.upbtn);
    // Needed in order to have both the button and the list item clickable
    upvoteButton.setFocusable(false);
    upvoteButton.setFocusableInTouchMode(false);
    upvoteButton.setClickable(true);
    upvoteButton.setTag(position);
    upvoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final int position = (Integer)view.getTag();
                Log.d(LOG_TAG, "position of view: " + position + "\n");
                //get the view that was clicked
                view = view.findViewWithTag(view.getTag());

                ContentValues values = new ContentValues();
                int recordsUpdated = 0;

                if(upvoteButton.isChecked()) {
                    Toast.makeText(getContext(), "Thank you for voting ", Toast.LENGTH_SHORT).show();
                    int newVotes = numVotes;
                    ++newVotes;

                    values.put(EventsContract.EventsColumns.EVENTS_NUM_VOTES, String.valueOf(newVotes));
                    Uri uri = EventsContract.Events.buildEventUri(String.valueOf(event.getId()));
                    recordsUpdated = mContentResolver.update(uri, values, null, null);

                    //TODO: display the new vote;
                    ((TextView) view.findViewById(R.id.event_numVotes)).setText(newVotes + " Votes");
                    Log.d(LOG_TAG, "number of records updated = " + recordsUpdated + " newVotes: " + newVotes);

                } else {
                    //TODO: decrease vote
                    ((TextView) view.findViewById(R.id.event_numVotes)).setText(numVotes + " Votes");
                    values.put(EventsContract.EventsColumns.EVENTS_NUM_VOTES, String.valueOf(numVotes));
                    Uri uri = EventsContract.Events.buildEventUri(String.valueOf(event.getId()));
                    recordsUpdated = mContentResolver.update(uri, values, null, null);
                    Log.d(LOG_TAG, "number of records updated = " + recordsUpdated + "newVotes: " + numVotes);
                }
            }
        });

    return view;
}
public void setData(List<Event> events){
    clear();
    if(events != null){
        for(Event event : events){
            add(event);
        }
    }
}

}

编译器抛出的错误表明空指针异常发生在:

11-28 15:29:51.881 9724-9724/com.eventappucsd.mikesapplication        E/AndroidRuntime: FATAL EXCEPTION: main
11-28 15:29:51.881 9724-9724/com.eventappucsd.mikesapplication E/AndroidRuntime: Process: com.eventappucsd.mikesapplication, PID: 9724
11-28 15:29:51.881 9724-9724/com.eventappucsd.mikesapplication E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

1 个答案:

答案 0 :(得分:0)

使用此代码修复它。问题似乎是我的旧参数

$i = 10
while ($i -le 1000){
$i | Out-File -Append test.txt
$i += 5
}

这在语法方面令人困惑,因为它意味着表示与OnClick方法之外的View视图不同的值。 getView方法中的视图是我在TextView中设置文本所需的。

public void onClick(View view)