2016-08-18 3 views
0

**Check the image....This is where I want the Play icon**私のアプリにはメディアプレーヤーが含まれているアイテムはほとんどありませんので、リストに「再生」アイコンを配置したいです右側にリストビューアイテムには、イメージビューとテキストビュー(アイテムアイコンとアイテム名を持つ)が含まれます。 私は特定の項目リストビューのアイテムの一部に「再生」アイコンを入れたい

に画像を挿入することができますどのような方法があり、ここに私のコードです:

first.java:

public class first extends AppCompatActivity { 
private ListView listView; 
private String[] names = {"१. प्रार्थना", 
     "२. सुखकर्ता दु:खहर्ता", 
     "३. गजानना श्रीगणराया", 
     "४. येई हो विठ्ठले", 
     "५. लवथवती विक्राळा", 
     "६. दुर्गे दुर्घट भारी", 
     "७. आरती ज्ञानराजा", 
     "८. हे भोळ्या शंकरा", 
     "९. ओवाळू आरती", 
     "१०. मंगेशा महारूद्रा", 
     "११. आला गणराया", 
     "१२. शेंदुर लाल चढायो", 
     "१३. एकदंता", 
     "१४. कर्पूर गौरा", 
     "१५. हे गजवधना", 
     "१६. प्रथमेश्वरा", 
     "१७. जयदेवा हे साईनाथा", 
     "१८. श्री सत्यनारायणाची आरती", 
     "१९. श्री महालक्ष्मीची आरती", 
     "२०. ॐ जय लक्ष्मी माता", 
     "२१. आरती संतोषी माता की", 
     "२२. श्री हनुमंताची आरती", 
     "२३. सुखंकर्ता की दु:खहर्ता", 
     "२४. ॐ जय जगदीश", 
     "२५. जय श्री विश्वकर्मा", 
     "२६. हे गौरी गजानन", 
     "२७. हे गजानना गौरीनंदना", 
     "२८. आरती नवदुर्गेची", 
     "२९. हे साईनाथा जगदिश्वरा", 
     "३०. धन्य धन्य हो प्रदक्षिणा", 
     "३१. घालीन लोटांगण"}; 
private int imageid[] = {R.drawable.prath, 
     R.drawable.ganeshaji, 
     R.drawable.ganpati, 
     R.drawable.vita, 
     R.drawable.shivji, 
     R.drawable.durgamaa, 
     R.drawable.nyan, 
     R.drawable.bol, 
     R.drawable.krishna, 
     R.drawable.mangeshi, 
     R.drawable.ala, 
     R.drawable.shen, 
     R.drawable.ekdant, 
     R.drawable.karpur, 
     R.drawable.gajavad, 
     R.drawable.pratha, 
     R.drawable.saibaba, 
     R.drawable.satyanarayan, 
     R.drawable.laxmimaa, 
     R.drawable.omjailak, 
     R.drawable.santoshimaa, 
     R.drawable.hanuman, 
     R.drawable.ganeshaji, 
     R.drawable.skd, 
     R.drawable.vishwakarma, 
     R.drawable.ganpati, 
     R.drawable.ganeshaji, 
     R.drawable.navdurga, 
     R.drawable.saibaba, 
     R.drawable.danya, 
     R.drawable.gana}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_first); 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayShowHomeEnabled(true); 
    actionBar.setIcon(R.mipmap.icon); 
    final CustomList CustomList = new CustomList(this, names, imageid); 
    listView = (ListView) findViewById(R.id.listView); 
    listView.setAdapter(CustomList); 
} 
} 

arti.java:

public class arti { 
private String names; 
private int imageid; 
public arti(String names,int imageid){ 
    this.names=names; 
    this.imageid=imageid; 
} 
public String getNames(){ 
    return this.names; 
} 
public int getImageid(){ 
    return this.imageid; 
} 
} 

customlist。 java:

public class CustomList extends ArrayAdapter<String> { 
private String[] names; 
private int[] imageid; 
private Activity context; 

public CustomList(Activity context, String[] names, int[] imageid) { 
    super(context, R.layout.list_layout,names); 
    this.context = context; 
    this.names = names; 
    this.imageid = imageid; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View listViewItem = inflater.inflate(R.layout.list_layout, null, true); 
    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName); 
    ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView); 
    textViewName.setText(names[position]); 
    image.setImageResource(imageid[position]); 
    return listViewItem; 
    } 

first.xml:

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listView" 
    android:dividerHeight="2dp" 
    android:layout_marginTop="3dp" 
    android:layout_marginBottom="1dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

listlayout.xml:

<ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+id/imageView" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="80dp" 
    android:gravity="center" 
    android:textSize="22dp" 
    android:text="New Text" 
    android:id="@+id/textViewName" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/imageView" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

checkout this image

私がプレイ画像は項目の名前

答えて

0

すぐチェックの後に配置することにしたいですgetView()の方法で画像が-1の場合:

if (imageid[position] != -1) { 
    image.setImageResource(imageid[position]); 
} else { 
    image.setVisibility(View.GONE); //or View.INVISIBLE 
} 

しかし、このようにあなたははい、listlayoutは相対作ってみましょうあなたがイメージ

0

を望んでいないすべての項目のIDとして-1を設定し、以下のようにオプションを使用する必要があります。

テキストビューの画像を右から見るための「右へ」
「可視性」は、音楽アイコンを表示または非表示にします。デフォルトでは表示されないので、txtView.setVisibility(View.VISIBLE)としてコード内の可視性を有効にする必要があります。

<RelativeLayout 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/headerLinearLay" 
    android:orientation="horizontal"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="80dp" 
     android:gravity="center" 
     android:textSize="22dp" 
     android:text="New Text" 
     android:id="@+id/textViewName" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/imageView" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 
    <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/imageView" 
     android:layout_toRightOf="@id/textViewName" 
     android:visibility="invisible" /> <!-- Enable when you want to show the play button --> 
</RelativeLayout> 
+0

? – Devk

+0

@Devkエラー、または問題や問題は何であるのdoesntの – sumandas

+0

アイテム名の直後に「再生」アイコンを置いておきます.... – Devk

0
あなたlistlayout.xmlで

<Button 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:visibility="gone" 
android:id="@+id/playButtonView" /> 

<ImageView 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:id="@+id/imageView" /> 

<TextView 
android:layout_width="wrap_content" 
android:layout_height="80dp" 
android:gravity="center" 
android:textSize="22dp" 
android:text="New Text" 
android:id="@+id/textViewName" 
android:layout_alignParentTop="true" 
android:layout_toRightOf="@+id/imageView" 
android:layout_alignParentRight="true" 
android:layout_alignParentEnd="true" /> 

再生ボタンの画像を保持するためにビューを追加します。これを行う別の方法があるかもしれませんが、これはあなたに良い意味合いをもたらしますが、これはあなたに良い出発点を与えるはずです。以下のコメントをアドレッシング

:あなたのcustomlist.java

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View listViewItem = inflater.inflate(R.layout.list_layout, null, true); 
    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName); 
    ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView); 
    Button playButton = (ImageView) listViewItem.findViewById(R.id.playButtonView); 
    if(playButtonShouldBeVisible) //change to fit your logic 
     playButton.setVisibility(View.VISIBLE); 
    textViewName.setText(names[position]); 
    image.setImageResource(imageid[position]); 
    return listViewItem; 
} 

編集1では

。あなたはあなたがプレイ画像(OPTION 1)のためのボタンを必要としないことを意味した場合、そのことにより、あなたはあなたのドンことを意味していることを意味するコメントする場合は、単に

<Button 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:visibility="gone" 
android:id="@+id/playImageView" /> 

<Button 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:visibility="gone" 
android:id="@+id/playButtonView" /> 

を変更「Tは、追加のビューを必要とし、あなたがすでに含またい画像の表示は、あなたのプレイ画像のために使用したいものであった。(OPTION 2)私はちょうど

<Button 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:visibility="gone" 
android:id="@+id/playButtonView" /> 

<ImageView 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:id="@+id/imageView" /> 

を変更します

<ImageView 
android:layout_width="80dp" 
android:layout_height="80dp" 
android:visibility="gone" 
android:id="@+id/imageView" /> 

いずれか

OPTION 1のように、これらの変更はcustomlist.javaに反映される:

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView); 
Button playButton = (ImageView) listViewItem.findViewById(R.id.playButtonView); 
if(playButtonShouldBeVisible) //change to fit your logic 
    playButton.setVisibility(View.VISIBLE); 

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView); 
Button playButton = (ImageView) listViewItem.findViewById(R.id.playButtonView); 
if(playButtonShouldBeVisible) //change to fit your logic 
    playButton.setVisibility(View.VISIBLE); 

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView); 
ImageView playImage = (ImageView) listViewItem.findViewById(R.id.playImageView); 
if(playImageShouldBeVisible) //change to fit your logic 
    playImage.setVisibility(View.VISIBLE); 

OPTION 2に変更されます

これらの変更は、Javaコードでそのビューを見つけて、それへのハンドルを提供し、その後、表示したいかもしれませんプレイ映像ですlistlayout.xmlでビューを定義する指示に役立つはず

ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView); 
if(imageViewShouldBeVisible) //change to fit your logic 
    image.setVisibility(View.VISIBLE); 

に変更を取得そのビューを表示する必要があるかどうかを判断し、その変更に基づいて可視性プロパティを設定します。あなたのXMLにあなたのアイコンとボタン非表示ボタンを追加

1.

+0

ほんの少しのリストビュー項目の再生画像を入れたいです.... – Devk

+0

再生画像は、この項目に音楽ファイルが含まれていることを示すためのものです.... .... thats it .... – Devk

+0

この部分を教えてくださいもし(playImageShouldBeVisible) – Devk

0

はただ、これは、手順行うアダプタクラスで

`<Button 
    android:id="@+id/playButton" 
    android:background="your_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentEnd="true" 
    android:visibility="invisible" />` 

2.がセットポジションを作成する=新しいHashSetの<> ()を使用して、このセットに必要な位置を追加します。 getViewメソッド()メソッドチェックで

:代わりArrayAdapterの

if(positions.contains(position)){ Button play = (Button) listViewItem.findViewById(R.id.playButton); play.setVisibility(View.VISIBLE);}

使用BaseAdapter、ちょうどBaseAdapterを拡張し、それは同様にgetCountにおける方法、(だ実装)リスト項目の合計数を返し、他のメソッドは空のままです。getViewメソッドで は() 最終コードの上方に書き込ま3は次のようになりポイント行う:

`パブリッククラスアダプタはArrayAdapter { プライベート文字列[]名を拡張します。 private int [] imageid; プライベートアクティビティmContext; プライベートセットセット。

public Adapter(Context context, String[] names, int[] imageid) { 
    super(context, R.layout.item); 
    mContext = (Activity) context; 
    this.names = names; 
    this.imageid = imageid; 
    fillSet(); 
} 

private void fillSet() { 
    set = new HashSet<>(); 
    set.add(1); 
    set.add(2); 
    set.add(6); 
} 

@Override 
public int getCount() { 
    return names.length; 
} 

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    LayoutInflater inflater = mContext.getLayoutInflater(); 
    view = inflater.inflate(R.layout.item, null); 
    TextView tv = (TextView) view.findViewById(R.id.tv); 
    ImageView iv = (ImageView) view.findViewById(R.id.iv); 
    tv.setText(names[i]); 
    iv.setImageResource(imageid[i]); 
    if (set.contains(i)) { 
     Button button = (Button) view.findViewById(R.id.playButton); 
     button.setVisibility(View.VISIBLE); 
    } 
    return view; 
} 

} `

それはあなたが欲しいものを作るべき

作業@sumandas
+0

私はlistlayout.xmlにボタンを追加しました.....そしてCustomlist.javaにこれを追加しました:Set positions = new HashSet <>(1,2,6);あなたがgetview()で言ったことをしました...まだ運がない.....これは私が何もしていないすべてです....助けてください!!!! – Devk

+0

@Devk回答をテストして編集しました。 –

+0

は強制的なクラッシュを引き起こします。私はlistviewのためにimageViewとTextViewを持っています...そしてあなたはそれのためにインフレータを使用していません...してくださいこれで助けてください...私のコードを使ってみてください... – Devk

関連する問題