- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是android的新手,想在listview中解析json数据,然后为其添加搜索功能,但我不知道如何添加搜索。
Java 类是:
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "http://10.0.2.2/quick/punk.php";
// JSON Node names
private static final String TAG_CUSTOMER = "Customer";
private static final String TAG_CUSTOMER_CODE = "customer_code";
private static final String TAG_CUSTOMER_NAME = "customer_name";
private static final String TAG_CUSTOMER_MOBILE = "customer_mobile";
private static final String TAG_CUSTOMER_ADDRESS = "customer_address";
// contacts JSONArray
JSONArray Customer = null;
@Ghi đè
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thiết lậpContentView(R.layout.main);
// Hashmap for ListView
ArrayList<>> contactList = new ArrayList<>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
thử {
// Getting Array of Contacts
Customer = json.getJSONArray(TAG_CUSTOMER);
// looping through All Contacts
for(int i = 0; i < Customer.length(); i++){
JSONObject c = Customer.getJSONObject(i);
// Storing each json item in variable
String customer_code = c.getString(TAG_CUSTOMER_CODE);
String customer_name = c.getString(TAG_CUSTOMER_NAME);
String customer_mobile = c.getString(TAG_CUSTOMER_MOBILE);
String customer_address = c.getString(TAG_CUSTOMER_ADDRESS);
// creating new HashMap
HashMap map = new HashMap();
// adding each child node to HashMap key => value
map.put(TAG_CUSTOMER_CODE, customer_code);
map.put(TAG_CUSTOMER_NAME, customer_name);
map.put(TAG_CUSTOMER_MOBILE, customer_mobile);
map.put(TAG_CUSTOMER_ADDRESS, customer_address);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_CUSTOMER_CODE, TAG_CUSTOMER_NAME,TAG_CUSTOMER_MOBILE,TAG_CUSTOMER_ADDRESS}, new int[] {
R.id.code, R.id.name,R.id.mobile,R.id.address});
setListAdapter(adapter);
}
}
xml文件是:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<>
android:id="@+id/button1"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="+" />
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
我想要的是,当用户在文本框中键入内容时,应该在列表中的数据中搜索它。谁能告诉我如何实现它?
câu trả lời hay nhất
使用自定义列表适配器在 ListView 中显示项目。在顶部显示一个 editText。对于自定义 ListView http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ .
用于搜索 ListView 。 How can I filter ListView data when typing on EditText in android .
用于自定义搜索 implement search on a custom listview .我建议您使用过滤器搜索自定义 ListView 。
biên tập
activity_main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
主要 Activity 类
public class MainActivity extends Activity {
ArrayList mTemp=new ArrayList();
ArrayList mPostingData=new ArrayList();
ArrayList< NewData> mOri = new ArrayList();
Myadapter ma;
EditText search;
NewData nd;
@Ghi đè
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thiết lậpContentView(R.layout.activity_main);
for(int i = 0; i < 20; i++)
{
// Add your Json Parsed Data here
// each item from json add it to hash map in NewData class. Arraylist of 0 contains jsondata of customer1
nd=new NewData();
nd.newDatacus.put(NewData.TAG_CUSTOMER_CODE, "i"+i);
nd.newDatacus.put(NewData.TAG_CUSTOMER_NAME, "a"+i);
nd.newDatacus.put(NewData.TAG_CUSTOMER_MOBILE, "number");
nd.newDatacus.put(NewData.TAG_CUSTOMER_ADDRESS, "address");
mOri.add(nd);
}
ma= new Myadapter(MainActivity.this);
mPostingData=mOri;
mTemp=mOri;
ListView lv= (ListView) findViewById(R.id.list);
lv.setAdapter(ma);
search= (EditText) findViewById(R.id.search);
search.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
ma.getFilter().filter(s);
ma.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
}
class Myadapter extends ArrayAdapter
{
LayoutInflater mInflater;
public void setData(ArrayList mPpst) {
mPostingData = mPpst;//contains class items data.
}
@Ghi đè
public Filter getFilter() {
return new Filter() {
@Ghi đè
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count >= 0) {
setData((ArrayList) results.values);//if results of search is null set the searched results data
} khác {
setData(mOri);// set original values
}
notifyDataSetInvalidated();
}
@Ghi đè
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
if (!TextUtils.isEmpty(constraint)) {
constraint = constraint.toString().toLowerCase();
ArrayList foundItems = new ArrayList();
if(mTemp!=null)
{
for(int i=0;i<>
{
if (mTemp.get(i).newDatacus.get(NewData.TAG_CUSTOMER_CODE).toString().contains(constraint)) {
System.out.println("My datas"+mTemp.get(i).newDatacus.get(NewData.TAG_CUSTOMER_CODE).toString());
foundItems.add(mTemp.get(i));
}
khác
{
}
}
}
result.count = foundItems.size();//search results found return count
result.values = foundItems;// return values
}
khác
{
result.count=-1;// no search results found
}
trả về kết quả;
}
};
}
public Myadapter(Context context) {
super(context, 0);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Hàm tạo sơ khai được tạo tự động
}
@Ghi đè
public int getCount() {
// TODO Phương thức tự động tạo stub
return mPostingData.size();
}
@Ghi đè
public Object getItem(int position) {
// TODO Phương thức tự động tạo stub
return position;
}
@Ghi đè
public long getItemId(int position) {
// TODO Phương thức tự động tạo stub
trả về 0;
}
@Ghi đè
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Phương thức tự động tạo stub
ViewHolder holder;
if(mOri == null ){
trả về giá trị null;
}
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list, null);
convertView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.t1=(TextView) convertView.findViewById(R.id.textView1);
holder.t2 = (TextView) convertView.findViewById(R.id.textView2);
holder.t3 = (TextView) convertView.findViewById(R.id.textView3);
convertView.setTag(holder);
} khác {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(mPostingData.get(position).newDatacus.get(NewData.TAG_CUSTOMER_CODE).toString());
holder.t2.setText(mPostingData.get(position).newDatacus.get(NewData.TAG_CUSTOMER_NAME).toString());
holder.t3.setText(mPostingData.get(position).newDatacus.get(NewData.TAG_CUSTOMER_MOBILE).toString());
return convertView;
}
}
class ViewHolder
{
TextView t1,t2,t3;
}
}
list.xml 在自定义列表适配器中膨胀
android:layout_width="phù hợp với cha mẹ"
android:layout_height="match_parent"
android:orientation="horizontal" >
<>
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="TextView" />
<>
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="TextView" />
<>
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="TextView" />
NewData 类 - 将所有数据保存在 hashmap 中
public class NewData {
public static final String TAG_CUSTOMER_CODE = "customer_code";
public static final String TAG_CUSTOMER_NAME = "customer_name";
public static final String TAG_CUSTOMER_MOBILE = "customer_mobile";
public static final String TAG_CUSTOMER_ADDRESS = "customer_address";
Hashtable newDatacus=new Hashtable();
public NewData()
{
newDatacus.put(NewData.TAG_CUSTOMER_CODE,new String());
newDatacus.put(NewData.TAG_CUSTOMER_ADDRESS,new String());
newDatacus.put(NewData.TAG_CUSTOMER_NAME,new String());
newDatacus.put(NewData.TAG_CUSTOMER_MOBILE,new String());
newDatacus.put(NewData.TAG_CUSTOMER_ADDRESS,new String());
}
}
修改以便在 for 循环中添加 json 数据。我在这里制定了搜索条件客户 ID。您可以根据需要更改此设置。此代码已经过测试并且可以正常工作。希望有人能给出更好的代码。希望对您有所帮助。
关于android - 如何在 ListView 中设置 json 解析数据,然后在其中添加搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15109406/
Tôi đã tạo một trường nơi người dùng có thể thêm bài kiểm tra. Tất cả điều này diễn ra suôn sẻ, tôi chỉ muốn khi người dùng nhấp vào (thêm một bài kiểm tra khác) thì bài kiểm tra trước đó (thêm một bài kiểm tra khác) sẽ xóa và bài kiểm tra này hiển thị trong trường mới. Vấn đề duy nhất khiến mọi thứ hoạt động tốt là nhấp chuột (thêm trường khác) trước khi thêm trường khác
Tùy chọn chuỗi [] = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
đóng cửa. Câu hỏi này không tuân thủ các nguyên tắc của Stack Overflow. Hiện tại nó không chấp nhận câu trả lời. Câu hỏi này dường như không phải về một vấn đề lập trình cụ thể, một phần mềm
Tôi đang nghiên cứu thêm chức năng cuộn jQuery vào tab điều hướng (Bootstrap 3). Tôi muốn người dùng có thể chọn tab họ muốn và có liên kết bên trong nội dung tab cuộn trơn tru đến điểm neo. Đây là mã của tôi, có thể
Tôi đang cố gắng thêm 2 tab ui nữa sau khi người dùng đăng nhập. Đầu tiên, tôi thử làm cái sau. $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
Tôi có một biểu mẫu với các thành phần được chọn và tôi muốn thêm và xóa một số thành phần đó thông qua lựa chọn. Đây là mã html (cũng có jsfiddle ở đây http://jsfiddle.net/txhajy2w/):
Viết cái này: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) tương đương với: view.backgroundColor = UICo
Được rồi, tôi muốn cộng các cột này lại với nhau nếu có bất kỳ thông tin nào trong đó. Vì vậy, giả sử tôi có tài khoản 1 2 3. Có chỗ cho 4 tài khoản, nhưng chỉ có 3 tài khoản. Làm cách nào tôi có thể tạo tập lệnh java để thêm nó. Câu trả lời hay nhất Ví dụ trực tiếp H
Tôi muốn biết liệu có thuật toán tạo sẵn hiệu quả nào để xác định xem tổng/hiệu của một tập hợp số có thể bằng các số khác nhau hay không. Ví dụ: 5, 8, 10, 2, sử dụng + hoặc - bằng 9. 5 - 8 = -3 + 10 = 7 + 2 = 9 nếu có trước
Tôi dường như có một repo git bị kẹt. Nó bị kẹt trên tất cả các lệnh thêm, cam kết cơ bản và git push trả về mọi thứ như cập nhật. Từ các bài đăng khác tôi đã thực hiện git gc và git fsck/ Tôi nghĩ các bước gỡ lỗi cơ bản là
Truy vấn Oracle SQL của tôi như sau - Q1- select hca.account_number, hca.attribute3, SUM(rcl.extends_amou
Tôi đang đọc http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
Tôi đang cố gắng thêm nút "tải thêm" và giới hạn kết quả bên dưới để không có hàng nghìn lượt tải nội dung cùng lúc trong trang danh mục đầu tư như thế này: http://typesetdesign.com/portfolio/ I' tôi đang gặp rắc rối với PHP
Tôi gặp vấn đề này, tôi đã thêm 8 hộp văn bản và nó hoạt động tốt, nhưng khi tôi thêm nhiều hộp văn bản hơn như 16 hộp văn bản, nó không thêm hộp văn bản cuối cùng. Có ai gặp phải vấn đề này? Cảm ơn trước. Liên kết trực tiếp: JAVASCRIP
thêm/xóa bản sao hàng đầu tiên mặc định không xóa Thêm/xóa bản sao mặc định hàng đầu tiên không xóa & lấy SrNo chính xác (ví dụ: thêm 3 hàng và xóa SrNo.2 sau khi thấy vấn đề)
Tôi đã mã hóa cái này nhưng nút xóa không hoạt động. Tôi không gặp bất kỳ lỗi nào trong bảng điều khiển.. var counter = 0; var dataList = document.getElementById('materi
Tôi có một đối tượng giống như mảng: [1:array[10], 2:array[2], 3:array[2], 4:array[2], 5:array[3], 6:array[1] ] Tôi đang cố gắng xóa hai phần tử đầu tiên, thực hiện một số thao tác và sau đó chèn lại chúng vào cùng một vị trí.
Phiên bản Delphi được sử dụng: 2007 Xin chào, tôi có một mảng Tecord TInfo = Tên bản ghi: Chuỗi Giá: Số nguyên var Thông tin;
Tôi đã sử dụng mã lưới cơ bản và sau đó tôi đã khai báo các hàm để thêm và xóa tiện ích thông qua các nút, nó hoạt động tốt nhưng khi tôi thêm chức năng thay đổi kích thước vào mã ở trên thì tất cả đều hoạt động (ý tôi là thay đổi kích thước kích thước, thêm và xóa tiện ích) mã js của tôi
tiêu đề 323 323 323 tiêu đề 323 323 323 tiêu đề 323 323 323 JS $(document).keydown(function(e){
Tôi là một lập trình viên xuất sắc, rất giỏi!