从List Adapter for OnItemClick获取数据时遇到麻烦

时间:2014-06-04 22:19:13

标签: java android string android-listview listadapter

我遇到了将listadapter设置为listview时获取的数据的问题。我试图在onItemClick中获取这些数据,这样我就可以将其用于我的其他活动中以获取其他活动。

问题

目前我已经创建了空字符串变量,然后在我的适配器中通过我的模型中的方法为所需的文本分配字符串。但是我遇到的问题是,正在提取的文本对于要求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;
}
}

1 个答案:

答案 0 :(得分:0)

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