【问题标题】:Parsing a ListView item onclick to another activity and match a specific data from JSON将 ListView 项目 onclick 解析为另一个活动并匹配来自 JSON 的特定数据
【发布时间】:2016-02-07 21:58:56
【问题描述】:

任何人都可以帮助我将点击时的 ListView 项解析为另一个活动,最好是一个能够获取解析后的 id 并将其与一些 JSON 数据匹配的活动。我正在开发一个 volley 库,并且肯定希望获得点击的特定项目的相关数据。

        // Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            LinkedList<String> mLinked = new LinkedList<String>();
            for (int i = 0; i < DRUGS.length; i++) {
                    mLinked.add(DRUGS[i]);
            }

            setListAdapter(new MyListAdaptor(this, mLinked));

            ListView lv = getListView();
            lv.setFastScrollEnabled(true);

            lv.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            // When clicked, show a toast with the TextView text
                            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

                            // Start new intent by passing value
                            // End intent
                    }

            });
       }

我需要在其他活动中看到我的价值

public class Activity2_view extends Activity {

// 从 MainActivity 类中收集值 TextView tvView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);

    tvView = (TextView) findViewById(R.id.tvView);
    Intent intent = getIntent();

    String ItemName = intent.getStringExtra("jsonId");

    tvView.setText("Your drug name is: "  + ItemName);
}

}

【问题讨论】:

  • 您想正确获取点击项的文本吗?
  • 是的 Zahan 我需要文本然后 onClick 它必须获取单击的项目的其他相关详细信息,这些详细信息远程存储在 SQL 数据库中。到目前为止,我还设法创建了一个有效的 JSON 部分。我可以承认我正在使用 Google 的 volley lib,这也非常有用,这要归功于 Google Android。

标签: java android json listview


【解决方案1】:

在您的适配器中,在您的 getView() 方法中,您可以设置一个标签来匹配您需要链接的 JSON 数据的 id。例如:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int jsonId;  // Set the view's tag to the ID of the JSON data
                 // you want to link it to.
    convertView.setTag(jsonId);
}

那么,在你的OnItemClickListener

ListView lv = getListView();
lv.setFastScrollEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

        // Get the id from the view.
        int jsonId = (int) view.getTag();

        // Start new intent by passing value
        Intent newActivityIntent = new Intent(YourActivity.this, YourNewActivity.class);
        newActivityIntent.putExtra("jsonId", jsonId);

        // Start it.
        startActivity(newActivityIntent);
    }
});

如果您在getView() 方法中使用ViewHolder 进行优化,则ViewHolder 将成为您视图的标签。您需要向 ViewHolder 对象添加一个成员属性,您可以使用它来保存 id。

public class ViewHolder {
    private int mJsonId;
    private TextView mTextView;

    public void setId(int id) {
        mJsonId = id;
    }

    public int getJsonId() {
        return mJsonId;
    }
}

然后

ViewHolder viewHolder = (ViewHolder) view.getTag();
int jsonId = viewHolder.getJsonId();

希望这会有所帮助。

【讨论】:

  • 你好 Andrew,请在 getView() 部分指导我;它应该包含代码块的哪一部分?单击该项目时,我仍然感到有些迷恋。
  • getView() 在您的 MyListAdapter 课程中。这是您的列表视图绘制每个列表项的方式
  • 好的,我只需要对 onItemClick() 函数做一个简单的解释。能够敬酒并很好地查看点击的项目,但我怎样才能将它解析为另一个活动而不会崩溃?在我上面更新的 Activity2_view 类上,我希望能捕捉到它。从那时起,我相信拥有价值我可以进步得很好。检查我上面更新的代码。
  • 发布你的 logcat 如果它崩溃了,我们需要查看错误
【解决方案2】:

根据我对您的代码的理解...

String stringId = mLinked.get(position);

Intent mIntent = new Intent(this,Activity2.class);
mIntent.putExtra(KEY,stringId)
startActivity(mIntent);

【讨论】:

  • 在 lv.setOnItemClickListener(new OnItemClickListener() 函数中使用您的代码;我收到一个错误:(46, 51) 错误:从内部类中访问局部变量 mLinked;需要声明最终。
  • Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();代码响应非常好,事实上点击的项目可以在点击时显示,但这不是我想要的。
  • 是的,当您在内部匿名覆盖实例中使用时,一定会发生这种情况。你有没有试过让它成为最终的。 [你可以,因为这个链表引用永远不会被覆盖],更好的是让 mLinked 解决一个类级别的变量问题。
【解决方案3】:
lv.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // When clicked, show a toast with the TextView text
                            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
                            // Start a new intent - get the id from the view and send to another activity for JSON
                            // selected item
                            String product = ((TextView) view).getText().toString();

                            // Launching new Activity on selecting single List Item
                            Intent i = new Intent(getApplicationContext(), DrugIndexing_view.class);
                            // sending data to new activity
                            i.putExtra("product", product);
                            startActivity(i);
                            // End intent
                    }

            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2016-01-31
    • 2022-01-10
    • 2018-07-28
    相关资源
    最近更新 更多