列表视图

时间:2016-04-04 07:31:11

标签: android listview custom-adapter

我正在尝试更改自定义列表视图的图标,但它一直在崩溃。 如何更改每个页面的图标?,例如:

我尝试在Forloop中添加Icon但是这只会让它崩溃,我也尝试谷歌它,但无法找到解决我问题的任何东西。另外我最近才开始开发android。所以我对此并不了解。

这是"文字页面"它将图标设置为文本图标。

Text Page

这是"新闻页面"我希望它有新闻图标,但现在它是这样的:

News Page

这是我用来生成"文字页面"

的代码
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.comp_cont_bar);

    ListView lv = (ListView) findViewById(R.id.lv1);
    registerForContextMenu(lv);

    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);

    mSwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                finish();
                startActivity(getIntent());
                }
            }
    );

    try {
    APIClient api = new APIClient();
    JSONArray result = null;

    try {
        result = api.execute("http://test.soundwave.drieo.nl/api/content" + apikey).get();
    }
    catch (Exception e) {
       e.printStackTrace();
    }

    List<CustomListView> contents = new ArrayList<CustomListView>();

    for(int i = 0; i < result.length(); i++){
        try {
            JSONObject row = result.getJSONObject(i);

            String content = row.optString("FormattedName");
            String content2 = row.optString("CreatedBy");
            String content3 = row.optString("Id");

            CustomListView item = new CustomListView();

            item.firstLine = content;
            item.description = content2;
            item.ID = content3;

            contents.add(item);

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

        ListAdapter theAdapter = new CustomAdapter(MainComp_Content.this, contents);
        ListView theListView = (ListView) findViewById(R.id.lv1);
        theListView.setAdapter(theAdapter);

        theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                TextView textView = (TextView) view.findViewById(R.id.tbid);
                UID = textView.getText().toString();

                Intent intent = new Intent(MainComp_Content.this, MainComp_Content_edit.class);
                intent.putExtra("uid", UID);
                startActivity(intent);
            }
        });

        theListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.tbid);
                UID = textView.getText().toString();
                return false;
            }
        });

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

自定义适配器代码(我已声明图标):

package nl.drieo.soundwave.test.cms;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class CustomAdapter extends ArrayAdapter<CustomListView> {
public CustomAdapter(Context context, List<CustomListView> contents) {
    super(context, R.layout.custom_row, contents);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater theInflater = LayoutInflater.from(getContext());

    View theView = theInflater.inflate(R.layout.custom_row, parent, false);

    CustomListView tvFirstItem = getItem(position);

    TextView theTextView = (TextView) theView.findViewById(R.id.firstLine);
    theTextView.setText(tvFirstItem.firstLine);

    TextView theTextView2 = (TextView) theView.findViewById(R.id.secondLine);
    theTextView2.setText(tvFirstItem.description);

    TextView theTextView3 = (TextView) theView.findViewById(R.id.tbid);
    theTextView3.setText(tvFirstItem.ID);

    ImageView theImageView = (ImageView) theView.findViewById(R.id.icon);
    theImageView.setImageResource(R.drawable.ic_content);

    return theView;
}
}

2 个答案:

答案 0 :(得分:1)

要回答你的问题,是的,有更好的方法来实现同样的事情。例如,如果对于新闻和测试页面,模型相同,则可以添加一个附加参数,如:

public class CustomListView {

// Along with your other properties

private boolean isTestPage;

public boolean isTestPage() {
    return isTestPage;
}

public void setIsTestPage(boolean isTestPage) {
    this.isTestPage = isTestPage;
}
}

然后你可以使用相同的CustomAdapter,

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater theInflater = LayoutInflater.from(getContext());

    View theView = theInflater.inflate(R.layout.custom_row, parent, false);

    CustomListView tvFirstItem = getItem(position);

    TextView theTextView = (TextView) theView.findViewById(R.id.firstLine);
    theTextView.setText(tvFirstItem.firstLine);

    TextView theTextView2 = (TextView) theView.findViewById(R.id.secondLine);
    theTextView2.setText(tvFirstItem.description);

    TextView theTextView3 = (TextView) theView.findViewById(R.id.tbid);
    theTextView3.setText(tvFirstItem.ID);

    ImageView theImageView = (ImageView) theView.findViewById(R.id.icon);

    if (tvFirstItem.isTestPage()) {
        // Test Page Image
        theImageView.setImageResource(R.drawable.ic_test_page);
    } else{
        // News Page Image
        theImageView.setImageResource(R.drawable.ic_news_page);
    }

    return theView;
}

当您的模型对于两者都不相同时,其他解决方案,然后您可以创建名为CustomInterface的接口并将其实现到两个模型。然后在适配器中,使用CustomInterface而不是模型,在getView中,您必须检查当前模型:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater theInflater = LayoutInflater.from(getContext());
    View theView = theInflater.inflate(R.layout.custom_row, parent, false);
    if(entity instanceof Obj1){
        // Test Page
    } else if(entity instanceof Obj2){
        // News Page
    }
    return theView;
}

请记住,List仅适用于同类集合。如果您有异构集合,则可以在所有模型上实现接口并创建接口列表,然后可以在该列表中放置任何实现相同接口的模型。

但是这个解决方案真的很复杂。简单而更好的方法是为不同的视图提供不同的适配器。

答案 1 :(得分:0)

我刚刚复制了CustomAdapter类,我只是觉得有一种比复制粘贴更好更清洁的方法。