Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android使用onClick和ToggleButton在列表视图中设置文本_Android - Fatal编程技术网

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

Android使用onClick和ToggleButton在列表视图中设置文本,android,Android,在Android应用程序中使用列表视图,该应用程序使用切换按钮记录其被按下的次数,并在每次单击时显示该次数。我无法获取视图对象,以便每次使用setText()将文本设置为新的数字。我可以使用标记获取位置,但每次我使用该标记获取所需的视图对象时,似乎都会出现空指针异常。也许我完全错了,但我似乎无法理解 package com.eventappucsd.activity; import android.content.ContentResolver; import android.content

在Android应用程序中使用列表视图,该应用程序使用切换按钮记录其被按下的次数,并在每次单击时显示该次数。我无法获取视图对象,以便每次使用setText()将文本设置为新的数字。我可以使用标记获取位置,但每次我使用该标记获取所需的视图对象时,似乎都会出现空指针异常。也许我完全错了,但我似乎无法理解

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);
        }
    }
}

用这个代码修复了它。问题似乎是我的旧参数

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

final ToggleButton upvoteButton = (ToggleButton) view.findViewById(R.id.upbtn);
    upvoteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.upbtn:
                    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;
                        //display the new vote count
                        ((TextView) view.findViewById(R.id.event_numVotes)).setText(newVotes + " Votes");
                        //update
                        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);
                        Log.d(LOG_TAG, "number of records updated = " + recordsUpdated + " newVotes: " + newVotes);
                    }else {
                        //revert view for vote count
                        ((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);
                    }
                    break;
                default:
            }
        }
    }); 

您的一个或多个TextView为空,请记录一些以了解其情况…当应用程序崩溃时,所有文本字段都已填充。我不明白为什么在那个点上(当我按下toggleButton时)任何TextView都是空的。这使我相信我没有得到正确的视图对象来设置文本。
final ToggleButton upvoteButton = (ToggleButton) view.findViewById(R.id.upbtn);
    upvoteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.upbtn:
                    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;
                        //display the new vote count
                        ((TextView) view.findViewById(R.id.event_numVotes)).setText(newVotes + " Votes");
                        //update
                        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);
                        Log.d(LOG_TAG, "number of records updated = " + recordsUpdated + " newVotes: " + newVotes);
                    }else {
                        //revert view for vote count
                        ((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);
                    }
                    break;
                default:
            }
        }
    });