2017-01-10 21 views
0
  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(); 

     } 
    }); 
} 

}的ListView與setItem onClickListner方法

代碼是沒有錯誤,但我沒能獲得敬酒消息,請單擊不應用內工作,我們應該修改或額外添加任何現有的代碼

下面是有關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; 
} 

}

+0

發表您的Android佈局 –

+0

如果你嘗試了所有的答案,人民給了你,不工作..嘗試清理你的項目,卸載你的設備中的應用程序,並再次編譯(併發布你的android佈局順便說一句) – Robert

回答

0

這似乎沒有錯。

我需要更多的代碼來確認。像WordAdapter等

另一件事,你可以在你的函數onItemClick()使用代碼

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

如果在onItemClick()中不起作用,那麼WordAdapter中的某些內容肯定是錯誤的。

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

嗨MVS,

使用你的代碼,它似乎可以顯示你的麪包。

守則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; 
} 

兩種可能性:

    代碼WordAdapter評論
  1. ,詳細覈查未使用的代碼(我曾評論)

2。有些電話,與三星一樣,您應該在系統設置中打開「Toast」。因此,請使用代碼Log.d("ss","position " + position + " id " + id);來查看onItemClick()是否在NumberActivity中執行。如果執行,這是您的手機的問題。

enter image description here

+0

我已經添加了wordAdapter類,因爲你提到,你想它調試更多,我無法點擊列表視圖 - 那裏沒有來自listview的迴應,。什麼是使用添加log.d日誌消息,我看不到任何相關的東西 – mvs

+0

這是你要求的代碼 – mvs

0

在適配器類將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; 
} 
} 
+0

它是完全錯誤的condiotion,你沒有說明這種方式沒有任何工作 – mvs

+0

你有什麼問題。 –

+0

我的意圖是當我點擊列表視圖時獲得敬酒信息 – mvs

0

我建議你創建父佈局作爲線性或相對佈局,其中包括你的englishTextView,miwokTextView和你的sideImage。

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

,並在您WordAdapter類發現的LinearLayout

LinearLayout linearLayout = (LinearLayout)listItemView.findViewById(R.id.linearLayout); 

linearLayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast display = Toast.makeText(context,"it is some text",Toast.LENGTH_LONG); 
    display.show(); 
      } 
     });