从整数数组列表中选择

时间:2018-09-11 15:28:30

标签: java android

在Android Studio中工作,并且是更大应用程序的一部分。我将拥有一个ArrayList of Integers,并将其发送到新的实例/屏幕,其中列表中的每个整数都显示一个字符串描述,用户可以从中选择所需的任何条目。一旦他们单击它,我希望新实例发回与他们选择的数组中的位置相对应的整数,以便我可以将其用于下一个操作。

我将此程序作为一个独立程序制作,只是为了降低此操作的速度,但它无法正常工作(ListChooser已停止加载工作)

我的MainActivity.java:

package com.example.kenn.listchooser;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.hardware.camera2.TotalCaptureResult;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.ListActivity;



public class MainActivity extends ListActivity {
private static final String TOTAL_COUNT = "total_count";
public ArrayList<Integer> listValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listValues= new ArrayList<Integer>();
    listValues.add(1);
    listValues.add(2);
    listValues.add(4);
    listValues.add(5);
}

public int randomMe(View view) {
    Intent randomIntent = new Intent(this, Chooser.class);
    randomIntent.putExtra(TOTAL_COUNT,  listValues);
    startActivity(randomIntent);
    int pick=getIntent().getIntExtra (TOTAL_COUNT, 2);
    return pick;
}
public void action(View view) {
    TextView showCountTextView = (TextView) findViewById(R.id.textViewA);
    showCountTextView.setText(Integer.toString(this.randomMe(view)));
}    }

我的Chooser.java:

package com.example.kenn.listchooser;
import ...
public class Chooser extends ListActivity{
private String TOTAL_COUNT = "total_count";
private TextView text;
private ArrayList<Integer> listHere;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.chooser);
    List<Integer> def = new ArrayList<Integer>();
    listHere=getIntent().getIntegerArrayListExtra(TOTAL_COUNT);
    text = (TextView) findViewById(R.id.mainText);
    // initiate the listadapter
    ArrayAdapter<Integer> myAdapter = new ArrayAdapter <Integer>(this,
            R.layout.row_layout, R.id.listText, listHere);
    // assign the list adapter
    setListAdapter(myAdapter);
}
// when an item of the list is clicked
@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
    super.onListItemClick(list, view, position, id);
    String selectedItem = (String) getListView().getItemAtPosition(position);
    Intent randomIntent = new Intent(this, com.example.kenn.listchooser.MainActivity.class);
    randomIntent.putExtra(TOTAL_COUNT, selectedItem);
}    }

我的活动main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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"
android:background="@color/colorPrimary"
tools:context=".MainActivity">

<Button
    android:id="@+id/random_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="8dp"
    android:background="@color/colorPrimaryDark"
    android:onClick="action"
    android:text="Choose"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

<TextView
    android:id="@+id/textViewA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:text="Alert Dialog"
    android:textSize="35dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

我的Chooser.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mainText"
    android:text="My list" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/mainText"
    android:id="@android:id/list"
    android:background="#aaaaaa" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/mainText"
    android:id="@android:id/empty"
    android:text="There is no data"
    android:textStyle="bold" />
</RelativeLayout>

我的row_layout.xml:

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

<TextView
    android:id="@+id/listText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="18sp"
    android:textStyle="bold"
    android:textColor="#ff00ff" />
</LinearLayout>

0 个答案:

没有答案