9

Tôi gặp vấn đề khi hiển thị Thông báo Bánh mì nướng khi tôi nhấp vào một nút trong Chế độ xem danh sách. Vấn đề là ia lớp tùy chỉnh mở rộng bởi BaseAdapter tôi có một phương thức có tên GetView Tôi muốn hiển thị một tin nhắn chúc mừng từ đó thông điệp cụ thểLàm thế nào để hiển thị Bánh mì nướng trong một lớp được mở rộng bởi BaseAdapter Get View Method

Sau đây là listview Row Xml tôi

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/tools" 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    app:context=".HomeActivity" > 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TableRow 
      android:id="@+id/tableRow_laugh" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       app:ignore="UselessParent" > 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" > 

        <TableRow 
         android:id="@+id/tableRow_Header_laugh" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:background="#818085" > 

         <RelativeLayout 
          android:id="@+id/relative_header" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:background="#818085" > 

          <ImageView 
           android:id="@+id/UserIconPic_laugh" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignParentLeft="true" 
           android:layout_alignParentTop="true" 
           android:layout_weight="0.4" 
           android:src="@drawable/ic_launcher" 
           app:ignore="ObsoleteLayoutParam,ContentDescription" /> 

          <Button 
           android:id="@+id/btn_Vote_laugh" 
           android:layout_width="wrap_content" 
           android:layout_height="match_parent" 
           android:layout_alignParentRight="true" 
           android:layout_alignParentTop="true" 
           android:layout_weight=".4" 
           android:text="VOTE" 
           app:ignore="ObsoleteLayoutParam,HardcodedText" /> 

          <TextView 
           android:id="@+id/txt_number_of_vote_laugh" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignBaseline="@+id/btn_Vote_laugh" 
           android:layout_alignBottom="@+id/btn_Vote_laugh" 
           android:layout_toRightOf="@+id/UserIconPic_laugh" 
           android:layout_weight=".4" 
           android:textAppearance="?android:attr/textAppearanceMedium" 
           app:ignore="ObsoleteLayoutParam,HardcodedText" /> 
         </RelativeLayout> 
        </TableRow> 

        <TableRow 
         android:id="@+id/tableRow_MainText_laugh" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 

         <TextView 
          android:id="@+id/txt_MainText_laugh" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="@string/whatweare_about_main" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          app:ignore="HardcodedText" /> 
        </TableRow> 

        <TableRow 
         android:id="@+id/tableRow_Footer_laugh" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" > 

         <RelativeLayout 
          android:id="@+id/relative_footer" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:background="#bababa" > 

          <Button 
           android:id="@+id/btn_Share_laugh" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="Share" 
           app:ignore="HardcodedText" /> 

          <ImageButton 
           android:id="@+id/btn_facebook_laugh" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_centerVertical="true" 
           android:layout_toLeftOf="@+id/btn_twitter_laugh" 
           android:src="@drawable/pic2" 
           app:ignore="ContentDescription" /> 

          <ImageButton 
           android:id="@+id/btn_twitter_laugh" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignParentRight="true" 
           android:layout_alignTop="@+id/btn_facebook_laugh" 
           android:src="@drawable/pic3" 
           app:ignore="ContentDescription" /> 
         </RelativeLayout> 
        </TableRow> 
       </TableLayout> 
      </RelativeLayout> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow_light" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       app:ignore="UselessParent" > 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" > 

        <TableRow 
         android:id="@+id/tableRow_header_light" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" > 

         <RelativeLayout 
          android:id="@+id/RelativeLayout02" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:background="#818085" > 

          <ImageView 
           android:id="@+id/UserIconPic_light" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignParentLeft="true" 
           android:layout_alignParentTop="true" 
           android:layout_weight="0.4" 
           android:src="@drawable/ic_launcher" 
           app:ignore="ObsoleteLayoutParam,ContentDescription" /> 

          <Button 
           android:id="@+id/btn_Vote_light" 
           android:layout_width="wrap_content" 
           android:layout_height="match_parent" 
           android:layout_alignParentRight="true" 
           android:layout_alignParentTop="true" 
           android:layout_weight=".4" 
           android:text="VOTE" 
           app:ignore="ObsoleteLayoutParam,HardcodedText" /> 

          <TextView 
           android:id="@+id/txt_vote_light" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignBaseline="@+id/btn_Vote_light" 
           android:layout_alignBottom="@+id/btn_Vote_light" 
           android:layout_toRightOf="@+id/UserIconPic_light" 
           android:layout_weight=".4" 
           android:textAppearance="?android:attr/textAppearanceMedium" 
           app:ignore="ObsoleteLayoutParam,HardcodedText" /> 
         </RelativeLayout> 
        </TableRow> 

        <TableRow 
         android:id="@+id/tableRow_content_light" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 

         <FrameLayout 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" > 

          <TextView 
           android:id="@+id/textView1" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_gravity="bottom|center" 
           android:maxLength="300" 
           android:text="@string/Main_laugh" 
           android:textAppearance="?android:attr/textAppearanceSmall" 
           app:ignore="HardcodedText" /> 

          <ImageView 
           android:id="@+id/light_image" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           app:ignore="ContentDescription" /> 
         </FrameLayout> 
        </TableRow> 

        <TableRow 
         android:id="@+id/tableRow_Footer_light" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" > 

         <RelativeLayout 
          android:id="@+id/RelativeLayout01" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:background="#bababa" > 

          <Button 
           android:id="@+id/Btn_Share_light" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="Share" 
           app:ignore="HardcodedText" /> 

          <ImageButton 
           android:id="@+id/btn_facebook_light" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_centerVertical="true" 
           android:layout_toLeftOf="@+id/btn_twitter_light" 
           android:src="@drawable/pic2" 
           app:ignore="ContentDescription" /> 

          <ImageButton 
           android:id="@+id/btn_twitter_light" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignParentRight="true" 
           android:layout_alignTop="@+id/btn_facebook_light" 
           android:src="@drawable/pic3" 
           app:ignore="ContentDescription" /> 
         </RelativeLayout> 
        </TableRow> 
       </TableLayout> 
      </RelativeLayout> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow_fun" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       app:ignore="UselessParent" > 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" > 

        <TableRow 
         android:id="@+id/tableRow_header_fun" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" > 

         <RelativeLayout 
          android:id="@+id/RelativeLayout04" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:background="#818085" > 

          <ImageView 
           android:id="@+id/UserIconPic_fun" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignParentLeft="true" 
           android:layout_alignParentTop="true" 
           android:layout_weight="0.4" 
           android:src="@drawable/ic_launcher" 
           app:ignore="ObsoleteLayoutParam,ContentDescription" /> 

          <Button 
           android:id="@+id/btn_vote_fun" 
           android:layout_width="wrap_content" 
           android:layout_height="match_parent" 
           android:layout_alignParentRight="true" 
           android:layout_alignParentTop="true" 
           android:layout_weight=".4" 
           android:text="VOTE" 
           app:ignore="ObsoleteLayoutParam,HardcodedText" /> 

          <TextView 
           android:id="@+id/txt_vote_number_fun" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignBaseline="@+id/btn_vote_fun" 
           android:layout_alignBottom="@+id/btn_vote_fun" 
           android:layout_toRightOf="@+id/UserIconPic_fun" 
           android:layout_weight=".4" 
           android:textAppearance="? android:attr/textAppearanceMedium" 
           app:ignore="ObsoleteLayoutParam,HardcodedText" /> 
         </RelativeLayout> 
        </TableRow> 

        <TableRow 
         android:id="@+id/tableRow_Content_fun" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" > 

         <RelativeLayout 
          android:id="@+id/relativelayout" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" > 

          <ImageView 
           android:id="@+id/image_fun" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent" 
           android:adjustViewBounds="true" 
           android:baselineAlignBottom="true" 
           android:cropToPadding="true" 
           app:ignore="ContentDescription" /> 

          <TextView 
           android:id="@+id/myImageViewText" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignBottom="@+id/image_fun" 
           android:layout_alignLeft="@+id/image_fun" 
           android:layout_alignRight="@+id/image_fun" 
           android:layout_alignTop="@+id/image_fun" 
           android:layout_margin="1dp" 
           android:gravity="center" 
           android:text="@string/main_light" 
           android:textColor="#000000" /> 
         </RelativeLayout> 
        </TableRow> 

        <TableRow 
         android:id="@+id/tableRow_Footer_fun" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" > 

         <RelativeLayout 
          android:id="@+id/RelativeLayout03" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:background="#bababa" > 

          <Button 
           android:id="@+id/Btn_share_fun" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="Share" 
           app:ignore="HardcodedText" /> 

          <ImageButton 
           android:id="@+id/btn_facebook_fun" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_centerVertical="true" 
           android:layout_toLeftOf="@+id/btn_twitter_fun" 
           android:src="@drawable/pic2" 
           app:ignore="ContentDescription" /> 

          <ImageButton 
           android:id="@+id/btn_twitter_fun" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignParentRight="true" 
           android:layout_alignTop="@+id/btn_facebook_fun" 
           android:src="@drawable/pic3" 
           app:ignore="ContentDescription" /> 
         </RelativeLayout> 
        </TableRow> 
       </TableLayout> 
      </RelativeLayout> 
     </TableRow> 
    </TableLayout> 

</ScrollView> 

tôi đang CustomeAdapter Lớp

package com.example.laysapp.ContentListView; 
import java.util.ArrayList; 
import java.util.List; 

import com.example.laysapp.R; 
import com.example.laysapp.RegisteredUser.LighterShare; 

import android.app.Application; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.webkit.WebView.FindListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ContentItemAdapter extends BaseAdapter { 

private final List<Content_Items> items; 

public ContentItemAdapter(final Context context, final int itemResId, 
     final ArrayList<Content_Items> items2) { 
    this.items = items2; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return items.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@SuppressWarnings("null") 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    final Content_Items item = this.items.get(position); 
    View itemView = null; 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) parent.getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     itemView = inflater.inflate(R.layout.contentlistrow, null); 
    } else { 
     itemView = convertView; 
    } 

    // Set the text of the Laugh Row Start 
    TextView txtLaugh_Vote = (TextView) itemView 
      .findViewById(R.id.txt_number_of_vote_laugh); 
    txtLaugh_Vote.setText(item.getLaughNo_Vote()); 

    TextView txtlaugh_Content = (TextView) itemView 
      .findViewById(R.id.txt_MainText_laugh); 
    txtlaugh_Content.setText(item.getLaughContent()); 

    ImageView imgView_Laugh_UserPic = (ImageView) itemView 
      .findViewById(R.id.UserIconPic_laugh); 

    imgView_Laugh_UserPic.setImageBitmap(item.getLaughtUserPic()); 

    // Set the text of the Laugh Row End 

    // Setting of Light Row Start 

    TextView txtLight_Vote = (TextView) itemView 
      .findViewById(R.id.txt_vote_light); 
    txtLight_Vote.setText(item.getLaughNo_Vote()); 

    TextView txtlight_Content = (TextView) itemView 
      .findViewById(R.id.textView1); 
    txtlight_Content.setText(item.getLaughContent()); 

    ImageView imgView_Light_UserPic = (ImageView) itemView 
      .findViewById(R.id.UserIconPic_light); 

    imgView_Light_UserPic.setImageBitmap((item.getLightStoriesUserPic())); 

    ImageView imgView_LightContent_Image = null; 
    // Check weather ContentImage is Available For Light 

    String Check = item.getIsLightContentAvaiable(); 
    if (Check == "yes") { 
     imgView_LightContent_Image = (ImageView) imgView_LightContent_Image 
       .findViewById(R.id.light_image); 
     imgView_LightContent_Image.setImageBitmap(item 
       .getLightStoriesContentImagePic()); 
    } 

    // Setting of Light Row End 

    // Set the text of the Funto Row Start 

    TextView txtfunto_Vote = (TextView) itemView 
      .findViewById(R.id.txt_vote_number_fun); 
    txtfunto_Vote.setText(item.getLaughNo_Vote()); 

    TextView txtfunto_Content = (TextView) itemView 
      .findViewById(R.id.myImageViewText); 
    txtfunto_Content.setText(item.getLaughContent()); 

    // Set the text of the Funto Row End 

    ImageView imgView_funto_UserPic = (ImageView) itemView 
      .findViewById(R.id.UserIconPic_fun); 

    imgView_funto_UserPic.setImageBitmap(item.getfuntoUserPic()); 

    ImageView imgView_funto_ContentPic = (ImageView) itemView 
      .findViewById(R.id.image_fun); 

    imgView_funto_ContentPic.setImageBitmap(item.getfuntoContentImagePic()); 

    Button MainVote_Laugh = null; 
    MainVote_Laugh = (Button) MainVote_Laugh 
      .findViewById(R.id.btn_Vote_laugh); 
    MainVote_Laugh.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).Show(); 
     } 
    }); 

    return itemView; 
} 
} 

Trả lời

25

Thay thế dòng này:

Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show(); 

với dòng này:

Toast.makeText(v.getContext(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show(); 
4

Bạn có ngữ cảnh ngay trong hàm tạo. tạo một biến ngữ cảnh cấp lớp nói ctx. bên trong constructor viết

this.ctx = context; 

cuối cùng thay thế getApplication() trong

Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).Show(); 

với ctx.

1

Bạn cũng có thể sử dụng parent.getContext(), mã đầy đủ của bánh mì nướng là như dưới đây

Toast.makeText(parent.getContext(), "view clicked", Toast.LENGTH_SHORT).show(); 
Các vấn đề liên quan