有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用@id/Android:list的java Android多列表

我有一个需要点击的新列表,但这个列表不起作用。永远不会调用onListItemClick。我的应用程序中有另一个应用程序按预期工作,我不知道有什么不同。我突然想到,可能有一些冲突,因为他们都在使用提供的@id/安卓:list,但他们在不同的活动中,我没有发现其他人抱怨相同的问题,所以我不确定

下面是一组代码。如果有任何建议,我将不胜感激

工作:

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    // get the item that was clicked
    v_ProjectInvestigatorSiteContact project = (v_ProjectInvestigatorSiteContact) this.getListAdapter().getItem(
            position);

    Intent myIntent = new Intent(this, Details.class);

    myIntent.putExtra(res.getString(R.string.project), project);

    startActivity(myIntent);
}// onListItemClick

不工作:

    @Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);

    // get the item that was clicked
    final v_SitePeople vSitePeople = (v_SitePeople) this.getListAdapter().getItem(
            position);

    AlertDialog.Builder builder = new AlertDialog.Builder(Share.this);
    builder.setTitle(res.getString(R.string.forgot_password_check_dialog_title))
            .setMessage(res.getString(R.string.share_check_dialog_text))
            .setPositiveButton(res.getString(R.string.send), new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    sendShareEmail(vSitePeople);
                }
            }).setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    // cancelled, so do nothing
                }
            });
    AlertDialog msgBox = builder.create();
    msgBox.show();
}// onListItemClick

工作XML:

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

<!-- dummy item to prevent edittext from gaining focus on activity start -->

<LinearLayout
    安卓:layout_width="0px"
    安卓:layout_height="0px"
    安卓:focusable="true"
    安卓:focusableInTouchMode="true" />

<RelativeLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:background="@drawable/title_background" >

    <ImageView
        安卓:id="@+id/logo"
        安卓:layout_width="30dp"
        安卓:layout_height="30dp"
        安卓:layout_centerVertical="true"
        安卓:layout_marginLeft="5dp"
        安卓:scaleType="centerCrop"
        安卓:src="@drawable/ic_logo" >
    </ImageView>

    <TextView
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_toRightOf="@+id/logo" 
        安卓:gravity="center"
        安卓:paddingBottom="5dp"
        安卓:paddingLeft="50dp"
        安卓:paddingRight="60dp"
        安卓:paddingTop="5dp"
        安卓:text="@string/app_header"
        安卓:textColor="#ffffffff"
        安卓:textSize="15sp"
        安卓:textStyle="bold" />
</RelativeLayout>

<RelativeLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:background="@drawable/search_gradient" >

    <ImageView
        安卓:id="@+id/searchBoxIcon"
        安卓:layout_width="38dp"
        安卓:layout_height="38dp"
        安卓:layout_centerVertical="true"
        安卓:layout_marginLeft="5dp"
        安卓:layout_marginRight="5dp"
        安卓:scaleType="centerCrop"
        安卓:src="@drawable/action_search" >
    </ImageView>

    <EditText
        安卓:id="@+id/searchBox"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_centerVertical="@+id/searchBoxIcon"
        安卓:layout_marginRight="8dp"
        安卓:layout_marginTop="4dp"
        安卓:layout_toRightOf="@+id/searchBoxIcon"
        安卓:background="@drawable/search_box"
        安卓:hint="@string/search_hint"
        安卓:inputType="text"
        安卓:maxLines="1"
        安卓:minHeight="30sp"
        安卓:paddingBottom="2dp"
        安卓:paddingLeft="25sp"
        安卓:paddingTop="2dp"
        安卓:textColor="#ff000000" />
</RelativeLayout>

<ListView
    安卓:id="@id/安卓:list"
    安卓:layout_width="match_parent"
    安卓:layout_height="0dp"
    安卓:layout_weight="1"
    安卓:background="@color/divider_gray"
    安卓:cacheColorHint="#00000000"
    安卓:divider="@color/divider_gray"
    安卓:dividerHeight="1dp"
    安卓:footerDividersEnabled="false"
    安卓:headerDividersEnabled="false" />

<TextView
    安卓:id="@+id/安卓:empty"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:gravity="center"
    安卓:text="@string/loading"
    安卓:textColor="@color/loading_gray"
    安卓:textSize="20sp" />

</LinearLayout>

不工作的XML

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

<!-- dummy item to prevent edittext from gaining focus on activity start -->

<LinearLayout
    安卓:layout_width="0px"
    安卓:layout_height="0px"
    安卓:focusable="true"
    安卓:focusableInTouchMode="true" />

<RelativeLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:background="@drawable/title_background" >

    <ImageView
        安卓:id="@+id/logo"
        安卓:layout_width="30dp"
        安卓:layout_height="30dp"
        安卓:layout_centerVertical="true"
        安卓:layout_marginLeft="5dp"
        安卓:scaleType="centerCrop"
        安卓:src="@drawable/ic_logo" >
    </ImageView>

    <TextView
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_toRightOf="@+id/logo"
        安卓:gravity="center"
        安卓:paddingBottom="5dp"
        安卓:paddingLeft="50dp"
        安卓:paddingRight="60dp"
        安卓:paddingTop="5dp"
        安卓:text="@string/share_header"
        安卓:textColor="#ffffffff"
        安卓:textSize="15sp"
        安卓:textStyle="bold" />
</RelativeLayout>

<ListView
    安卓:id="@id/安卓:list"
    安卓:layout_width="match_parent"
    安卓:layout_height="0dp"
    安卓:layout_weight="1"
    安卓:background="@color/divider_gray"
    安卓:cacheColorHint="#00000000"
    安卓:divider="@color/divider_gray"
    安卓:dividerHeight="1dp"
    安卓:footerDividersEnabled="false"
    安卓:headerDividersEnabled="false" />

<TextView
    安卓:id="@+id/安卓:empty"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:gravity="center"
    安卓:text="@string/loading"
    安卓:textColor="@color/loading_gray"
    安卓:textSize="20sp" />

</LinearLayout>

这里有更多关于坏代码是如何工作的,以防您需要更多代码

不工作行XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="?安卓:attr/listPreferredItemHeight"
安卓:layout_weight="1"
安卓:baselineAligned="false"
安卓:orientation="vertical"
安卓:padding="6dp"
安卓:background="#ffffffff"
安卓:layout_margin="10dp" >

<TextView
    安卓:id="@+id/toptext"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:layout_toLeftOf="@id/moreInfo"
    安卓:gravity="center_vertical"
    安卓:minHeight="20sp"
    安卓:textColor="#ff000000"
    安卓:textStyle="bold" />

</RelativeLayout>

不工作的视图适配器

    @Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder = null;
    if (convertView == null)
    {
        holder = new ViewHolder();

        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = vi.inflate(R.layout.share_row, null);
        convertView.setTag(holder);
    }// if
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    v_SitePeople i = items.get(position);

    if (i != null)
    {
        TextView topText = (TextView) convertView.findViewById(R.id.toptext);
        topText.setGravity(Gravity.CENTER_VERTICAL);
        topText.setMinHeight(40);
        if (topText != null)
        {
            if (i.SitePerson != null)
            {
                if (i.PersonTitle != null)
                {
                    topText.setText(String.format(i.SitePerson + ", " + i.PersonTitle));
                }
                else
                {
                    topText.setText(i.SitePerson);
                }
            }// if has ProtocolNumber
            else
            {
                if (i.Nickname != null)
                {
                    topText.setText(i.Nickname);
                }
            }// if does not have ProtocolNumber
        }// if
    }// if
    return convertView;
}// getView

非常感谢你的帮助

需要更多的代码

工作setListAdapter:

    public void updateDisplay(ArrayList<v_ProjectInvestigatorSiteContact> vProjectInvestigatorSiteContactList)
{
    if (fullAdapter != null)
    {
        if (fullAdapter.isEmpty())
        {
            fullAdapter = new ProjectAdapter(this, R.layout.row, vProjectInvestigatorSiteContactList);
            fullAdapter = reOrder(fullAdapter);
            setListAdapter(fullAdapter);
        }
        else
        {
            filteredAdapter = new ProjectAdapter(this, R.layout.row, vProjectInvestigatorSiteContactList);
            filteredAdapter = reOrder(filteredAdapter);
            setListAdapter(filteredAdapter);
        }
    }
    else
    {
        fullAdapter = new ProjectAdapter(this, R.layout.row, vProjectInvestigatorSiteContactList);
        fullAdapter = reOrder(fullAdapter);
        setListAdapter(fullAdapter);
    }
}// updateDisplay

工作列表设置:

    private class DownloadProjectsTask extends AsyncTask<String, Void, ArrayList<v_ProjectInvestigatorSiteContact>> {
    @Override
    protected ArrayList<v_ProjectInvestigatorSiteContact> doInBackground(String... URLs)
    {
        return ProjectsHelper.parseProjects(URLs, CurrentStudies.this);
    }// doInBackground

    @Override
    protected void onPostExecute(ArrayList<v_ProjectInvestigatorSiteContact> vProjectInvestigatorSiteContactList)
    {
        updateDisplay(vProjectInvestigatorSiteContactList);
    }// onPostExecute
}// DownloadProjectsTask

工作适配器:

公共类ProjectAdapter扩展了ArrayAdapter{ 私有ArrayList项; 私人资源

public ProjectAdapter(Context context, int textViewResourceId, ArrayList<v_ProjectInvestigatorSiteContact> items)
{
    super(context, textViewResourceId, items);
    this.items = items;
    this.res = context.getResources();
}// ProjectAdapater

@Override
public int getCount()
{
    return items.size();
}

@Override
public v_ProjectInvestigatorSiteContact getItem(int position)
{
    return items.get(position);
}

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

@Override
public int getViewTypeCount()
{
    return 2;
}

@Override
public int getItemViewType(int position)
{
    if (items.get(position).ProjectID == null)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

@Override
public boolean isEnabled(int position)
{
    if (getItemViewType(position) == 0)
    {
        return false;
    }
    return true;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder = null;
    int type = getItemViewType(position);
    if (convertView == null)
    {
        holder = new ViewHolder();

        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (type == 0)
        {
            convertView = vi.inflate(R.layout.row_header, null);
        }
        else
        {
            convertView = vi.inflate(R.layout.row, null);
        }
        convertView.setTag(holder);
    }// if
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    v_ProjectInvestigatorSiteContact i = items.get(position);

    if (i != null)
    {
        TextView topText = (TextView) convertView.findViewById(R.id.toptext);
        TextView bottomText = (TextView) convertView.findViewById(R.id.bottomtext);
        if (topText != null)
        {
            if (i.ProtocolNumber != null)
            {
                if (i.WebIndication != null)
                {
                    topText.setText(i.ProtocolNumber);
                    bottomText.setText(i.WebIndication);
                }
                else if (i.ProjectName != null)
                {
                    topText.setText(i.ProtocolNumber);
                    bottomText.setText(i.ProjectName);
                }
            }// if has ProtocolNumber
            else
            {
                if (i.WebIndication != null)
                {
                    topText.setText(i.WebIndication);
                    topText.setMinHeight(40);
                    topText.setGravity(Gravity.CENTER_VERTICAL);
                }
                else if (i.ProjectName != null)
                {
                    topText.setText(i.ProjectName);
                    topText.setMinHeight(40);
                    topText.setGravity(Gravity.CENTER_VERTICAL);
                }
                else
                {
                    topText.setText(i.SiteID);
                    topText.setMinHeight(40);
                    topText.setGravity(Gravity.CENTER_VERTICAL);
                }
            }// if does not have ProtocolNumber
        }// if

        // Coming Soon logic
        if (i.ProjectStatusID != null)
        {
            if (i.ProjectStatusID.equals(res.getString(R.string.feasibility_id)))
            {
                topText.setTextColor(res.getColor(R.color.coming_soon_gray));
                bottomText.setText("(Coming Soon)");
            }
            else {
                topText.setTextColor(Color.BLACK);
            }
        }//if

        if (bottomText != null)
        {
            if (type == 0)
            {
                if (position == getCount() - 1 || getItemViewType(position + 1) == 0)
                {
                    bottomText.setVisibility(View.VISIBLE);
                    bottomText.setText(res.getString(R.string.no_studies));
                    bottomText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 0, (float) 1.0));
                }
                else 
                {
                    bottomText.setVisibility(View.GONE);
                }
            }// header followed by another header, or end of list
        }// if

        //recenters row headers
        if (type == 0)
        {
            topText.setGravity(Gravity.CENTER);
        }
    }// if
    return convertView;
}// getView

public static class ViewHolder {
    public TextView textView;
}

}//ProjectAdapter

setListAdapter不工作:

    public void updateDisplay(ArrayList<v_SitePeople> vSitePeopleShareList)
{
        adapter = new ShareAdapter(this, R.layout.share_row, vSitePeopleShareList);
        setListAdapter(adapter);
}// updateDisplay

非工作列表设置:

    private class DownloadShareTask extends AsyncTask<String, Void, ArrayList<v_SitePeople>> {
    @Override
    protected ArrayList<v_SitePeople> doInBackground(String... params)
    {
        return ProjectsHelper.getSharePeople(params[0], Share.this);
    }// doInBackground

    @Override
    protected void onPostExecute(ArrayList<v_SitePeople> vSitePeopleShareList)
    {
        updateDisplay(vSitePeopleShareList);
    }// onPostExecute
}// DownloadProjectsTask

共 (1) 个答案

  1. # 1 楼答案

    我想出来了。我从来没有发布过相关代码,否则我相信你们会帮我发现的

    我从一段工作代码中复制了一个不工作的适配器,如果viewType为0,该代码将返回false。由于我的第二个列表只有一种类型,所以所有内容都被禁用

    谢谢大家的努力。很抱歉,我没有给你正确的密码