如何使用4个子项创建ListView onClick项目详细信息视图

时间:2015-09-23 01:40:51

标签: android listview android-listview import

如何创建包含4个子项目的列表视图,其中包含名称,电话,移动电话,电子邮件

我是一个非常新的机器学习者

例如:

Name 1
Ph:123456
Mo:123456789
email:example@example.com
--------------------------
Name 2
Ph:123456
Mo:123456789
email:example@example.com
--------------------------
Name 3
Ph:123456
Mo:123456789
email:example@example.com
--------------------------

以下是我的代码,每个列表显示一行 GMOFragment.java

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.rupomkhondaker.sonalibank.adapter.PhoneListAdapter;
import com.rupomkhondaker.sonalibank.model.ContactItem;


import java.io.Serializable;
import java.util.ArrayList;

public class GMOFragment extends android.app.Fragment {
    public GMOFragment(){}


    private ArrayList<ContactItem> phoneItems;
    private PhoneListAdapter adapters;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_gmo, container, false);



        ArrayList<ContactItem> listData = getListData();

        final ListView listView = (ListView) rootView.findViewById(R.id.gmolistView);
        listView.setAdapter(new PhoneListAdapter(getActivity(), listData));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                ContactItem newsData = (ContactItem) listView.getItemAtPosition(position);

                Toast.makeText(getActivity(), "Selected :" + " " + newsData, Toast.LENGTH_LONG).show();
                Intent in = new Intent(getActivity(), ContactDetail.class);
                // sending data to new activity

      //!!! I NEED HELP HERE !!!!

                //
                startActivity(in);
            }
        });


        return rootView;
    }

  private ArrayList<ContactItem> getListData() {
        ArrayList<ContactItem> listMockData = new ArrayList<ContactItem>();
        String[] names = getResources().getStringArray(R.array.gmo_name_list);
        String[] phones = getResources().getStringArray(R.array.gmo_ph_list);
        String[] mobiles = getResources().getStringArray(R.array.gmo_mob_list);
        String[] emails = getResources().getStringArray(R.array.gmo_email_list);

        for (int i = 0; i < names.length; i++) {
            ContactItem newsData = new ContactItem();
            newsData.setName(names[i]);
            newsData.setPhone(phones[i]);
            newsData.setMobile(mobiles[i]);
            newsData.setEmail(emails[i]);

            listMockData.add(newsData);
        }
        return listMockData;
    }
}

还如何将商品和子商品数据传递给下一个活动 ContactDetailActivity.class

以下是我尝试查看数据的代码

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;


public class ContactDetailActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.fragment_service_details);

        TextView txtName = (TextView) findViewById(R.id.conName);
        TextView txtPhone = (TextView) findViewById(R.id.conPhone);
        TextView txtMobile = (TextView) findViewById(R.id.conMobile);
        TextView txtEmail = (TextView) findViewById(R.id.conEmail);


        Intent i = getIntent();
        // getting attached intent data
        String name = i.getStringExtra("KEY_DATA");
        String phone = i.getStringExtra("KEY_DATA2");
        String mobile = i.getStringExtra("KEY_DATA3");
        String email = i.getStringExtra("KEY_DATA4");
        // displaying selected contact name
        txtName.setText(name);
        txtPhone.setText(phone);
        txtMobile.setText(mobile);
        txtEmail.setText(email);

    }

}

1 个答案:

答案 0 :(得分:2)

您需要创建自己的自定义适配器和自定义视图。

查看以下教程 -

Tutorial 1

Tutorial 2

让我知道如果您无法理解这个概念。

这是一个有效的代码 -

自定义适配器 -

public class CustomAdapter extends ArrayAdapter<CustomObject> {

    public CustomAdapter(Context context, List<CustomObject> objects) {
        super(context, -1, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        View view = convertView;
        if (view == null) {
            view = View.inflate(getContext(), R.layout.list_view_item, null);
            viewHolder = new ViewHolder();
            viewHolder.setView(view);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.mName.setText(getItem(position).getName());
        viewHolder.mPhone.setText(getItem(position).getPhone());
        viewHolder.mMobile.setText(getItem(position).getMobile());
        viewHolder.mEmail.setText(getItem(position).getEmail());

        return view;
    }

    private class ViewHolder {

        public TextView mName;
        public TextView mPhone;
        public TextView mMobile;
        public TextView mEmail;

        public void setView(View view) {
            mName = (TextView) view.findViewById(R.id.textView);
            mPhone = (TextView) view.findViewById(R.id.textView2);
            mMobile = (TextView) view.findViewById(R.id.textView3);
            mEmail = (TextView) view.findViewById(R.id.textView4);
            view.setTag(this);
        }

    }
}

自定义对象 -

public class CustomObject implements Parcelable {

    public String mName;
    public String mPhone;
    public String mMobile;
    public String mEmail;

    public CustomObject(String name, String phone, String mobile, String email) {
        mName = name;
        mPhone = phone;
        mMobile = mobile;
        mEmail = email;
    }

    private CustomObject(Parcel parcel) {
        readFromParcel(parcel);
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getPhone() {
        return mPhone;
    }

    public void setPhone(String phone) {
        mPhone = phone;
    }

    public String getMobile() {
        return mMobile;
    }

    public void setMobile(String mobile) {
        mMobile = mobile;
    }

    public String getEmail() {
        return mEmail;
    }

    public void setEmail(String email) {
        mEmail = email;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mName);
        dest.writeString(mPhone);
        dest.writeString(mMobile);
        dest.writeString(mEmail);
    }

    private void readFromParcel(Parcel parcel) {
        mName = parcel.readString();
        mPhone = parcel.readString();
        mMobile = parcel.readString();
        mEmail = parcel.readString();
    }

    public static final Creator<CustomObject> CREATOR = new Creator<CustomObject>() {
        @Override
        public CustomObject createFromParcel(Parcel source) {
            return new CustomObject(source);
        }

        @Override
        public CustomObject[] newArray(int size) {
            return new CustomObject[0];
        }
    };
}

您的活动 -

public class MainActivity extends Activity {


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

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

        ArrayList<CustomObject> customObjects = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            CustomObject customObject = new CustomObject("Name", "Phone", "Mobile", "Email");
            customObjects.add(customObject);
        }

        CustomAdapter customAdapter = new CustomAdapter(this, customObjects);
        listView.setAdapter(customAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CustomObject object = customAdapter.getItem(position);
            Intent intent = new Intent(this, NextActivity.class);
            intent.putExtra("Item", customObjects);
            startActivity(intent);
        }
    });
    }

}

自定义视图(list_view_item.xml) -

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


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textView"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textView2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textView3"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textView4"/>
</LinearLayout>

activity_main.xml -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

如果有什么不合理的,请告诉我,我会向你解释。