从自定义列表视图中获取字符串

Getting String out of custom List View

所以我想做的是在单击某个项目时获取字符串值。 但是通过这段代码,当我点击项目时,我得到了转换器的位置 class 或类似的东西,我不明白。 "MainActivity$Converter@30e6eb1" 在敬酒中。我只希望主题部分出现在 Toast 中。

   @Override
    protected void onPostExecute(String result) {

        JSONObject jsonObject;
        JSONArray jsonArray;
        CustomAdapter customAdapter;
        ListView listview;

        listview = (ListView) findViewById(R.id.xlistview);
        customAdapter = new CustomAdapter(getApplicationContext(), R.layout.row_layout);
        listview.setAdapter(customAdapter);
                                                                                 //Log.d("json_raw_home",result);

        if (result != null) {                                                    //Log.d("post execute",result);
            try {

                jsonObject = new JSONObject(result);
                jsonArray = jsonObject.getJSONArray("data");
                int count = 0;
                String subject, description;
                while (count < jsonArray.length()) {
                    JSONObject JO = jsonArray.getJSONObject(count);
                    subject = JO.getString("subject");
                    description = JO.getString("description");

                    Converter converter = new Converter(subject, description);
                    customAdapter.add(converter);

                    count++;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = String.valueOf(parent.getItemAtPosition(position));
                Toast.makeText(getApplicationContext(),item, Toast.LENGTH_LONG).show();}
        });
    }
}

public class CustomAdapter extends ArrayAdapter {
    List list = new ArrayList();

    public CustomAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        row = convertView;
        ContactHolder contactHolder;

        if(row==null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            contactHolder=new ContactHolder();
            contactHolder.tv_subject=(TextView)row.findViewById(R.id.xsubject_tv);
            contactHolder.tv_description=(TextView)row.findViewById(R.id.xdescription_tv);
            row.setTag(contactHolder);
        }

        else
        {
            contactHolder=(ContactHolder)row.getTag();
        }

        Converter converter = (Converter) this.getItem(position);
        contactHolder.tv_subject.setText(converter.getSubject());
        contactHolder.tv_description.setText(converter.getDescription());
        return row;
    }

    public class ContactHolder{
        TextView tv_subject,tv_description;
    }


}

public class Converter {
    private String subject,description;

    public Converter(String subject, String description) {
        this.setSubject(subject);
        this.setDescription(description);
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

}

改变

String item = String.valueOf(parent.getItemAtPosition(position));

String item = ((Converter)parent.getAdapter().getItem(position)).getSubject() 

您将 Converter 对象传递给 CustomAdapter 以列出它们。因此,如果您调用 getItem,则必须将对象连接到转换器。所以;

改变

String item = String.valueOf(parent.getItemAtPosition(position));

String item = ((Converter)parent.getAdapter().getItem(position)).getSubject();

所以最终代码:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = ((Converter)parent.getAdapter().getItem(position)).getSubject();
                Toast.makeText(getApplicationContext(),item, Toast.LENGTH_LONG).show();}
        });

parent.getItemAtPosition(位置); return 无论你 return 来自 getItem(position).

的位置的对象

转换器项目 = (转换器)parent.getItemAtPosition(位置);

Toast.makeText(getApplicationContext(),item.getSubject(),Toast.LENGTH_LONG).show();