English 中文(简体)
Anders - onClick in Adaptationer not work
原标题:Android - onClick in adapter not working

设想:

用户点击一个顿,并添加一行(火箭)。 用户的投入具有价值,并点击生成的纽扣吨。 该数值将计算并在编辑文本中展示。

问题: 价值没有计算,也没有在编辑文本中显示。 我做了什么错误?

custom_list_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="fill_parent"
android:layout_height="wrap_content">  

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">

<EditText
android:id="@+id/name"
android:layout_width="125dp" 
android:layout_height="wrap_content"
android:hint="Name"
android:maxLength="10"
/>

<EditText
android:id="@+id/price"
android:layout_width="80dp" 
android:layout_height="wrap_content"
android:hint="Price"
android:inputType="numberDecimal"
android:maxLength="5"
/>

<Button
android:id="@+id/calculate"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:text="Calculate"
/>

</LinearLayout>

<EditText
android:id="@+id/result"
android:layout_width="150dp" 
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:editable="false" 
android:cursorVisible="false"
android:text="$0.00"
/>    

主编:java<<EDITTED>>

public class AdvancedBillSplitter extends Activity{

ArrayList<String> noteList = new ArrayList<String>();
FancyAdapter aa = null;

Button calculate;
//EditText price;
EditText result;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView myListView = (ListView)findViewById(R.id.noteList);
    aa = new FancyAdapter();

    myListView.setAdapter(aa);

    myListView.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> arg0, View arg1,
                 int position, long arg3) {
           EditText price = (EditText)findViewById(R.id.price);
           price = (EditText)findViewById(R.id.price);
           double price1 = Double.parseDouble(price.getText().toString());
           double total = price1 *1.1;
           String resultPrice = Double.toString(total);
           result.setText(resultPrice);
           System.out.println(total);

        }
    });
    aa.notifyDataSetChanged();


    Button btnSimple = (Button)findViewById(R.id.btnSimple);        

    btnSimple.setOnClickListener(new OnClickListener() {
        public void onClick(View v)
        {
            noteList.add(0, "");
            aa.notifyDataSetChanged();

        }
    });        

}

class FancyAdapter extends ArrayAdapter<String>
{
    Button calculate;
    EditText price;
    EditText result;

    FancyAdapter()
    {
        super(AdvancedBillSplitter.this, android.R.layout.simple_list_item_1, noteList);
    }

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

        if(row == null)
        {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.custom_list_item, null);
        }

        return (row);
    }
}

}
问题回答

simple addition,

<Button
android:id="@+id/calculate"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Calculate"
/>

由于该清单有2个可集中处理的项目(清单项目和纽伦),因此目前无法追踪点击事件。 因此,纽特州不能成为焦点。 它仍然收集点击事件。

或许请您使用建议的方法OnItemClickListener (

1. 斜线

EditText Price = (EditText)findViewById(R.id.price);

页: 1

EditText Price = (EditText)view1.findViewById(R.id.price);

(说明意见1)

另外,您有<条码>限定语ById(两条。

You can use the Handler to update your view.
Like this:

Handler mHandler = new Handler() {
@Override

    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:
            aa.notifyDataSetChanged();
            break;
        default:
            break;
        }

    }};

btnSimple.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
        noteList.add(0, "");
        mHandler.sendEmptyMessage(0);
    }
});

您不使用<条码>新调试器View.OnItemClickListener (,可使用new OnItemClickListener()进口import android.widget.AdapterView.OnItemClickListener;

www.un.org/Depts/DGACM/index_spanish.htm Dont有点听众,以了解预定布局以及家长布局(名单见)。 如果你们的布局不定,那么控制就会直接转移。

myListViewmyListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                  price = (EditText)findViewById(R.id.price);
                  double price1 = Double.parseDouble(price.getText().toString());
                  double total = price1 *1.1;
                  String resultPrice = Double.toString(total);
                  result.setText(resultPrice);

            }
        });

我认为这可能给你一些想法。 一旦数据发生变化,就试图使用<代码>,以示DataSetChanged()方法来更新看法。

一切最好。

class FancyAdapter extends BaseAdapter
{
Button calculate;
EditText price;
EditText result;



    @Override
    public int getCount() {
        return noteList.length;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;     

            if(row == null)
            {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.custom_list_item, null);
            }

            return (row);
    }

}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签