如何从存储在数据库中的 Listview 获取值到另一个活动?使用 Android 和 Mysql

Posted

技术标签:

【中文标题】如何从存储在数据库中的 Listview 获取值到另一个活动?使用 Android 和 Mysql【英文标题】:How can i get the values from Listview stored in database to another activity? Using Android and Mysql 【发布时间】:2018-07-16 02:23:54 【问题描述】:

我在这里使用带有 mysql 数据库的 android。我已经创建了表,它还将值存储到数据库中。它也显示在 Listview 中,但是当我尝试单击 Listview 中的项目时。它不会参加其他活动。请帮帮我。

这是我在适配器中的代码

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

        TextView tvTitleEntry = listViewItem.findViewById(R.id.tvTitleEntry);
        TextView tvCityEntry = listViewItem.findViewById(R.id.tvCityEntry);

        final Entry entry = entryList.get(position);

        tvTitleEntry.setText(entry.getTitle());
        tvCityEntry.setText(entry.getCity());

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                String title = ((TextView) view).getText().toString();
                Intent i = new Intent(getApplicationContext(), ViewEntry.class);
                i.putExtra("title", title);
                startActivity(i);
            
        );

        return listViewItem;
    

另一个活动(ViewEntry 类)代码

    TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
    Intent i = getIntent();
    String title = i.getStringExtra("title");
    tvTitle.setText(title);

日志猫

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
                                                                                               at com.example.arko.torranboapplicationentry.EntriesActivity$EntryAdapter$1.onItemClick(EntriesActivity.java:125)
                                                                                               at android.widget.AdapterView.performItemClick(AdapterView.java:305)
                                                                                               at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
                                                                                               at android.widget.AbsListView$PerformClick.run(AbsListView.java:3057)
                                                                                               at android.widget.AbsListView$3.run(AbsListView.java:3864)
                                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                               at android.os.Looper.loop(Looper.java:135)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5268)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

我该如何解决这个问题?

【问题讨论】:

尝试在您的活动中设置 OnItemClickListener..not 适配器和((TextView) view).getText().toString(); --> 这里的视图是您的列表视图项目视图 不,它不工作,仍然得到同样的错误 在设置适配器时从活动中获取上下文 【参考方案1】:

String title = ((TextView) view).getText().toString();

在这一行中,您试图将列表视图项的视图转换为引发异常的文本视图。

将其更改为 onclicklistener,并从列表视图项的视图中获取文本视图

 listViewItem.setOnClickListener(new View.OnClickListener() 
    @Override
    public void onClick(View v) 
                String title = ((TextView)v.findViewById(R.id.tvTitleEntry)).getText().toString();
                Intent i = new Intent(getApplicationContext(), ViewEntry.class);
                i.putExtra("title", title);
                context.startActivity(i);
            
        );

【讨论】:

【参考方案2】:

你可以这样使用

convertView.setOnClickListener(new View.OnClickListener() 

    @Override
    public void onClick(View v) 
            String title = ((TextView) view).getText().toString();
            Intent i = new Intent(activity , ViewEntry.class);
            i.putExtra("title", title);
            startActivity(i);

    
);

并将private Activity activity ; this.activity = activity ; 添加到构造函数中

【讨论】:

【参考方案3】:

通常setOnItemClickListener不在getView()中

我做了一些基本的结构。希望对您有所帮助。

plus : 习惯了就应该使用 holder pattern。

TestActivity.class

public class TestActivity extends Activity 

private Context mContext;
private Activity mActivity;

private ListView listView;
private MyAdapter myAdapter;
private ArrayList<MyAdapter.MyData> data;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_activity);

    mContext = mActivity = this;

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

    myAdapter = new MyAdapter(mContext, data);
    listView.setAdapter(myAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            String title =data.get(position).getTitle();
            Intent i = new Intent(mActivity , ViewEntry.class);
            i.putExtra("title", title);
            startActivity(i);

        
    );



private void someProceeToLoadData()
    MyAdapter.MyData testData1 = new MyAdapter.MyData(1, "James", "downtown");
    MyAdapter.MyData testData2 = new MyAdapter.MyData(1, "Smith" "New Town");
    data.add(testData1);
    data.add(testData2);

    myAdapter.notifyDataSetChanged();



MyAdapter.clss

public class MyAdapter extends BaseAdapter 

private Context mContext;
private ArrayList<MyData> items;

private LayoutInflater inflater;

public MyAdapter(Context context, ArrayList<MyData> items) 
    this.mContext = context;
    this.items = items;
    inflater = LayoutInflater.from(mContext);


@Override
public int getCount() 
    return items.size();


@Override
public MyData getItem(int position) 
    return items.get(position);


@Override
public long getItemId(int position) 
    return items.get(position).getId();


@Override
public View getView(int position, View convertView, ViewGroup parent) 

    View listViewItem = inflater.inflate(R.layout.list_view_entries, null, true);

    TextView tvTitleEntry = listViewItem.findViewById(R.id.tvTitleEntry);
    TextView tvCityEntry = listViewItem.findViewById(R.id.tvCityEntry);

    final Entry entry = entryList.get(position);

    tvTitleEntry.setText(entry.getTitle());
    tvCityEntry.setText(entry.getCity());

    return listViewItem;


public static class MyData 
    long id;
    String title;
    String city;

    public MyData(long id, String name, String city) 
        this.id = id;
        this.title = name;
        this.city = city;
    

    public long getId() 
        return id;
    

    public String getTitle() 
        return title;
    

    public String getCity() 
        return city;
    


【讨论】:

以上是关于如何从存储在数据库中的 Listview 获取值到另一个活动?使用 Android 和 Mysql的主要内容,如果未能解决你的问题,请参考以下文章

获取 JSON 值到数组

如何从一个活动中获取edittext值到下一个活动的recyclerview?

从 sql server 获取选定的值到 php 中的下拉列表

如何从循环中获取值到刀片 - laravel

我可以在下一个活动中将字符串值从 MulitiColumn listView 存储到 Simple ListView 吗?

React Native:如何从循环中获取值到钩子 useState