java 显示数据列表_java-搜索功能-如何将过滤的数据显示到列表视图中

我已经从网站上解析了数据,并将其显示在列表视图中,这是我的第二个活动,在第一个活动中有一个edittext和搜索按钮,在基于该关键字的editext中指定了关键字后,便可以在其中获取相关的搜索结果.吐司,但我希望它在列表视图中显示,这是代码部分.

public class Home extends ListActivity {

ArrayList> songsList;

ListView list;

LazyAdapter adapter;

JSONArray posts;

//ArrayList thats going to hold the search results

ArrayList> searchResults;

LayoutInflater inflater;

// All static variables

static final String URL = "http://www.example.com/ads/?json=get_recent_posts";

static final String KEY_POSTS = "posts";

static final String KEY_ID = "id";

static final String KEY_TITLE = "title";

static final String KEY_DATE = "date";

static final String KEY_CONTENT = "content";

static final String KEY_AUTHOR = "author";

static final String KEY_NAME = "name";

static final String KEY_ATTACHMENTS = "attachments";

static final String KEY_SLUG = "slug";

static final String KEY_THUMB_URL = "thumbnail";

static final String KEY_IMAGES = "images";

static final String KEY_URL = "url";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final EditText searchBox = (EditText) findViewById(R.id.search);

final ListView list = (ListView) findViewById(android.R.id.list);

//get the LayoutInflater for inflating the customomView

//this will be used in the custom adapter

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

final ArrayList> songsList = new ArrayList>();

// Creating JSON Parser instance

final JSONParser jParser = new JSONParser();

// getting JSON string from URL

JSONObject json = jParser.getJSONFromUrl(URL);

try {

posts = json.getJSONArray(KEY_POSTS);

// looping through all song nodes

for (int i = 0; i < posts.length(); i++) {

JSONObject c = posts.getJSONObject(i);

// Storing each json item in variable

String id = c.getString(KEY_ID);

String title = c.getString(KEY_TITLE);

String date = c.getString(KEY_DATE);

String content = c.getString(KEY_CONTENT);

// to remove all


and replace with ""

content = content.replace("
", "");

content = content.replace("

", "");

content = content.replace("

", "");

//authornumber is agin JSON Object

JSONObject author = c.getJSONObject(KEY_AUTHOR);

String name = author.getString(KEY_NAME);

String url = null;

String slug = null;

try {

JSONArray atta = c.getJSONArray("attachments");

for (int j = 0; j < atta.length(); j++) {

JSONObject d = atta.getJSONObject(j);

slug = d.getString(KEY_SLUG);

JSONObject images = d.getJSONObject(KEY_IMAGES);

JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);

url = thumbnail.getString(KEY_URL);

}

} catch (Exception e) {

e.printStackTrace();

}

// creating new HashMap

HashMap map = new HashMap();

// adding each child node to HashMap key => value

map.put(KEY_ID, id);

map.put(KEY_TITLE, title);

map.put(KEY_DATE, date);

map.put(KEY_NAME, name);

map.put(KEY_CONTENT, content);

map.put(KEY_SLUG, slug);

map.put(KEY_URL, url);

// adding HashList to ArrayList

songsList.add(map);

}

} catch (JSONException e) {

e.printStackTrace();

}

//searchResults=OriginalValues initially

searchResults = new ArrayList>(songsList);

// Getting adapter by passing json data ArrayList

adapter = new LazyAdapter(this, songsList);

list.setAdapter(adapter);

searchBox.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before, int count) {

//get the text in the EditText

String searchString = searchBox.getText().toString();

int textLength = searchString.length();

//clear the initial data set

searchResults.clear();

for (int i = 0; i < songsList.size(); i++) {

String playerName = songsList.get(i).get("title").toString();

if (textLength <= playerName.length()) {

//compare the String in EditText with Names in the ArrayList

if (searchString.equalsIgnoreCase(playerName.substring(0, textLength)))

Toast.makeText(getApplicationContext(), playerName, 1).show();

searchResults.add(songsList.get(i));

}

}

adapter.notifyDataSetChanged();

}

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

}

public void afterTextChanged(Editable s) {

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值