ListView与setItem onClickListner方法

时间:2017-01-10 01:44:29

标签: android

         public class NumbersActivity extends AppCompatActivity {
            private MediaPlayer mMediaPlayer;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.word_list);

          ArrayList<Words> numbers = new ArrayList<Words>();
          numbers.add(new Words("one","lutti",R.drawable.number_one));
          numbers.add(new Words("two","otiiko",R.drawable.number_two));
          numbers.add(new Words("three","tolookosu",R.drawable.number_three));
          numbers.add(new Words("four","oyyisa",R.drawable.number_four));
          numbers.add(new Words("five","massokka",R.drawable.number_five));
          numbers.add(new Words("six","temmokka",R.drawable.number_six));
          numbers.add(new Words("seven","kenekaku",R.drawable.number_seven));
          numbers.add(new Words("eight","kawinta",R.drawable.number_eight));
          numbers.add(new Words("nine","wo’e",R.drawable.number_nine));
          numbers.add(new Words("ten","na’aacha",R.drawable.number_ten));
       WordAdapter adapter = new WordAdapter(this, numbers, R.color.category_numbers);
      ListView listView = (ListView) findViewById(R.id.list);
      listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Toast display = Toast.makeText(NumbersActivity.this,"it is some text",Toast.LENGTH_LONG);
            display.show();

        }
    });
}

}

代码没有错误,但我无法获取toast消息,点击在应用内无法正常工作,我们是否应修改或添加现有代码以外的任何内容

以下是与WordAdapter类相关的代码

public class WordAdapter extends ArrayAdapter<Words> {

/** Resource ID for the background color for this list of words */
private int mColorResourceId;



/**
 * Create a new {@link WordAdapter} object.
 *
 * @param context is the current context (i.e. Activity) that the adapter is being created in.
 * @param words is the list of {@link Words}s to be displayed.
 * @param colorResourceId is the resource ID for the background color for this list of words
 */
public WordAdapter(Context context, ArrayList<Words> words, int colorResourceId) {
    super(context, 0, words);
    mColorResourceId = colorResourceId;
}





@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if(listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }


    Words wordsTranslation = getItem(position);

    // Find the TextView in the list_item.xml layout with the ID version_name
    TextView englishTextView = (TextView) listItemView.findViewById(R.id.englishWord);

    englishTextView.setText(wordsTranslation.getDefaultEnglish());

    TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwokWord);

    miwokTextView.setText(wordsTranslation.getTranslatedMiwok());


    // Find the ImageView in the list_item.xml layout with the ID image.
    ImageView sideImage = (ImageView) listItemView.findViewById(R.id.imageView);

    // Check if an image is provided for this word or not
    if (wordsTranslation.hasImage()) {
        // If an image is available, display the provided image based on the resource ID
         sideImage.setImageResource(wordsTranslation.getImage());
        // Make sure the view is visible
        sideImage.setVisibility(View.VISIBLE);
    } else {
           sideImage.setVisibility(View.GONE);
    }

    View textContainer = listItemView.findViewById(R.id.text_container);
    int color = ContextCompat.getColor(getContext(), mColorResourceId);
    textContainer.setBackgroundColor(color);

    return listItemView;
}

}

3 个答案:

答案 0 :(得分:1)

我建议您将父布局创建为线性或相对布局,其中包括您的englishTextView,miwokTextView和您的sideImage。

type Window = BiTree Split View

data SplitRule =
  Percentage Double
  | FromStart Int
  | FromEnd Int
  deriving (Show)

data Dir = Hor
        | Vert
        deriving (Show)

data Split = Split
  { _dir :: Dir
  , _splitRule :: SplitRule
  } deriving (Show)

makeLenses ''Split

data View = View
  { _active :: Bool
  , _bufIndex :: Int
  } deriving (Show)

makeLenses ''View

并在您的WordAdapter类中找到LinearLayout

<LinearLayout android:id="@+id/linearLayout>
  <TextView android:id="@+id/englishTextView />
    <TextView android:id="@+id/miwokTextView />
     <ImageView android:id="@+id/sideImage/>
</LinearLayout>

答案 1 :(得分:0)

It seems nothing wrong.

I need more code to confirm. like WordAdapter etc.

Another thing, you can use code

Log.d("ss","position " + position + " id " + id);

in your function onItemClick().

If not worked in onItemClick(), something must be wrong in your WordAdapter.

-----------------------UPDATE------------------------------------

Hi mvs,

using your code,it seems could show your Toast.

Code in WordAdapter:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }


    Words wordsTranslation = getItem(position);
    TextView englishTextView = (TextView) listItemView.findViewById(R.id.textItem);

    englishTextView.setText(position + "");
    /**
     TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwokWord);

     miwokTextView.setText(wordsTranslation.getTranslatedMiwok());


     // Find the ImageView in the list_item.xml layout with the ID image.
     ImageView sideImage = (ImageView) listItemView.findViewById(R.id.imageView);

     // Check if an image is provided for this word or not
     if (wordsTranslation.hasImage()) {
     // If an image is available, display the provided image based on the resource ID
     sideImage.setImageResource(wordsTranslation.getImage());
     // Make sure the view is visible
     sideImage.setVisibility(View.VISIBLE);
     } else {
     sideImage.setVisibility(View.GONE);
     }

     View textContainer = listItemView.findViewById(R.id.text_container);
     int color = ContextCompat.getColor(getContext(), mColorResourceId);
     textContainer.setBackgroundColor(color);
     **/

    return listItemView;
}

Two possibilities:

  1. in code WordAdapter comment, detail check unused code(I have commented)

2.Some Phone, Like Samsung, you should turn on 'Toast' in System Settings. So, Please use code Log.d("ss","position " + position + " id " + id); to see whether onItemClick() is executed in NumberActivity. if executed, it is your phone's problem.

enter image description here

答案 2 :(得分:0)

在Adapter类中放置onClickListener。

public class WordAdapter extends ArrayAdapter<Words> {

private int mColorResourceId;
Context context;


public WordAdapter(Context context, ArrayList<Words> words, int colorResourceId) {
super(context, 0, words);
this.context=context;
mColorResourceId = colorResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
    listItemView = LayoutInflater.from(getContext()).inflate(
            R.layout.list_item, parent, false);
}


Words wordsTranslation = getItem(position);

// Find the TextView in the list_item.xml layout with the ID version_name
TextView englishTextView = (TextView)    listItemView.findViewById(R.id.englishWord);

englishTextView.setText(wordsTranslation.getDefaultEnglish());

TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwokWord);

miwokTextView.setText(wordsTranslation.getTranslatedMiwok());


// Find the ImageView in the list_item.xml layout with the ID image.
ImageView sideImage = (ImageView) listItemView.findViewById(R.id.imageView);

// Check if an image is provided for this word or not
if (wordsTranslation.hasImage()) {
    // If an image is available, display the provided image based on the resource ID
     sideImage.setImageResource(wordsTranslation.getImage());
    // Make sure the view is visible
    sideImage.setVisibility(View.VISIBLE);
} else {
       sideImage.setVisibility(View.GONE);
}

View textContainer = listItemView.findViewById(R.id.text_container);
int color = ContextCompat.getColor(getContext(), mColorResourceId);
textContainer.setBackgroundColor(color);
 listItemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
  Toast display = Toast.makeText(context,"it is some text",Toast.LENGTH_LONG);
        display.show();
        }
    });

return listItemView;
}
}