在列表项目的文本视图上链接文本加入项目在另一个列表中单击视图

时间:2016-06-07 07:15:21

标签: android list

我有两个彼此相邻的列表视图,它们的项目都有文本视图。我需要将第二个列表中的文本设置为用户在第一个列表中单击的项目的文本。关键是让用户定义一个事物的顺序,所以无论他在第一个列表上选择哪个项目,我都需要在第0个位置的第二个项目列表上设置文本,然后依此类推。

这是我的xml:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/assaignmentListViewLeft"
            android:layout_weight="0.5"/>

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/assaignmentListViewRight"
            android:layout_weight="0.5"/>
    </LinearLayout>

我的适配器:

public class ListLeftAdapter extends ArrayAdapter<RoomateModel> {

    public Context context;

    public ListLeftAdapter(Context context, ArrayList<RoomateModel> items){
        super(context, 0, items);
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.assaignment_list_item_left, parent, false);
        }

        TextView name = (TextView) convertView.findViewById(R.id.profileName);
        name.setText("my text");

        return convertView;
    }

我的第二个列表适配器:

public class ListRightAdapter extends ArrayAdapter<RoomateModel> {

public Context context;

public ListRightAdapter(Context context, ArrayList<RoomateModel> items){
        super(context, 0, items);
        this.context = context;
        }

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.assaignment_list_item_right, parent, false);
        }

        TextView name = (TextView) convertView.findViewById(R.id.textView5);

        name.setVisibility(View.INVISIBLE);

        return convertView;
        }

我的主要活动:

public class AddNewAssaignment extends AppCompatActivity {
    ArrayList<RoomateModel> list;
    ListLeftAdapter leftAdapter;
    ListRightAdapter rightAdapter;
    ListView lvLeft;
    ListView lvRight;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_assaignment);
        list = new ArrayList<RoomateModel>();
        lvLeft = (ListView)findViewById(R.id.assaignmentListViewLeft);
        lvRight = (ListView)findViewById(R.id.assaignmentListViewRight);
        lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // here i need to change the text in the 2nd list view items

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

您需要更新ListView for ListView或someData和notifyDataSetChanged,只有这样才能访问以前的列表项