我在ListView

时间:2017-08-16 13:09:00

标签: android

我想在我的应用程序上实现精简,其中项目将来自服务器但在下面的Custom_List类代码中只有一个项目即将到来而另一个项目显示为null。This is screenshot of items. 在这个项目上,我正在检索身份,年龄,身高,社区,种姓,职业,教育,收入,地点,图片。

public class RefineCustomList extends ArrayAdapter<String>  {
private NetworkImageView imageView;
private ImageLoader imageLoader;
private final String[] ids;
private String[] ages;
private String[] heights;
public String[] communities;
public String[] castes;
public String[] educations;
public String[] occupations;
public String[] incomes;
public String[] pics;
public String[] locations;
public String[] shortlist;
public String[] expressinterest;
private Activity context;

public RefineCustomList(Activity context, String[] ids, String[] ages, String[] heights, String[] communities, String[] castes,
                        String[] educations, String[] occupations, String[]incomes, String[]pics, String[] locations,
                        String[] shortlist, String[] expressinterest) {
    super(context, R.layout.list_view_layout,ids);
    this.ids = ids;
    this.ages = ages;
    this.heights = heights;
    this.communities = communities;
    this.castes = castes;
    this.educations = educations;
    this.occupations = occupations;
    this.incomes = incomes;
    this.pics = pics;
    this.locations = locations;
    this.context = context;
    this.shortlist = shortlist;
    this.expressinterest = expressinterest;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.refine_custom_list, null, true);

    String url1 = "https://www.maangal.com/thumb/thumb_";
    String url =url1+pics[position];

    imageView = (NetworkImageView) listViewItem.findViewById(R.id.offer_image);
    imageLoader = CustomVolleyRequest.getInstance(this.getContext()).getImageLoader();
    imageLoader.get(url, ImageLoader.getImageListener(imageView,R.drawable.image,android.R.drawable.ic_dialog_alert));
    imageView.setImageUrl(url,imageLoader);
    TextView textViewId = (TextView) listViewItem.findViewById(R.id.textViewId);
    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);

    textViewId.setText(ids[position]);
    textViewName.setText( ages[position]+" years"+" , "+heights[position]+" cm"+", "+communities[position]+" : "+castes[position]+" , "+educations[position]+" , "+occupations[position]+" , "+incomes[position]+", "+locations[position]);

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), BlankActivity.class);
            Toast.makeText(getContext(), ids[position], Toast.LENGTH_LONG).show();
            i.putExtra("id", ids[position]);
            v.getContext().startActivity(i);
        }
    });

    Button btnSort =(Button) listViewItem.findViewById(R.id.btnshort);
    btnSort.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(),"Shortlisted",Toast.LENGTH_LONG).show();
        }
    });

    Button btnChat =(Button) listViewItem.findViewById(R.id.btnchat);
    btnChat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(),"Chatting",Toast.LENGTH_LONG).show();
        }
    });

    Button declineButton = (Button)listViewItem.findViewById(R.id.declineButton);
    declineButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(),"Decline",Toast.LENGTH_LONG).show();
        }
    });
    return listViewItem;
}

}  

这是解析代码

 public class RefineActivity extends FragmentActivity {

SessionManager session;
String email;

public String JSON_URL;
private ListView listView;
Button rfb;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.refine_activity);
    // Session class instance
    session = new SessionManager(this);
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    email = user.get(SessionManager.KEY_EMAIL);
    Log.e("email==========>", email);
    //JSON_URL = "http://10.0.2.2/xp/ei_sent_pending.php?matri_id="+email;

    listView = (ListView) findViewById(R.id.listView);

    rfb = (Button) findViewById(R.id.refineBtn);
    rfb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager = getFragmentManager();
            RefineFragment ls_fragment = new RefineFragment();
            ls_fragment.show(fragmentManager, "simple fragment");
        }
    });
    Intent intent = getIntent();
    final String response = getIntent().getExtras().getString("res");
    Log.e("responde rfi", response);
    showJSON(response);
}

protected void showJSON(String json) {
    ParseJSON pj = new ParseJSON(json);
    pj.parseJSON();
    RefineCustomList cl = new RefineCustomList(this, ParseJSON.ids, ParseJSON.ages, ParseJSON.heights, ParseJSON.communities, ParseJSON.castes, ParseJSON.educations, ParseJSON.occupations, ParseJSON.incomes, ParseJSON.pics, ParseJSON.locations, ParseJSON.shortlist, ParseJSON.expressinterest);
    listView.setAdapter(cl);
  }
}

这是项目数据显示的日志

      responde rfi: {"result":[{"id":"Mgl11638","age":"21","height":"160","caste":"Brahmin","community":"Kumaoni","education":"MA","occupation":"Not Working","income":"Will tell later","pic":"Mgl11638.jpg","location":"Almora"},{"id":"Mgl16111","age":"22","height":"160","caste":"Brahmin","community":"Kumaoni","education":"B.Sc","occupation":"Student","income":"Will tell later","pic":"","location":"Almora"},{"id":"Mgl11658","age":"22","height":"154","caste":"Brahmin","community":"Kumaoni","education":"Undergraduate","occupation":"Student","income":"Will tell later","pic":"","location":"Lucknow"},{"id":"Mgl11621","age":"21","height":"134","caste":"Brahmin","community":"Kumaoni","education":"MA","occupation":"Not Working","income":"No income","pic":"","location":"Bareilly"}]}

1 个答案:

答案 0 :(得分:0)

为什么在适配器中使用过多的Arrays?只需按照以下步骤操作即可。

  1. 将所有字符串作为单个字段放在Object中。
  2. 将该对象作为参数从您的Activity传递到适配器。
  3. 从适配器中的该对象获取详细信息。
  4. 然后适配器将自动获取多个数据并填充到列表视图中。我的建议是将课程扩展为 BaseAdapter ,而不是 ArrayAdapter

    这是你的对象。我专门为你创造了它。创建一个新的java类(不是活动,只是一个.java文件)并将此代码放入其中。

    public class DetailsObject implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private String id;
    private String age;
    private String height;
    public String community;
    public String caste;
    public String education;
    public String occupation;
    public String income;
    public byte[] pic;
    public String location;
    public String shortlist;
    public String expressinterest;
    
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getAge() {
        return age;
    }
    
    public void setAge(String age) {
        this.age = age;
    }
    
    public String getHeight() {
        return height;
    }
    
    public void setHeight(String height) {
        this.height = height;
    }
    
    public String getCommunity() {
        return community;
    }
    
    public void setCommunity(String community) {
        this.community = community;
    }
    
    public String getCaste() {
        return caste;
    }
    
    public void setCaste(String caste) {
        this.caste = caste;
    }
    
    public String getEducation() {
        return education;
    }
    
    public void setEducation(String education) {
        this.education = education;
    }
    
    public String getOccupation() {
        return occupation;
    }
    
    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }
    
    public String getIncome() {
        return income;
    }
    
    public void setIncome(String income) {
        this.income = income;
    }
    
    public byte[] getPic() {
        return pic;
    }
    
    public void setPic(byte[] pic) {
        this.pic = pic;
    }
    
    public String getLocation() {
        return location;
    }
    
    public void setLocation(String location) {
        this.location = location;
    }
    
    public String getShortlist() {
        return shortlist;
    }
    
    public void setShortlist(String shortlist) {
        this.shortlist = shortlist;
    }
    
    public String getExpressinterest() {
        return expressinterest;
    }
    
    public void setExpressinterest(String expressinterest) {
        this.expressinterest = expressinterest;
    }
    }
    

    在您的适配器类中,将extends ArrayAdapter<String>更改为extends BaseAdapter,将所有String[]更改为String,并在构造函数中,将juzt传递(Context context, DetailsObject detailObject)作为参数。并在您的活动中调用适配器,如下所示:

    RefineCustomList refineCustomList;
    refineCustomList = new RefineCustomList(MainActivity.this,detailObject);
    yourListView.setAdapter(refineCustomList);              
    

    多数民众赞成......