当前位置: 首页 > 知识库问答 >
问题:

Android ViewPager不显示任何视图

谷善
2023-03-14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:background="@drawable/rounded_corner_view_bg">

    <TextView
        android:id="@+id/memberRegion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/doctor_location" />
        <TextView
            android:id="@+id/memberLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/availabilityPager"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

</LinearLayout>
package eng.hasan.medico;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.sql.Time;

public class AvailabilityPagerAdapter extends PagerAdapter {

    int innserSearchedDocIndex;
    AvailabilityPagerAdapter(int searchedDocIndex){
        innserSearchedDocIndex = searchedDocIndex;
    }

    @Override
    public int getCount() {
        return Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup view, int i) {
        LayoutInflater lI = (LayoutInflater) Globals.memberPageGlobalObject.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = lI.inflate(R.layout.availability_item ,view ,true);
        TextView availabilityDay = v.findViewById(R.id.availabilityDay);
        String[] days = {"فارغ", "السبت", "الأحد", "الاثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة"};
        availabilityDay.setText(days[Integer.parseInt(Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.get(i).get(0))]);
        TextView availabilityTime = v.findViewById(R.id.availabilityTime);
        Time avTimeFrom = Time.valueOf(Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.get(i).get(1));
        Time avTimeTo = Time.valueOf(Globals.searchedDocs.get(innserSearchedDocIndex).availabilities.get(i).get(2));
        availabilityTime.setText(avTimeFrom.toString() + "\n" + avTimeTo);
        return v;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="100dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/availabilityDay"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:textAlignment="center"
        android:background="@drawable/left_right_top_rounded_corner"
        android:text="السبت" />

    <TextView
        android:id="@+id/availabilityTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:text="3 م \n إلى \n 9 م" />

    <Button
        android:id="@+id/availabilityReserve"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@drawable/left_right_bottom_rounded_corner_red"
        android:text="احجز" />

</LinearLayout>
package eng.hasan.medico;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MemberPage extends AppCompatActivity {

    String title = "بيانات الدكتور";
    ListView memberList;
    TextView innerMemberInfo;
    ProgressBar docInfoProgressBar;
    ProgressBar headerProgressBar;
    ImageView userLikeButton;
    RatingBar docRatingBar;
    boolean userSignedIn = false;
    public boolean docLiked;
    public boolean docRated;
    public int views ;
    public float rate;
    boolean RATING_MODE = false;
    ViewPager avPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_page);
        Globals.memberPageGlobalObject = this;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        setActivityTitle();
        rate = 0;
        checkUserLiked();
        views = Globals.searchedDocs.get(getIntent().getExtras()
                .getInt("docObjIndex")).views;
        setMemberList();
        /*Toast.makeText(this ,String.valueOf(Globals.searchedDocs.get(getIntent().getExtras()
                .getInt("docObjIndex")).availabilities.get(0).size()) ,Toast.LENGTH_LONG).show();*/




        ////////////////////////////////////////////////////
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    private void setMemberList(){
        memberList = findViewById(R.id.memberList);
        memberList.setAdapter(new MemberListArrayAdapter(getIntent().getExtras().getInt("docObjIndex")
                ,docLiked));
    }

    private void checkUserLiked(){
        if(!userSignedIn){
            File liked = new File(getFilesDir() + "/likes");
            if(liked.exists()){
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("likes")));
                    String line;
                    while((line = br.readLine()) != null)
                    {
                        int pos = 0;
                        for(int i = 0 ;i < line.length() ;i++){
                            if(line.substring(i ,i + 1).equals("-")){
                                pos = i;
                                break;
                            }
                        }
                        int docId = Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
                                .docId);
                        if(line.substring(0 ,pos).equals(String.valueOf(docId))){
                            docLiked = true;
                            break;
                        }
                    }
                    br.close();
                }catch (FileNotFoundException e){
                    docLiked = false;
                    //Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();
                }catch (IOException e){
                    docLiked = false;
                    //Toast.makeText(this ,"خطأ فتح الذاكرة" ,Toast.LENGTH_LONG).show();
                }
            }else {
                docLiked = false;
                //Toast.makeText(this ,"الملف غير موجود" ,Toast.LENGTH_LONG).show();
            }
        }
        checkUserRated();
    }

    private void checkUserRated(){
        if(!userSignedIn){
            File liked = new File(getFilesDir() + "/rates");
            if(liked.exists()){
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("rates")));
                    String line;
                    while((line = br.readLine()) != null)
                    {
                        int pos = 0;
                        for(int i = 0 ;i < line.length() ;i++){
                            if(line.substring(i ,i + 1).equals("-")){
                                pos = i;
                                break;
                            }
                        }
                        int docId = Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
                                .docId);
                        if(line.substring(0 ,pos).equals(String.valueOf(docId))){
                            rate = Float.parseFloat(line.substring(pos + 1 ,line.length()));
                            docRated = true;
                            break;
                        }
                    }
                    br.close();
                }catch (FileNotFoundException e){
                    docRated = false;
                    //Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();
                }catch (IOException e){
                    docRated = false;
                    //Toast.makeText(this ,"خطأ فتح الذاكرة" ,Toast.LENGTH_LONG).show();
                }
            }else {
                docRated = false;
                //Toast.makeText(this ,"الملف غير موجود" ,Toast.LENGTH_LONG).show();
            }
        }
    }

    private void setActivityTitle(){
        LayoutInflater lI = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = lI.inflate(R.layout.activity_title ,null ,false);
        TextView tv = v.findViewById(R.id.activityTitle);
        tv.setText(title);
        headerProgressBar = v.findViewById(R.id.titleProgressBar);
        getSupportActionBar().setCustomView(v);
    }

    public void loadMemberInfo(TextView memberInfo ,String memberId){
        innerMemberInfo = memberInfo;
        new Thread(new GetMemberInfo (memberId)).start();
    }

    public void showMemberInfo(JSONArray info){
       if(info.length() > 0){
           String processedInfo = "<ul>";
           try {
               for(int i = 0 ;i < info.length() ;i++){
                   processedInfo += "<li>" + info.getJSONObject(i).getString("info") + "</li>";
               }
               processedInfo += "</ul>";
               innerMemberInfo.setText(Html.fromHtml(processedInfo));
           }catch (JSONException e){

           }

       }
    }

    public void hideInfoProgressBar(){
        docInfoProgressBar.setVisibility(ProgressBar.INVISIBLE);
        if(!userSignedIn){
            new Thread(new RecordDocView(0 ,
                    Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
                            .docId))).start();
        }
    }

    public void hideHeaderProgressBar(){
        headerProgressBar.setVisibility(ProgressBar.INVISIBLE);
    }

    public void assignInfoPBar(ProgressBar bar){
        docInfoProgressBar = bar;
    }

    public  void recordUserLike(){
        if(!userSignedIn){
            new Thread(new RecordUserLike(0 ,
                    Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
                            .docId))).start();
            headerProgressBar.setVisibility(ProgressBar.VISIBLE);
        }
    }

    public void recordUserUnlike(){
        int likeId = -1;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("likes")));
            String line;
            while ((line = br.readLine()) != null) {
                int pos = 0;
                for(int i = 0 ;i < line.length() ;i++){
                    if(line.substring(i ,i + 1).equals("-")){
                        pos = i;
                        break;
                    }
                }
                int docId = Integer.parseInt(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex"))
                        .docId);
                if(line.substring(0 ,pos).equals(String.valueOf(docId))){
                    likeId = Integer.parseInt(line.substring(pos + 1 ,line.length()));
                    break;
                }
            }
        }catch (IOException e){

        }
        new Thread(new RecordUserLike(likeId)).start();
        headerProgressBar.setVisibility(ProgressBar.VISIBLE);
    }

    public void assignUserLikeButton(ImageView likeButton){
        userLikeButton = likeButton;
    }

    public void processLiked(int likeId ,int docId){
       if(!userSignedIn){
           userLikeButton.setImageResource(R.drawable.heart_solid);
           OutputStreamWriter oSW ;
           if(!userSignedIn){
               try {
                   oSW = new OutputStreamWriter(openFileOutput("likes" ,MODE_APPEND));
                   oSW.append( docId + "-" + likeId);
                   oSW.append("\n\r");
                   oSW.flush();
                   oSW.close();
               }catch (FileNotFoundException e){
                   Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();

               }catch (IOException e) {
                   Toast.makeText(this, "خطأ فتح الذاكرة", Toast.LENGTH_LONG).show();
               }
           }
       }
    }

    public void processUnliked(int likeId){
       if(!userSignedIn){
           File inputFile = new File(getFilesDir() + "/likes");
           File tempFile = new File(getFilesDir() +  "/likes_temp");

           try {
               BufferedReader reader = new BufferedReader(new FileReader(inputFile));
               BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

               String lineToRemove = Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex")).docId
                       + "-" + likeId;
               String currentLine;

               while((currentLine = reader.readLine()) != null) {
                   // trim newline when comparing with lineToRemove
                   String trimmedLine = currentLine.trim();
                   if(trimmedLine.equals(lineToRemove)) continue;
                   writer.write(currentLine + System.getProperty("line.separator"));
               }
               writer.close();
               reader.close();
               tempFile.renameTo(inputFile);
           }catch (IOException e){

           }
           userLikeButton.setImageResource(R.drawable.heart);
       }
    }

    public void recordRating(final float value){
        RATING_MODE = true;
        headerProgressBar.setVisibility(ProgressBar.VISIBLE);
        LayoutInflater lI = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       if (!userSignedIn){
           final AlertDialog alert = new AlertDialog.Builder(this ,R.style.MyDialogTheme).create();
          if(!docRated){
              View alertView = lI.inflate(R.layout.rate_doc_alert ,null ,false);
              alertView.setBackgroundColor(Color.TRANSPARENT);
              ((TextView) alertView.findViewById(R.id.rateAlertMessage)).setText("هل تود اختيار التقييم : " +
                      value + " لهذه الصفحة");
              ((Button) alertView.findViewById(R.id.alertPositiveButton)).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      new Thread(new RecordRating(Globals.searchedDocs.get(getIntent().getExtras().getInt("docObjIndex")).docId
                              ,value ,0)).start();
                      alert.dismiss();
                      RATING_MODE = true;
                  }
              });
              ((Button) alertView.findViewById(R.id.alertNegativeButton)).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      alert.dismiss();
                      hideHeaderProgressBar();
                      docRatingBar.setRating(0);
                      RATING_MODE = false;
                  }
              });
              alert.setView(alertView);
              alert.show();
          }else{

              View alertView = lI.inflate(R.layout.rate_doc_alert ,null ,false);
              alertView.setBackgroundColor(Color.TRANSPARENT);
              ((TextView) alertView.findViewById(R.id.rateAlertMessage)).setText("لقد قمت بالفعل بتقييم هذه الصفحة");
              ((Button) alertView.findViewById(R.id.alertPositiveButton)).setEnabled(false);
              ((Button) alertView.findViewById(R.id.alertNegativeButton)).setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      alert.dismiss();
                      docRatingBar.setRating(rate);
                      RATING_MODE = false;
                      hideHeaderProgressBar();
                  }
              });
              alert.setView(alertView);
              alert.setCancelable(false);
              alert.show();
          }
       }
    }

    public void processRated(int docId , float rateVal){
        OutputStreamWriter oSW ;
        if(!userSignedIn){
            try {
                oSW = new OutputStreamWriter(openFileOutput("rates" ,MODE_APPEND));
                oSW.append( docId + "-" + rateVal);
                oSW.append("\n\r");
                oSW.flush();
                oSW.close();
                docRated = true;
                docRatingBar.setRating(rateVal);
                RATING_MODE = false;
                rate = rateVal;

            }catch (FileNotFoundException e){
                Toast.makeText(this ,"خطأ فتح الملف" ,Toast.LENGTH_LONG).show();
                resetRating();

            }catch (IOException e) {
                Toast.makeText(this, "خطأ فتح الذاكرة", Toast.LENGTH_LONG).show();
                resetRating();
            }
        }
    }

    public void assignRateBar(RatingBar bar){
        docRatingBar = bar;
    }

    public void assignAvViewPager(ViewPager vP){
        avPager = vP;
        avPager.setAdapter(new AvailabilityPagerAdapter(getIntent().getExtras().getInt("docObjIndex") ,getSupportFragmentManager()));
    }

    public boolean isRatingModeOn(){
        return RATING_MODE;
    }

    public void resetRating(){
        docRatingBar.setRating(0);
        RATING_MODE = false;
    }
}

下面是XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layoutDirection="rtl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f5f5"
    tools:context="eng.hasan.medico.MemberPage">

    <ListView
        android:id="@+id/memberList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="60dp"
        android:dividerHeight="10dp"
        android:background="#f5f5f5"/>

    <TextView
        android:id="@+id/memberCustomersRatings"
        android:padding="10dp"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_height="50dp"
        android:textAlignment="center"
        android:gravity="center_vertical"
        android:textSize="18dp"
        android:text="تقييمات الزائرين"
        android:background="@drawable/rounded_bottom_corners_bg"/>

</RelativeLayout>

共有1个答案

贺彬
2023-03-14

适配器构造函数与从该活动调用它的方式不匹配:

AvailabilityPagerAdapter(int searchedDocIndex)

AvailabilityPagerAdapter(getIntent().getExtras().getInt("docObjIndex") ,getSupportFragmentManager())

没有适当的上下文,适配器不知道在哪里显示内容

 类似资料:
  • 我有个组件叫我的组件 模型贝宝。php 控制器paypal.php 意见 贝宝视图。html。php索引。html TMPL(文件夹)default.phpindex.html 在控制器中,我有这个代码 在模型中我有一段代码 在视图/模板/default.php我有这个 当我运行这个url

  • 我已经制作了Hello World RCP应用程序,得到了以下类结构: 向Perspective.createInitialLayout()添加额外代码: 但不显示视图。 我将breakpoint设置到perspective.createInitialLayout()中,发现它没有执行。 我的观点声明是: ApplicationWorkbenchAdvisor.GetInitialWindowPe

  • 我试图用自己的数据创建一个dataTable:代码如下: 但当我运行它时,我看到一张空桌子。我想查看我的数据,我怎么能?

  • 我目前正在使用Firebase_Auth.当我点击登录按钮并且Firebase连接到我的应用程序时,一切似乎都很好。它在终端上显示用户的ID。但是当我去消防基地时。 它无法识别任何已注册的用户。 Ps:问题是当用户注册时,它不会显示给firebase。

  • 我使用的是一个模板,我用一个改装2请求替换了占位符数据,该请求从URL解析数据的JSON文件,并将其放入对象列表中。我遇到了一个问题,首先执行改装调用并返回items=0,然后使用值items=0进一步执行代码,在调试器中执行了许多步骤后,它返回调用并显示正确的响应(items=12),但适配器/RecycleView操作似乎已经执行,并且它们不使用延迟到达的解析数据。 主要活动。Java语言 改

  • 问题内容: 我正在一个用于启动和运行的项目。我对docker- compose不太熟悉,我在文档中找不到真正的答案。根据文档,该命令从docker文件重建容器,但是这似乎没有发生。 当我尝试添加用于调试的命令时,没有任何反应。该程序已经抛出了一些打印命令,因此我尝试更改这些命令以确保它们始终打印相同的命令。我是否必须在命令中添加一些内容以进行容器重建? docker-compose / yml: