【问题标题】:Having troubles getting data from List Adapter for OnItemClick从列表适配器获取 OnItemClick 数据时遇到问题
【发布时间】:2014-06-04 22:19:12
【问题描述】:

当我的 listadapter 设置为我的 listview 时,我在获取获取的数据时遇到问题。我试图在onItemClick 中获取这些数据,以便我可以将其放入我的额外意图中,以便我的其他活动获得。

问题

目前我已经创建了空字符串变量,然后在我的适配器中通过我的模型中的方法为字符串分配了所需的文本。然而,我遇到的问题是,被拉出的 text 不是 onitemclick 被调用位置的正确文本。

这里有一些代码...

XMLParseActivity

​​>
public class XMLParseActivity extends Activity implements AdapterView.OnItemClickListener {

 private ListView mIssueListView;
 private IssueParser mIssueParser;
 private List<IssueFeed> mIssueList;
 private IssueAdapter mIssueAdapter;

 private String result_connectedtype = "";
 private String result_symptom = "";
 private String result_problem = "";
 private String result_solution = "";
 private String result_comments = "";

...

public class IssueAdapter extends ArrayAdapter<IssueFeed> {
    public List<IssueFeed> issueFeedList;

    public IssueAdapter(Context context, int textViewResourceId, List<IssueFeed> issueFeedList) {
        super(context, textViewResourceId, issueFeedList);
        this.issueFeedList = issueFeedList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        IssueHolder issueHolder = null;
        if (convertView == null) {
            view = View.inflate(XMLParseActivity.this, R.layout.issue_list_item, null);
            issueHolder = new IssueHolder();
            issueHolder.issueConnectedType = (TextView) view.findViewById(R.id.result_connected_type);
            issueHolder.issueSymptomView = (TextView) view.findViewById(R.id.result_symptom);
            view.setTag(issueHolder);
        } else {
            issueHolder = (IssueHolder) view.getTag();
        }
        IssueFeed issueFeed = issueFeedList.get(position);
        issueHolder.issueConnectedType.setText(issueFeed.getConnected_type());
        issueHolder.issueSymptomView.setText(issueFeed.getSymptom());

        //THE DATA I WANT TO USE IN MY INTENT
        result_solution = issueFeed.getSolution();
        result_comments = issueFeed.getComments();
        result_connectedtype = issueFeed.getConnected_type();
        result_problem = issueFeed.getProblem();
        result_symptom = issueFeed.getSymptom();

        return view;
    }
}

static class IssueHolder {
    public TextView issueSymptomView;
    public TextView issueConnectedType;
}

@Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {

    //Put the strings in intent extra
    Intent intent = new Intent(this, SpecificIssueActivity.class);
    intent.putExtra("symptom", result_symptom);
    intent.putExtra("problem", result_problem);
    intent.putExtra("solution", result_solution);
    intent.putExtra("comments", result_comments);
    intent.putExtra("connectedtype", result_connectedtype);
    startActivity(intent);
}

listAdapter 在下面代码的 asynctask 中设置

public class DoLocalParse extends AsyncTask<String, Void, List<IssueFeed>> {
    ProgressDialog prog;
    String jsonStr = null;
    Handler innerHandler;

    @Override
    protected void onPreExecute() {
        prog = new ProgressDialog(XMLParseActivity.this);
        prog.setMessage("Loading....");
        prog.show();
    }

    @Override
    protected List<IssueFeed> doInBackground(String... params) {
        mIssueParser = new IssueParser(null);
        mIssueList = mIssueParser.parseLocally(params[0]);

        return mIssueList;
    }

    @Override
    protected void onPostExecute(List<IssueFeed> result) {
        prog.dismiss();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                mIssueAdapter = new IssueAdapter(XMLParseActivity.this, R.layout.issue_list_item,
                        mIssueList);
                int count = mIssueAdapter.getCount();
                if (count != 0 && mIssueAdapter != null) {
                    mIssueListView.setAdapter(mIssueAdapter);
                }
            }
        });
    }
}

而我的模型IssueFeed 看起来像这样

public class IssueFeed implements Serializable {

private String connected_type;
private String symptom;
private String problem; 
private String solution; 
private String comments; 

public IssueFeed() {
}

public IssueFeed(String connected_type, String symptom, String problem, String solution, String comments) {
    this.connected_type = connected_type;
    this.symptom = symptom;
    this.problem = problem;
    this.solution = solution;
    this.comments = comments;
}

public String getConnected_type() {
    return connected_type;
}

public String getSymptom() {
    return symptom;
}

public String getProblem() {
    return problem;
}

public String getSolution() {
    return solution;
}

public String getComments() {
    return comments;
}

public void setConnected_type(String connected_type) {
    this.connected_type = connected_type;
}

public void setSymptom(String symptom) {
    this.symptom = symptom;
}

public void setProblem(String problem) {
    this.problem = problem;
}

public void setSolution(String solution) {
    this.solution = solution;
}

public void setComments(String comments) {
    this.comments = comments;
}
}

【问题讨论】:

  • 为什么要向适配器传递数据?如果您定义了模型,那么您可以从您的主类中获取数据。如果我误解了这个问题,请纠正我。
  • @mike20132013 是的,我猜你是对的,我想我只是不确定获取数据的最佳方法。现在,正如您在适配器代码中看到的那样,我的 IssueFeed 和 issueFeedList 已定义,因此我能够使用模型中的方法来提取数据。你会推荐一个替代品吗?

标签: java android string android-listview listadapter


【解决方案1】:

我已经通过从我的模型中的一些简单方法获取数据来获取值来解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多