0

我对 Adroid 非常陌生!我有一个登录页面,当我单击登录时,它会为用户提供一个列表视图,然后当我单击该行时,应该向服务器发出请求并根据这些凭据登录。

我使用自定义适配器制作了列表视图,但无法单击这些项目。只需要一些关于如何使其可点击的帮助。在我使用简单的适配器之前,列表是可点击的并且我的对象被返回。

当我调试时,我可以看到它点击了“AgencyListview.setOnItemClickListener(new OnItemClickListener()”行但没有使项目可点击。我的listview xml设置为可点击。PS我没有像我正在做的那样扩展ListActivity所有这一切都集中在一项活动中。

 private void showList(final String deviceId, final String TokenId) {

     findViewById(R.id.emailText).setVisibility(View.INVISIBLE);
      findViewById(R.id.passwordText).setVisibility(View.INVISIBLE);
      findViewById(R.id.loginButton).setVisibility(View.INVISIBLE);
      findViewById(R.id.listview).setVisibility(View.VISIBLE);

      final ListView AgencyListview = (ListView) findViewById(R.id.listview);
       //ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, agencyList);
       // ListAdapter adapter = new SimpleAdapter (this,agencyList, android.R.layout.item_row, 
           //     new String[] { "Eid", "Ename", "Eaddress" }, 
             //   new int[] {  R.id.TextView1,R.id.TextView2,R.id.TextView3 });
              //  setListAdapter(adapter);
         //  AgencyListview.setAdapter(arrayAdapter);


          AgencyAdapter aa = new AgencyAdapter(this, R.id.listview, list) {
            public boolean isEnabled(int position){
                return false;
            }
        };
        AgencyListview.setAdapter(aa);



AgencyListview.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
          Log.i("HelloListView", "You clicked Item: " + " at position:" + position);
        Object o = AgencyListview.getItemAtPosition(position);
        o.toString();

        url= "http://10.0.1.41/api/Account/select?deviceid="+deviceId+"&TokenId="+TokenId+"&userid=a29a5dba-d8f9-474b-92ca-b5dcadcc6390&agencyid=62b904fa-b38f-4686-b2e4-d748fa129c50";  
        new ServiceTask(){

            protected void onPostExecute(String result){
            System.out.println( "NEW MULTI***" + result);
            try {
                 userObject = new JSONObject(result);
                 HasMultiple = "false";
                convertJson(result);
                 saveSession( AgencyName);
                  startMain();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           }
            }.execute(url);
      }
    });
}

这是我的自定义适配器:

   class AgencyAdapter extends ArrayAdapter<AgencyItems> {
    private Context context;
    private List<AgencyItems> items;

    public AgencyAdapter(Context context, int textViewResourceId, List<AgencyItems> items){
        super(context, textViewResourceId, items);
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;
        if(v==null) {
            LayoutInflater layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflator.inflate(R.layout.agency_row, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.textViewItemName = (TextView)v.findViewById(R.id.agency_label_text);
            viewHolder.textViewItemValue = (TextView)v.findViewById(R.id.agency_value_text);
            v.setTag(viewHolder);
        }

        ViewHolder holder = (ViewHolder) v.getTag();
        final int currentPosition = position;
        holder.textViewItemName.setText(items.get(currentPosition).getName());
        holder.textViewItemValue.setText(items.get(currentPosition).getValue());

        return v;
    }

    public void OnClick(View v)
    {

    }
    public class ViewHolder {
        public TextView textViewItemName;
        public TextView textViewItemValue;
    }
}

这是我的机构_row

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
        android:paddingTop="@dimen/list_padding"
       android:paddingBottom="@dimen/list_padding"
      android:orientation="horizontal" >   
       <TextView 
          android:id="@+id/agency_label_text"
         android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginLeft="5dp"
          android:text="Default"
          android:layout_weight="0.3"
         android:textColor="@android:color/darker_gray"

    />
<TextView
    android:id="@+id/agency_value_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Default value"
    android:layout_weight="0.7"
    android:clickable="true"
    />
     </LinearLayout>


enter code here
4

2 回答 2

0

你不应该需要android:clickable="true"在你的 xml 文件中。很多时候,这实际上会把事情搞砸。尝试删除它。

于 2013-08-01T17:48:03.410 回答
0

我假设您的意思是您想对列表中的项目点击做出反应,而不是对被点击的列表本身做出反应。

为此,您需要在适配器的 getView 方法中放置一个侦听器,而不是您在此处描述的列表本身:

AgencyListview.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
          Log.i("HelloListView", "You clicked Item: " + " at position:" + position);
        Object o = AgencyListview.getItemAtPosition(position);
        o.toString();

        url= "http://10.0.1.41/api/Account/select?deviceid="+deviceId+"&TokenId="+TokenId+"&userid=a29a5dba-d8f9-474b-92ca-b5dcadcc6390&agencyid=62b904fa-b38f-4686-b2e4-d748fa129c50";  
        new ServiceTask(){

            protected void onPostExecute(String result){
            System.out.println( "NEW MULTI***" + result);
            try {
                 userObject = new JSONObject(result);
                 HasMultiple = "false";
                convertJson(result);
                 saveSession( AgencyName);
                  startMain();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           }
            }.execute(url);
      }
    });
}

应该:

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    if(v==null) {
        LayoutInflater layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = layoutInflator.inflate(R.layout.agency_row, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.textViewItemName = (TextView)v.findViewById(R.id.agency_label_text);
        viewHolder.textViewItemValue = (TextView)v.findViewById(R.id.agency_value_text);
        v.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) v.getTag();
    final int currentPosition = position;
    holder.textViewItemName.setText(items.get(currentPosition).getName());
    holder.textViewItemValue.setText(items.get(currentPosition).getValue());

    // put your click listener here
    v.OnClickListener{

      @Override
      public void onClick(
        // example
        activity.methodToExecute();
      }
    }

    return v;
}
于 2013-08-01T17:48:31.277 回答