0

質問パート:1

製品IDと製品名をリスト項目として含むアクティビティを作成しました。各行には、特定の製品の数量を入力するために使用できる編集テキストが含まれています。行には、特定の製品を選択するためのチェックボックスも含まれています。

リストは次のようになります。

リスト

リストアイテムをクリックすると、特定のリストアイテムのIDと名前を取得できますが、ユーザーがリストアイテムに入力した数量も取得したいと思います。

これは、リストビューの生成を担当するアクティビティです。

public class PollStationActivity extends Activity {     

    // Hashmap for ListView
            ArrayList<HashMap<String, String>> PSList = new ArrayList<HashMap<String, String>>();
            String status_code_from_prev;
            List<HashMap<String, String>> fillMaps=null;
            String alert_message;
            String quantity_message;

            //quantity edittext
            EditText quantity_edit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_poll_station);


         //quantity edittext
         quantity_edit   = (EditText)findViewById(R.id.qty_editText);

        //database insertion
         DatabaseHelper db = new DatabaseHelper(this);
        ContentValues values = new ContentValues();
        try {
         db.createDataBase();
         values.put("_id", "1");
         values.put("name", "rose");
         db.insert(values);

        } catch (IOException e) {
         e.printStackTrace();
          }
        db.close();            


            ArrayList<TestItem> PSList = new ArrayList<TestItem>();            

             try {
              db.createDataBase();

              PSList =  db.getAllData();

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

             db.close();

              fillMaps = new ArrayList<HashMap<String, String>>();

                Iterator<TestItem> i = PSList.iterator();

                while(i.hasNext())
                {
                    HashMap<String, String> map = new HashMap<String, String>();
                    TestItem objPSItem = i.next();

                    map.put("name", objPSItem.NAME);
                    map.put("Id", objPSItem.ID);
                    //map.put("quantity", objPSItem.QUANTITY);
                    fillMaps.add(map);
                }


            Log.i("Size: ", ""+fillMaps.size());
            //populating listview from database
            ListView listView1 = (ListView) findViewById(R.id.poll_list_listView);          


              if (null != listView1 && null != PSList) {
                  listView1.setAdapter(new ListAdapter(PollStationActivity.this,
                 R.id.ListViewContainer, new String[fillMaps.size()]));

              }

    }       


    //class for the list and on click handler
    class ListAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;          

        public ListAdapter(Context context, int textViewResourceId,
                String[] objects) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
            this.context = context;
            this.values = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final View rowView = inflater.inflate(R.layout.list_layout, parent,
                    false);

            final HashMap<String, String> map =  fillMaps.get(position);

            TextView textView = (TextView) rowView
                     .findViewById(R.id.list_label_name);
                     textView.setText("("+map.get("Id")+") "+map.get("name"));          


            rowView.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    rowView.setBackgroundResource(R.drawable.list_bg_pressed);

                    Handler handler = new Handler();
                    Runnable r = new Runnable() {
                        public void run() {
                            rowView.setBackgroundResource(R.drawable.list_bg);

                        }
                    };
                    handler.postDelayed(r, 200);        

                    //alert box
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(PollStationActivity.this);

                     // Setting Dialog Title
                     alertDialog.setTitle("Please Note!");

                     // Setting Dialog Message
                     alertDialog.setMessage("Are you sure you want to select "+"("+map.get("Id")+") "+map.get("name")+"?");


                     // Setting Positive "Yes" Button
                     alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,int which) {

                         // Write your code here to invoke YES event
//                           Intent intent = new Intent(RegisterFirstActivity.this, RegisterSecondActivity.class);
//                              
//                              intent.putExtra("AC_Code", map.get(TAG_CODE));
//                              RegisterFirstActivity.this.startActivity(intent);

                         }
                     });

                     // Setting Negative "NO" Button
                     alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {
                         // Write your code here to invoke NO event

                         dialog.cancel();
                         }
                     });

                     // Showing Alert Message
                     alertDialog.show();

                }
            });

            return rowView;

        }
    }


    public void makeAToast(String str) {
        //yet to implement
        Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

 }

これはTestItemクラスです:

 public class TestItem {

public String ID;
public String NAME;
public String QUANTITY;
public boolean selected;

    // Empty constructor
    public TestItem(){

    }
    // constructor
    public TestItem(String id, String name, String quantity){
        this.ID = id;
        this.NAME = name;
        this.QUANTITY = quantity;
    }

    // constructor
    public TestItem(String name, String quantity){
        this.NAME = name;
        this.QUANTITY = quantity;
    }
    // getting ID
    public String getID(){
        return this.ID;
    }

    // setting id
    public void setID(String id){
        this.ID = id;
    }

    // getting name
    public String getName(){
        return this.NAME;
    }

    // setting name
    public void setName(String name){
        this.NAME = name;
    }

    // getting phone number
    public String getQuantity(){
        return this.QUANTITY;
    }

    // setting quantity
    public void setQuantity(String quantity){
        this.QUANTITY = quantity;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

 }

これはactivity_poll_station.xmlです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@drawable/main_bg">    


<RelativeLayout
    android:id="@+id/ListViewContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/poll_label_textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp" >

    <ListView
        android:id="@+id/poll_list_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ListView>

</RelativeLayout>



    </RelativeLayout>

これはlist_layout.xmlです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/library_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:background="@drawable/list_bg"
    android:padding="5dp" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="44dp"
        android:background="@null" >

        <TextView
            android:id="@+id/list_label_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="name"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="17sp" />

        <CheckBox
            android:id="@+id/item_checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>

        <EditText
            android:id="@+id/qty_editText"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:ems="10"
            android:maxLines="1"
            android:inputType="number" >

        </EditText>

    </RelativeLayout>

</LinearLayout>

すべてのリストアイテムに対して作成したエディットテキストからテキストを抽出する方法が必要です。上のテキストを抽出しようとすると

rowView.setOnClickListener(new View.OnClickListener()

このような:

// Setting Dialog Message
                     alertDialog.setMessage("Are you sure you want to select "+"("+map.get("Id")+") "+map.get("name")+"Quantity: "+quantity_edit.getText().toString()+"?");

私はのnull pointer exceptionために生成されていますrowView.setOnClickListener(new View.OnClickListener()

私は何をすべきか?回避策は何ですか?

前もって感謝します!

// ------------------------------------------------ ------------------------------ //

質問パート:2

今、私は次のようなことをしたいと思います。それをクリックすると特定の行を削除したいと思います。行は既存のリストビューからのみ削除され、新しいリストが表示されます。これを行うにはどうすればよいですか?

もう一度ありがとう!

4

3 に答える 3

2

まず、オブジェクトへの参照を取得する必要がありEditTextます。これは、次を使用して実行できます。findViewById

quantity_edit = (EditText) view.findViewById("qty_editText");
于 2013-03-07T05:53:48.333 に答える
2

カスタムアダプタのgetViewメソッドをオーバーライドします。次のように。

public class SimpleAdapter1 extends ArrayAdapter<Data> {

    public SimpleAdapter1(Context context, int textViewResourceId,
            List<Data> catDesc) {
        super(context, textViewResourceId, catDesc);
        this.items = (ArrayList<Data>) catDesc;
        this.context = context;
        itemsid = new ArrayList<Integer>();
        System.out.println(items);
    }

    @Override
    public View getView(final int position, View convertView,
            final ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.YourLayout, null);
        }

        edit = (EditText) v.findViewById(R.id.editText1);
        String tx = edit.getText().toString();
                return v;
       }
    }
于 2013-03-07T05:56:28.560 に答える
0

リストビューにカスタムアダプタを使用すると、非常に簡単になります。

あなたはこれを見ることができますCustom Adapter for List View

これは、リストビューからEditText値を取得する方法にも役立ちます

于 2013-03-07T05:50:33.890 に答える