如何检索此文本视图的ID号

时间:2017-07-21 08:06:01

标签: java android mysql json

如何检索特定textView的文本?因为当我点击一个ID号时,它会显示' 1'在我单击的每个文本视图中的下一个类的textview中。我使用了JSON数组,这对我来说很棘手。是的,我完全是初学者。

DisplayListView     包rjj.tutorial_jsonandlistview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



public class DisplayListView extends AppCompatActivity {

    String json_string;
    JSONObject jsonObject;
    JSONArray jsonArray;
    ContactAdapter contactAdapter;
    ListView listView;
    SearchView sv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_listview_layout);
        listView = (ListView)findViewById(R.id.listview);

        contactAdapter = new ContactAdapter(this,R.layout.row_layout);

        listView.setAdapter(contactAdapter);
        json_string = getIntent().getExtras().getString("json_data");
        //Searchview
        sv = (SearchView)findViewById(R.id.search);

        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                contactAdapter.getFilter().filter(newText);
                return true;
            }
        });
        //End of Searchview

        try {
            jsonObject = new JSONObject(json_string);
            jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            String id ,firstname , surname, age , username, password;


            while(count<jsonArray.length()){
                JSONObject JO = jsonArray.getJSONObject(count);
                id = JO.getString("id");
                firstname = JO.getString("firstname");
                surname = JO.getString("surname");
                age = JO.getString("age");
                username = JO.getString("username");
                password = JO.getString("password");
                Contacts contact = new Contacts(id, firstname, surname, age,username,password);
                contactAdapter.add(contact);


                count++;

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

    }

    public void hello(View view) {

        Intent intent = new Intent(this, Update.class);
        TextView textView = (TextView)findViewById(R.id.tx_id);


        String id = textView.getText().toString();
        intent.putExtra("id", id);
        startActivity(intent);
    }

}

我的ContactAdapter类:

package rjj.tutorial_jsonandlistview;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Julian on 7/20/2017.
 */

public class ContactAdapter extends ArrayAdapter {
    List list = new ArrayList();
    public ContactAdapter(@NonNull Context context, @LayoutRes int resource) {
        super(context, resource);

    }


    public void add(Contacts object) {
        super.add(object);
        list.add(object);
    }

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

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull 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.tx_id = (TextView)row.findViewById(R.id.tx_id);
            contactHolder.tx_firstname = (TextView)row.findViewById(R.id.tx_firstname);
            contactHolder.tx_surname = (TextView)row.findViewById(R.id.tx_surname);
            contactHolder.tx_age = (TextView)row.findViewById(R.id.tx_age);
            contactHolder.tx_username = (TextView)row.findViewById(R.id.tx_username);
            contactHolder.tx_password = (TextView)row.findViewById(R.id.tx_password);
            row.setTag(contactHolder);

        } else{
            contactHolder = (ContactHolder)row.getTag();
        }
        Contacts contacts = (Contacts)this.getItem(position);
        contactHolder.tx_id.setText(contacts.getId());
        contactHolder.tx_firstname.setText(contacts.getFirstname());
        contactHolder.tx_surname.setText(contacts.getSurname());
        contactHolder.tx_age.setText(contacts.getAge());
        contactHolder.tx_username.setText(contacts.getUsername());
        contactHolder.tx_password.setText(contacts.getPassword());
        return row;
    }

    static class ContactHolder{
        TextView tx_id, tx_firstname, tx_surname, tx_age, tx_username, tx_password;
    }

}

我的更新课程:

package rjj.tutorial_jsonandlistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Update extends AppCompatActivity {

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

        id = getIntent().getExtras().getString("id");

        TextView textView = (TextView)findViewById(R.id.textView);
        textView.setText(id);
    }
}

我的display_listview_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="rjj.tutorial_jsonandlistview.DisplayListView">

    <SearchView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:id="@+id/search"
        android:queryHint="Search..."/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/search"
        android:id="@+id/listview">

    </ListView>


</android.widget.RelativeLayout>

row_layout.xml(我用它来显示JSON数组)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="75dp">

    <TextView
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:id="@+id/tx_id"
        android:layout_alignParentLeft="true"
        android:text="ID"
        android:gravity="center"
        android:textAppearance="?android:textAppearanceLarge"
        android:onClick="hello"
        />
    <TextView
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:id="@+id/tx_firstname"
        android:layout_toRightOf="@+id/tx_id"
        android:text="Firstname"
        android:gravity="center"
        android:textAppearance="?android:textAppearanceLarge"
        />
    <TextView
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:id="@+id/tx_surname"
        android:layout_toRightOf="@+id/tx_firstname"
        android:text="Lastname"
        android:gravity="center"
        android:textAppearance="?android:textAppearanceLarge"
        />
    <TextView
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:id="@+id/tx_age"
        android:layout_toRightOf="@+id/tx_surname"
        android:text="Age"
        android:gravity="center"
        android:textAppearance="?android:textAppearanceLarge"
        />
    <TextView
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:id="@+id/tx_username"
        android:layout_toRightOf="@+id/tx_age"
        android:text="Username"
        android:gravity="center"
        android:textAppearance="?android:textAppearanceLarge"
        />
    <TextView
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:id="@+id/tx_password"
        android:layout_toRightOf="@+id/tx_username"
        android:text="Password"
        android:gravity="center"
        android:textAppearance="?android:textAppearanceLarge"
        />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在方法hello(View view)中,您不需要此字符串:

TextView textView = (TextView)findViewById(R.id.tx_id);

因为view hello(View view)这是我们的TextView。只需将其转换为TextView并从中获取文字:

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

另一种最普遍的方法:改变

TextView textView = (TextView)findViewById(R.id.tx_id);

TextView textView = (TextView)((ViewGroup)(view.getParent())).findViewById(R.id.tx_id);

通过这种方式,您可以将任何R.id用于当前列表项。